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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\Model;
use Magento\Framework\Api\AttributeValueFactory;
use Magento\Framework\Api\ExtensionAttributesFactory;
/**
* Abstract model with custom attributes support.
*
* This class defines basic data structure of how custom attributes are stored in an ExtensibleModel.
* Implementations may choose to process custom attributes as their persistence requires them to.
* @SuppressWarnings(PHPMD.NumberOfChildren)
*/
abstract class AbstractExtensibleModel extends AbstractModel implements
\Magento\Framework\Api\CustomAttributesDataInterface
{
/**
* @var ExtensionAttributesFactory
*/
protected $extensionAttributesFactory;
/**
* @var \Magento\Framework\Api\ExtensionAttributesInterface
*/
protected $extensionAttributes;
/**
* @var AttributeValueFactory
*/
protected $customAttributeFactory;
/**
* @var string[]
*/
protected $customAttributesCodes = null;
/**
* @var bool
*/
protected $customAttributesChanged = false;
/**
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
* @param ExtensionAttributesFactory $extensionFactory
* @param AttributeValueFactory $customAttributeFactory
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
* @param array $data
*/
public function __construct(
\Magento\Framework\Model\Context $context,
\Magento\Framework\Registry $registry,
ExtensionAttributesFactory $extensionFactory,
AttributeValueFactory $customAttributeFactory,
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
array $data = []
) {
$this->extensionAttributesFactory = $extensionFactory;
$this->customAttributeFactory = $customAttributeFactory;
$data = $this->filterCustomAttributes($data);
parent::__construct($context, $registry, $resource, $resourceCollection, $data);
if (isset($data['id'])) {
$this->setId($data['id']);
}
if (isset($data[self::EXTENSION_ATTRIBUTES_KEY]) && is_array($data[self::EXTENSION_ATTRIBUTES_KEY])) {
$this->populateExtensionAttributes($data[self::EXTENSION_ATTRIBUTES_KEY]);
}
}
/**
* Verify custom attributes set on $data and unset if not a valid custom attribute
*
* @param array $data
* @return array processed data
*/
protected function filterCustomAttributes($data)
{
if (empty($data[self::CUSTOM_ATTRIBUTES])) {
return $data;
}
$customAttributesCodes = $this->getCustomAttributesCodes();
$data[self::CUSTOM_ATTRIBUTES] = array_intersect_key(
(array)$data[self::CUSTOM_ATTRIBUTES],
array_flip($customAttributesCodes)
);
foreach ($data[self::CUSTOM_ATTRIBUTES] as $code => $value) {
if (!($value instanceof \Magento\Framework\Api\AttributeInterface)) {
$data[self::CUSTOM_ATTRIBUTES][$code] = $this->customAttributeFactory->create()
->setAttributeCode($code)
->setValue($value);
}
}
return $data;
}
/**
* Initialize customAttributes based on existing data
*
* @return $this
*/
protected function initializeCustomAttributes()
{
if (!isset($this->_data[self::CUSTOM_ATTRIBUTES]) || $this->customAttributesChanged) {
if (!empty($this->_data[self::CUSTOM_ATTRIBUTES])) {
$customAttributes = $this->_data[self::CUSTOM_ATTRIBUTES];
} else {
$customAttributes = [];
}
$customAttributeCodes = $this->getCustomAttributesCodes();
foreach ($customAttributeCodes as $customAttributeCode) {
if (isset($this->_data[self::CUSTOM_ATTRIBUTES][$customAttributeCode])) {
$customAttribute = $this->customAttributeFactory->create()
->setAttributeCode($customAttributeCode)
->setValue($this->_data[self::CUSTOM_ATTRIBUTES][$customAttributeCode]->getValue());
$customAttributes[$customAttributeCode] = $customAttribute;
} elseif (isset($this->_data[$customAttributeCode])) {
$customAttribute = $this->customAttributeFactory->create()
->setAttributeCode($customAttributeCode)
->setValue($this->_data[$customAttributeCode]);
$customAttributes[$customAttributeCode] = $customAttribute;
}
}
$this->_data[self::CUSTOM_ATTRIBUTES] = $customAttributes;
$this->customAttributesChanged = false;
}
}
/**
* Retrieve custom attributes values.
*
* @return \Magento\Framework\Api\AttributeInterface[]|null
*/
public function getCustomAttributes()
{
$this->initializeCustomAttributes();
// Returning as a sequential array (instead of stored associative array) to be compatible with the interface
return array_values($this->_data[self::CUSTOM_ATTRIBUTES]);
}
/**
* Get an attribute value.
*
* @param string $attributeCode
* @return \Magento\Framework\Api\AttributeInterface|null null if the attribute has not been set
*/
public function getCustomAttribute($attributeCode)
{
$this->initializeCustomAttributes();
return $this->_data[self::CUSTOM_ATTRIBUTES][$attributeCode] ?? null;
}
/**
* {@inheritdoc}
*/
public function setCustomAttributes(array $attributes)
{
return $this->setData(self::CUSTOM_ATTRIBUTES, $attributes);
}
/**
* {@inheritdoc}
*/
public function setCustomAttribute($attributeCode, $attributeValue)
{
$customAttributesCodes = $this->getCustomAttributesCodes();
/* If key corresponds to custom attribute code, populate custom attributes */
if (in_array($attributeCode, $customAttributesCodes)) {
$attribute = $this->customAttributeFactory->create();
$attribute->setAttributeCode($attributeCode)
->setValue($attributeValue);
$this->_data[self::CUSTOM_ATTRIBUTES][$attributeCode] = $attribute;
}
return $this;
}
/**
* {@inheritdoc}
*
* Added custom attributes support.
*/
public function setData($key, $value = null)
{
if (is_array($key)) {
$key = $this->filterCustomAttributes($key);
} elseif ($key == self::CUSTOM_ATTRIBUTES) {
$filteredData = $this->filterCustomAttributes([self::CUSTOM_ATTRIBUTES => $value]);
$value = $filteredData[self::CUSTOM_ATTRIBUTES];
}
$this->customAttributesChanged = true;
parent::setData($key, $value);
return $this;
}
/**
* {@inheritdoc}
*
* Unset customAttributesChanged flag
*/
public function unsetData($key = null)
{
if (is_string($key) && isset($this->_data[self::CUSTOM_ATTRIBUTES][$key])) {
unset($this->_data[self::CUSTOM_ATTRIBUTES][$key]);
}
return parent::unsetData($key);
}
/**
* Convert custom values if necessary
*
* @param array $customAttributes
* @return void
*/
protected function convertCustomAttributeValues(array &$customAttributes)
{
foreach ($customAttributes as $attributeCode => $attributeValue) {
if ($attributeValue instanceof \Magento\Framework\Api\AttributeValue) {
$customAttributes[$attributeCode] = $attributeValue->getValue();
}
}
}
/**
* Object data getter
*
* If $key is not defined will return all the data as an array.
* Otherwise it will return value of the element specified by $key.
* It is possible to use keys like a/b/c for access nested array data
*
* If $index is specified it will assume that attribute data is an array
* and retrieve corresponding member. If data is the string - it will be explode
* by new line character and converted to array.
*
* In addition to parent implementation custom attributes support is added.
*
* @param string $key
* @param string|int $index
* @return mixed
*/
public function getData($key = '', $index = null)
{
if ($key === self::CUSTOM_ATTRIBUTES) {
throw new \LogicException("Custom attributes array should be retrieved via getCustomAttributes() only.");
} elseif ($key === '') {
/** Represent model data and custom attributes as a flat array */
$customAttributes = isset($this->_data[self::CUSTOM_ATTRIBUTES])
? $this->_data[self::CUSTOM_ATTRIBUTES]
: [];
$this->convertCustomAttributeValues($customAttributes);
$data = array_merge($this->_data, $customAttributes);
unset($data[self::CUSTOM_ATTRIBUTES]);
} else {
$data = parent::getData($key, $index);
if ($data === null) {
/** Try to find necessary data in custom attributes */
$data = isset($this->_data[self::CUSTOM_ATTRIBUTES][$key])
? $this->_data[self::CUSTOM_ATTRIBUTES][$key]
: null;
if ($data instanceof \Magento\Framework\Api\AttributeValue) {
$data = $data->getValue();
}
if (null !== $index && isset($data[$index])) {
return $data[$index];
}
}
}
return $data;
}
/**
* Get a list of custom attribute codes.
*
* By default, entity can be extended only using extension attributes functionality.
*
* @return string[]
*/
protected function getCustomAttributesCodes()
{
return [];
}
/**
* Receive a list of EAV attributes using provided metadata service.
*
* Can be used in child classes, which represent EAV entities.
*
* @param \Magento\Framework\Api\MetadataServiceInterface $metadataService
* @return string[]
*/
protected function getEavAttributesCodes(\Magento\Framework\Api\MetadataServiceInterface $metadataService)
{
$attributeCodes = [];
$customAttributesMetadata = $metadataService->getCustomAttributesMetadata(get_class($this));
if (is_array($customAttributesMetadata)) {
/** @var $attribute \Magento\Framework\Api\MetadataObjectInterface */
foreach ($customAttributesMetadata as $attribute) {
$attributeCodes[] = $attribute->getAttributeCode();
}
}
return $attributeCodes;
}
/**
* Identifier setter
*
* @param mixed $value
* @return $this
*/
public function setId($value)
{
parent::setId($value);
return $this->setData('id', $value);
}
/**
* Set an extension attributes object.
*
* @param \Magento\Framework\Api\ExtensionAttributesInterface $extensionAttributes
* @return $this
*/
protected function _setExtensionAttributes(\Magento\Framework\Api\ExtensionAttributesInterface $extensionAttributes)
{
$this->_data[self::EXTENSION_ATTRIBUTES_KEY] = $extensionAttributes;
return $this;
}
/**
* Retrieve existing extension attributes object or create a new one.
*
* @return \Magento\Framework\Api\ExtensionAttributesInterface
*/
protected function _getExtensionAttributes()
{
if (!$this->getData(self::EXTENSION_ATTRIBUTES_KEY)) {
$this->populateExtensionAttributes([]);
}
return $this->getData(self::EXTENSION_ATTRIBUTES_KEY);
}
/**
* Instantiate extension attributes object and populate it with the provided data.
*
* @param array $extensionAttributesData
* @return void
*/
private function populateExtensionAttributes(array $extensionAttributesData = [])
{
$extensionAttributes = $this->extensionAttributesFactory->create(get_class($this), $extensionAttributesData);
$this->_setExtensionAttributes($extensionAttributes);
}
/**
* @inheritdoc
*/
public function __sleep()
{
return array_diff(parent::__sleep(), ['extensionAttributesFactory', 'customAttributeFactory']);
}
/**
* @inheritdoc
*/
public function __wakeup()
{
parent::__wakeup();
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$this->extensionAttributesFactory = $objectManager->get(ExtensionAttributesFactory::class);
$this->customAttributeFactory = $objectManager->get(AttributeValueFactory::class);
}
}