Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
EnumLookup.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
14 
19 {
23  private $typeConfig;
24 
28  private $enumDataMapper;
29 
34  public function __construct(ConfigInterface $typeConfig, DataMapperInterface $enumDataMapper)
35  {
36  $this->typeConfig = $typeConfig;
37  $this->enumDataMapper = $enumDataMapper;
38  }
39 
48  public function getEnumValueFromField(string $enumName, string $fieldValue) : string
49  {
50  $priceViewEnum = $this->typeConfig->getConfigElement($enumName);
51  if ($priceViewEnum instanceof Enum) {
52  foreach ($priceViewEnum->getValues() as $enumItem) {
53  $mappedValues = $this->enumDataMapper->getMappedEnums($enumName);
54  if (isset($mappedValues[$enumItem->getName()]) && $mappedValues[$enumItem->getName()] == $fieldValue) {
55  return $enumItem->getValue();
56  }
57  }
58  } else {
59  throw new \Magento\Framework\Exception\RuntimeException(
60  new Phrase('Enum type "%1" not defined', [$enumName])
61  );
62  }
63  return '';
64  }
65 }
getEnumValueFromField(string $enumName, string $fieldValue)
Definition: EnumLookup.php:48
__construct(ConfigInterface $typeConfig, DataMapperInterface $enumDataMapper)
Definition: EnumLookup.php:34