Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FieldMapperResolver.php
Go to the documentation of this file.
1 <?php
7 
11 
13 {
19  private $objectManager;
20 
24  private $fieldMappers;
25 
31  private $fieldMapperEntity;
32 
37  public function __construct(
38  ObjectManagerInterface $objectManager,
39  array $fieldMappers = []
40  ) {
41  $this->objectManager = $objectManager;
42  $this->fieldMappers = $fieldMappers;
43  }
44 
48  public function getFieldName($attributeCode, $context = [])
49  {
50  $entityType = isset($context['entityType']) ? $context['entityType'] : Config::ELASTICSEARCH_TYPE_DEFAULT;
51  return $this->getEntity($entityType)->getFieldName($attributeCode, $context);
52  }
53 
57  public function getAllAttributesTypes($context = [])
58  {
59  $entityType = isset($context['entityType']) ? $context['entityType'] : Config::ELASTICSEARCH_TYPE_DEFAULT;
60  return $this->getEntity($entityType)->getAllAttributesTypes($context);
61  }
62 
70  private function getEntity($entityType)
71  {
72  if (empty($this->fieldMapperEntity)) {
73  if (empty($entityType)) {
74  throw new \Exception(
75  'No entity type given'
76  );
77  }
78  if (!isset($this->fieldMappers[$entityType])) {
79  throw new \LogicException(
80  'There is no such field mapper: ' . $entityType
81  );
82  }
83  $fieldMapperClass = $this->fieldMappers[$entityType];
84  $this->fieldMapperEntity = $this->objectManager->create($fieldMapperClass);
85  if (!($this->fieldMapperEntity instanceof FieldMapperInterface)) {
86  throw new \InvalidArgumentException(
87  'Field mapper must implement \Magento\Elasticsearch\Model\Adapter\FieldMapperInterface'
88  );
89  }
90  }
91  return $this->fieldMapperEntity;
92  }
93 }
$objectManager
Definition: bootstrap.php:17
__construct(ObjectManagerInterface $objectManager, array $fieldMappers=[])
$attributeCode
Definition: extend.phtml:12