Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractExtensibleObject.php
Go to the documentation of this file.
1 <?php
7 
8 use \Magento\Framework\Api\AttributeValueFactory;
9 
18 {
22  const CUSTOM_ATTRIBUTES_KEY = 'custom_attributes';
23 
27  protected $extensionFactory;
28 
33 
38 
46  public function __construct(
49  $data = []
50  ) {
51  $this->extensionFactory = $extensionFactory;
52  $this->attributeValueFactory = $attributeValueFactory;
53  parent::__construct($data);
54  if (isset($data[self::EXTENSION_ATTRIBUTES_KEY]) && is_array($data[self::EXTENSION_ATTRIBUTES_KEY])) {
55  $this->populateExtensionAttributes($data[self::EXTENSION_ATTRIBUTES_KEY]);
56  }
57  }
58 
66  {
67  return isset($this->_data[self::CUSTOM_ATTRIBUTES])
68  && isset($this->_data[self::CUSTOM_ATTRIBUTES][$attributeCode])
70  : null;
71  }
72 
78  public function getCustomAttributes()
79  {
80  return $this->_data[self::CUSTOM_ATTRIBUTES] ?? [];
81  }
82 
90  public function setCustomAttributes(array $attributes)
91  {
93  foreach ($attributes as $attribute) {
94  if (!$attribute instanceof AttributeValue) {
95  throw new \LogicException('Custom Attribute array elements can only be type of AttributeValue');
96  }
97  $attributeCode = $attribute->getAttributeCode();
98  if (in_array($attributeCode, $customAttributesCodes)) {
100  }
101  }
102  return $this;
103  }
104 
112  public function setCustomAttribute($attributeCode, $attributeValue)
113  {
115  /* If key corresponds to custom attribute code, populate custom attributes */
116  if (in_array($attributeCode, $customAttributesCodes)) {
118  $attribute = $this->attributeValueFactory->create();
119  $attribute->setAttributeCode($attributeCode)
120  ->setValue($attributeValue);
122  }
123  return $this;
124  }
125 
133  protected function getCustomAttributesCodes()
134  {
135  return $this->customAttributesCodes ?? [];
136  }
137 
146  protected function getEavAttributesCodes(\Magento\Framework\Api\MetadataServiceInterface $metadataService)
147  {
148  $attributeCodes = [];
149  $customAttributesMetadata = $metadataService->getCustomAttributesMetadata(get_class($this));
150  if (is_array($customAttributesMetadata)) {
152  foreach ($customAttributesMetadata as $attribute) {
153  $attributeCodes[] = $attribute->getAttributeCode();
154  }
155  }
156  return $attributeCodes;
157  }
158 
164  protected function _getExtensionAttributes()
165  {
166  if (!$this->_get(self::EXTENSION_ATTRIBUTES_KEY)) {
167  $this->populateExtensionAttributes([]);
168  }
169  return $this->_get(self::EXTENSION_ATTRIBUTES_KEY);
170  }
171 
178  private function populateExtensionAttributes(array $extensionAttributesData = [])
179  {
180  $extensionAttributes = $this->extensionFactory->create(get_class($this), $extensionAttributesData);
182  }
183 
191  {
193  return $this;
194  }
195 }
_setExtensionAttributes(\Magento\Framework\Api\ExtensionAttributesInterface $extensionAttributes)
setCustomAttribute($attributeCode, $attributeValue)
__construct(\Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory, AttributeValueFactory $attributeValueFactory, $data=[])
$attributeCode
Definition: extend.phtml:12
$extensionAttributes
Definition: payment.php:22
$attributes
Definition: matrix.phtml:13