Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CustomAttributesMapper.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Eav\Model;
7 
14 
19 {
23  private $attributeRepository;
24 
28  private $metadataPool;
29 
33  private $searchCriteriaBuilder;
34 
38  private $attributes;
39 
45  public function __construct(
46  AttributeRepositoryInterface $attributeRepository,
47  MetadataPool $metadataPool,
48  SearchCriteriaBuilder $searchCriteriaBuilder
49  ) {
50  $this->attributeRepository = $attributeRepository;
51  $this->metadataPool = $metadataPool;
52  $this->searchCriteriaBuilder = $searchCriteriaBuilder;
53  }
54 
59  {
60  if (!$this->metadataPool->hasConfiguration($entityType)
61  || !$this->metadataPool->getMetadata($entityType)->getEavEntityType()
62  ) {
63  return $data;
64  }
66  foreach ($this->getNonStaticAttributes($entityType) as $attribute) {
67  foreach ($data[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES] as $key => $customAttribute) {
68  if ($customAttribute[AttributeInterface::ATTRIBUTE_CODE] == $attribute->getAttributeCode()) {
70  $data[$attribute->getAttributeCode()] = $customAttribute[AttributeInterface::VALUE];
71  }
72  }
73  }
74  }
75  return $data;
76  }
77 
82  {
83  $metadata = $this->metadataPool->getMetadata($entityType);
84  if (!$metadata->getEavEntityType()) {
85  return $data;
86  }
87  foreach ($this->getNonStaticAttributes($entityType) as $attribute) {
88  if (isset($data[$attribute->getAttributeCode()])) {
90  AttributeInterface::ATTRIBUTE_CODE => $attribute->getAttributeCode(),
91  AttributeInterface::VALUE => $data[$attribute->getAttributeCode()]
92  ];
93  }
94  }
95  return $data;
96  }
97 
105  private function getNonStaticAttributes($entityType)
106  {
107  if (!isset($this->attributes[$entityType])) {
108  $metadata = $this->metadataPool->getMetadata($entityType);
109  $searchResult = $this->attributeRepository->getList(
110  $metadata->getEavEntityType(),
111  $this->searchCriteriaBuilder->addFilter('attribute_set_id', null, 'neq')->create()
112  );
113  $attributes = [];
114  foreach ($searchResult->getItems() as $attribute) {
115  if (!$attribute->isStatic()) {
116  $attributes[] = $attribute;
117  }
118  }
119  $this->attributes[$entityType] = $attributes;
120  }
121  return $this->attributes[$entityType];
122  }
123 }
$searchCriteriaBuilder
__construct(AttributeRepositoryInterface $attributeRepository, MetadataPool $metadataPool, SearchCriteriaBuilder $searchCriteriaBuilder)