FieldTranslator.php 918 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Framework\GraphQl\Query;
  8. /**
  9. * Translate field names to their database equivalent
  10. */
  11. class FieldTranslator
  12. {
  13. /**
  14. * @var string[]
  15. */
  16. private $translationMap = [];
  17. /**
  18. * @param string[] $translationMap
  19. */
  20. public function __construct(array $translationMap)
  21. {
  22. $this->translationMap = $translationMap;
  23. }
  24. /**
  25. * Return translated field name if present in configuration, otherwise return back the original passed in name.
  26. *
  27. * @param string $fieldName
  28. * @return string
  29. */
  30. public function translate(string $fieldName) : string
  31. {
  32. if (isset($this->translationMap[$fieldName])) {
  33. return $this->translationMap[$fieldName];
  34. }
  35. return $fieldName;
  36. }
  37. }