-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
Copy pathCustomAttributesMapper.php
123 lines (114 loc) · 3.92 KB
/
CustomAttributesMapper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Eav\Model;
use Magento\Eav\Api\AttributeRepositoryInterface;
use Magento\Framework\Api\AttributeInterface;
use Magento\Framework\Api\CustomAttributesDataInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\EntityManager\MapperInterface;
use Magento\Framework\EntityManager\MetadataPool;
/**
* Class CustomAttributesMapper
*/
class CustomAttributesMapper implements MapperInterface
{
/**
* @var AttributeRepositoryInterface
*/
private $attributeRepository;
/**
* @var MetadataPool
*/
private $metadataPool;
/**
* @var SearchCriteriaBuilder
*/
private $searchCriteriaBuilder;
/**
* @var array
*/
private $attributes;
/**
* @param AttributeRepositoryInterface $attributeRepository
* @param MetadataPool $metadataPool
* @param SearchCriteriaBuilder $searchCriteriaBuilder
*/
public function __construct(
AttributeRepositoryInterface $attributeRepository,
MetadataPool $metadataPool,
SearchCriteriaBuilder $searchCriteriaBuilder
) {
$this->attributeRepository = $attributeRepository;
$this->metadataPool = $metadataPool;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
}
/**
* {@inheritdoc}
*/
public function entityToDatabase($entityType, $data)
{
if (!$this->metadataPool->hasConfiguration($entityType)
|| !$this->metadataPool->getMetadata($entityType)->getEavEntityType()
) {
return $data;
}
if (isset($data[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES])) {
foreach ($this->getNonStaticAttributes($entityType) as $attribute) {
foreach ($data[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES] as $key => $customAttribute) {
if ($customAttribute[AttributeInterface::ATTRIBUTE_CODE] == $attribute->getAttributeCode()) {
unset($data[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES][$key]);
$data[$attribute->getAttributeCode()] = $customAttribute[AttributeInterface::VALUE];
}
}
}
}
return $data;
}
/**
* {@inheritdoc}
*/
public function databaseToEntity($entityType, $data)
{
$metadata = $this->metadataPool->getMetadata($entityType);
if (!$metadata->getEavEntityType()) {
return $data;
}
foreach ($this->getNonStaticAttributes($entityType) as $attribute) {
if (isset($data[$attribute->getAttributeCode()])) {
$data[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES][] = [
AttributeInterface::ATTRIBUTE_CODE => $attribute->getAttributeCode(),
AttributeInterface::VALUE => $data[$attribute->getAttributeCode()]
];
}
}
return $data;
}
/**
* Get custom attributes
*
* @param string $entityType
* @return \Magento\Eav\Api\Data\AttributeInterface[]
* @throws \Exception
*/
private function getNonStaticAttributes($entityType)
{
if (!isset($this->attributes[$entityType])) {
$metadata = $this->metadataPool->getMetadata($entityType);
$searchResult = $this->attributeRepository->getList(
$metadata->getEavEntityType(),
$this->searchCriteriaBuilder->addFilter('attribute_set_id', null, 'neq')->create()
);
$attributes = [];
foreach ($searchResult->getItems() as $attribute) {
if (!$attribute->isStatic()) {
$attributes[] = $attribute;
}
}
$this->attributes[$entityType] = $attributes;
}
return $this->attributes[$entityType];
}
}