/** * {@inheritdoc} */ public function getCustomAttributesMetadata($dataObjectClassName = null) { $attributes = []; if (!is_null($this->attributeMetadataBuilder) && !is_null($dataObjectClassName)) { /** * Attribute metadata builder and data object class name are expected to be configured * via DI using virtual types. If configuration is missing, empty array should be returned. */ $allAttributes = $this->serviceConfigReader->read(); if (isset($allAttributes[$dataObjectClassName]) && is_array($allAttributes[$dataObjectClassName])) { $attributeCodes = array_keys($allAttributes[$dataObjectClassName]); foreach ($attributeCodes as $attributeCode) { $this->attributeMetadataBuilder->setAttributeCode($attributeCode); $attributes[$attributeCode] = $this->attributeMetadataBuilder->create(); } } } return $attributes; }
/** * 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]; } else { if (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; } //If custom attribute value is an array then its a data object type $attributeValue = $this->_createFromArray($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; }