Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractExtensibleModel.php
Go to the documentation of this file.
1 <?php
8 
11 
19 abstract class AbstractExtensibleModel extends AbstractModel implements
21 {
26 
31 
36 
40  protected $customAttributesCodes = null;
41 
45  protected $customAttributesChanged = false;
46 
56  public function __construct(
57  \Magento\Framework\Model\Context $context,
58  \Magento\Framework\Registry $registry,
59  ExtensionAttributesFactory $extensionFactory,
61  \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
62  \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
63  array $data = []
64  ) {
65  $this->extensionAttributesFactory = $extensionFactory;
66  $this->customAttributeFactory = $customAttributeFactory;
68  parent::__construct($context, $registry, $resource, $resourceCollection, $data);
69  if (isset($data['id'])) {
70  $this->setId($data['id']);
71  }
72  if (isset($data[self::EXTENSION_ATTRIBUTES_KEY]) && is_array($data[self::EXTENSION_ATTRIBUTES_KEY])) {
73  $this->populateExtensionAttributes($data[self::EXTENSION_ATTRIBUTES_KEY]);
74  }
75  }
76 
83  protected function filterCustomAttributes($data)
84  {
85  if (empty($data[self::CUSTOM_ATTRIBUTES])) {
86  return $data;
87  }
89  $data[self::CUSTOM_ATTRIBUTES] = array_intersect_key(
90  (array)$data[self::CUSTOM_ATTRIBUTES],
91  array_flip($customAttributesCodes)
92  );
93  foreach ($data[self::CUSTOM_ATTRIBUTES] as $code => $value) {
94  if (!($value instanceof \Magento\Framework\Api\AttributeInterface)) {
95  $data[self::CUSTOM_ATTRIBUTES][$code] = $this->customAttributeFactory->create()
96  ->setAttributeCode($code)
97  ->setValue($value);
98  }
99  }
100  return $data;
101  }
102 
108  protected function initializeCustomAttributes()
109  {
110  if (!isset($this->_data[self::CUSTOM_ATTRIBUTES]) || $this->customAttributesChanged) {
111  if (!empty($this->_data[self::CUSTOM_ATTRIBUTES])) {
112  $customAttributes = $this->_data[self::CUSTOM_ATTRIBUTES];
113  } else {
114  $customAttributes = [];
115  }
116  $customAttributeCodes = $this->getCustomAttributesCodes();
117 
118  foreach ($customAttributeCodes as $customAttributeCode) {
119  if (isset($this->_data[self::CUSTOM_ATTRIBUTES][$customAttributeCode])) {
120  $customAttribute = $this->customAttributeFactory->create()
121  ->setAttributeCode($customAttributeCode)
122  ->setValue($this->_data[self::CUSTOM_ATTRIBUTES][$customAttributeCode]->getValue());
123  $customAttributes[$customAttributeCode] = $customAttribute;
124  } elseif (isset($this->_data[$customAttributeCode])) {
125  $customAttribute = $this->customAttributeFactory->create()
126  ->setAttributeCode($customAttributeCode)
127  ->setValue($this->_data[$customAttributeCode]);
128  $customAttributes[$customAttributeCode] = $customAttribute;
129  }
130  }
131  $this->_data[self::CUSTOM_ATTRIBUTES] = $customAttributes;
132  $this->customAttributesChanged = false;
133  }
134  }
135 
141  public function getCustomAttributes()
142  {
144  // Returning as a sequential array (instead of stored associative array) to be compatible with the interface
145  return array_values($this->_data[self::CUSTOM_ATTRIBUTES]);
146  }
147 
155  {
157  return $this->_data[self::CUSTOM_ATTRIBUTES][$attributeCode] ?? null;
158  }
159 
163  public function setCustomAttributes(array $attributes)
164  {
165  return $this->setData(self::CUSTOM_ATTRIBUTES, $attributes);
166  }
167 
171  public function setCustomAttribute($attributeCode, $attributeValue)
172  {
174  /* If key corresponds to custom attribute code, populate custom attributes */
175  if (in_array($attributeCode, $customAttributesCodes)) {
176  $attribute = $this->customAttributeFactory->create();
177  $attribute->setAttributeCode($attributeCode)
178  ->setValue($attributeValue);
180  }
181  return $this;
182  }
183 
189  public function setData($key, $value = null)
190  {
191  if (is_array($key)) {
192  $key = $this->filterCustomAttributes($key);
193  } elseif ($key == self::CUSTOM_ATTRIBUTES) {
194  $filteredData = $this->filterCustomAttributes([self::CUSTOM_ATTRIBUTES => $value]);
195  $value = $filteredData[self::CUSTOM_ATTRIBUTES];
196  }
197  $this->customAttributesChanged = true;
198  parent::setData($key, $value);
199  return $this;
200  }
201 
207  public function unsetData($key = null)
208  {
209  if (is_string($key) && isset($this->_data[self::CUSTOM_ATTRIBUTES][$key])) {
210  unset($this->_data[self::CUSTOM_ATTRIBUTES][$key]);
211  }
212  return parent::unsetData($key);
213  }
214 
221  protected function convertCustomAttributeValues(array &$customAttributes)
222  {
223  foreach ($customAttributes as $attributeCode => $attributeValue) {
224  if ($attributeValue instanceof \Magento\Framework\Api\AttributeValue) {
225  $customAttributes[$attributeCode] = $attributeValue->getValue();
226  }
227  }
228  }
229 
247  public function getData($key = '', $index = null)
248  {
249  if ($key === self::CUSTOM_ATTRIBUTES) {
250  throw new \LogicException("Custom attributes array should be retrieved via getCustomAttributes() only.");
251  } elseif ($key === '') {
253  $customAttributes = isset($this->_data[self::CUSTOM_ATTRIBUTES])
254  ? $this->_data[self::CUSTOM_ATTRIBUTES]
255  : [];
256  $this->convertCustomAttributeValues($customAttributes);
257  $data = array_merge($this->_data, $customAttributes);
258  unset($data[self::CUSTOM_ATTRIBUTES]);
259  } else {
260  $data = parent::getData($key, $index);
261  if ($data === null) {
263  $data = isset($this->_data[self::CUSTOM_ATTRIBUTES][$key])
264  ? $this->_data[self::CUSTOM_ATTRIBUTES][$key]
265  : null;
266  if ($data instanceof \Magento\Framework\Api\AttributeValue) {
267  $data = $data->getValue();
268  }
269  if (null !== $index && isset($data[$index])) {
270  return $data[$index];
271  }
272  }
273  }
274 
275  return $data;
276  }
277 
285  protected function getCustomAttributesCodes()
286  {
287  return [];
288  }
289 
298  protected function getEavAttributesCodes(\Magento\Framework\Api\MetadataServiceInterface $metadataService)
299  {
300  $attributeCodes = [];
301  $customAttributesMetadata = $metadataService->getCustomAttributesMetadata(get_class($this));
302  if (is_array($customAttributesMetadata)) {
304  foreach ($customAttributesMetadata as $attribute) {
305  $attributeCodes[] = $attribute->getAttributeCode();
306  }
307  }
308  return $attributeCodes;
309  }
310 
317  public function setId($value)
318  {
319  parent::setId($value);
320  return $this->setData('id', $value);
321  }
322 
329  protected function _setExtensionAttributes(\Magento\Framework\Api\ExtensionAttributesInterface $extensionAttributes)
330  {
332  return $this;
333  }
334 
340  protected function _getExtensionAttributes()
341  {
342  if (!$this->getData(self::EXTENSION_ATTRIBUTES_KEY)) {
343  $this->populateExtensionAttributes([]);
344  }
345  return $this->getData(self::EXTENSION_ATTRIBUTES_KEY);
346  }
347 
354  private function populateExtensionAttributes(array $extensionAttributesData = [])
355  {
356  $extensionAttributes = $this->extensionAttributesFactory->create(get_class($this), $extensionAttributesData);
358  }
359 
363  public function __sleep()
364  {
365  return array_diff(parent::__sleep(), ['extensionAttributesFactory', 'customAttributeFactory']);
366  }
367 
371  public function __wakeup()
372  {
375  $this->extensionAttributesFactory = $objectManager->get(ExtensionAttributesFactory::class);
376  $this->customAttributeFactory = $objectManager->get(AttributeValueFactory::class);
377  }
378 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$objectManager
Definition: bootstrap.php:17
_setExtensionAttributes(\Magento\Framework\Api\ExtensionAttributesInterface $extensionAttributes)
$resource
Definition: bulk.php:12
$value
Definition: gender.phtml:16
$attributeCode
Definition: extend.phtml:12
$attributes
Definition: matrix.phtml:13
__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=[])
$index
Definition: list.phtml:44
$code
Definition: info.phtml:12