/** * Convert custom attribute data array to array of AttributeValue Data Object * * @param array $customAttributesValueArray * @param string $returnType * @param string $dataObjectClassName * @return AttributeValue[] */ protected function convertCustomAttributeValue($customAttributesValueArray, $returnType, $dataObjectClassName) { $result = []; $allAttributes = $this->serviceConfigReader->read(); $dataObjectClassName = ltrim($dataObjectClassName, '\\'); if (!isset($allAttributes[$dataObjectClassName])) { return $this->_convertValue($customAttributesValueArray, $returnType); } $dataObjectAttributes = $allAttributes[$dataObjectClassName]; $camelCaseAttributeCodeKey = lcfirst(SimpleDataObjectConverter::snakeCaseToUpperCamelCase(AttributeValue::ATTRIBUTE_CODE)); foreach ($customAttributesValueArray as $customAttribute) { if (isset($customAttribute[AttributeValue::ATTRIBUTE_CODE])) { $customAttributeCode = $customAttribute[AttributeValue::ATTRIBUTE_CODE]; } elseif (isset($customAttribute[$camelCaseAttributeCodeKey])) { $customAttributeCode = $customAttribute[$camelCaseAttributeCodeKey]; } else { $customAttributeCode = null; } //Check if type is defined, else default to mixed $type = isset($dataObjectAttributes[$customAttributeCode]) ? $dataObjectAttributes[$customAttributeCode] : TypeProcessor::ANY_TYPE; $customAttributeValue = $customAttribute[AttributeValue::VALUE]; if (is_array($customAttributeValue)) { //If type for AttributeValue's value as array is mixed, further processing is not possible if ($type === TypeProcessor::ANY_TYPE) { continue; } $attributeValue = $this->_createDataObjectForTypeAndArrayValue($type, $customAttributeValue); } else { $attributeValue = $this->_convertValue($customAttributeValue, $type); } //Populate the attribute value data object once the value for custom attribute is derived based on type $result[] = $this->attributeValueBuilder->setAttributeCode($customAttributeCode)->setValue($attributeValue)->create(); } return $result; }
public function testSetCustomAttributesAsLiterals() { $attributeCode = 'attribute2'; $attributeValue = 'attribute_value'; $this->attributeDataBuilderMock->expects($this->once())->method('setAttributeCode')->with($attributeCode)->willReturn($this->attributeDataBuilderMock); $this->attributeDataBuilderMock->expects($this->once())->method('setValue')->with($attributeValue)->willReturn($this->attributeDataBuilderMock); $this->attributeDataBuilderMock->expects($this->once())->method('create'); $this->model->setData(\Magento\Framework\Api\ExtensibleDataInterface::CUSTOM_ATTRIBUTES, [$attributeCode => $attributeValue]); }
/** * {@inheritdoc} */ public function setCustomAttribute($attributeCode, $attributeValue) { $customAttributesCodes = $this->getCustomAttributesCodes(); if (in_array($attributeCode, $customAttributesCodes)) { $attribute = $this->attributeValueBuilder->setAttributeCode($attributeCode)->setValue($attributeValue)->create(); //Stores as an associative array for easier lookup and processing $this->data[ExtensibleDataInterface::CUSTOM_ATTRIBUTES][$attributeCode] = $attribute; } return $this; }