/** * Update Data Object with the data from array * * @param mixed $dataObject * @param array $data * @param string $interfaceName * @return $this * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ protected function _setDataValues($dataObject, array $data, $interfaceName) { $dataObjectMethods = get_class_methods(get_class($dataObject)); foreach ($data as $key => $value) { /* First, verify is there any setter for the key on the Service Data Object */ $camelCaseKey = \Magento\Framework\Api\SimpleDataObjectConverter::snakeCaseToUpperCamelCase($key); $possibleMethods = ['set' . $camelCaseKey, 'setIs' . $camelCaseKey]; if ($key === CustomAttributesDataInterface::CUSTOM_ATTRIBUTES && $dataObject instanceof ExtensibleDataInterface && is_array($data[$key]) && !empty($data[$key])) { foreach ($data[$key] as $customAttribute) { $dataObject->setCustomAttribute($customAttribute[AttributeInterface::ATTRIBUTE_CODE], $customAttribute[AttributeInterface::VALUE]); } } elseif ($methodNames = array_intersect($possibleMethods, $dataObjectMethods)) { $methodName = array_values($methodNames)[0]; if (!is_array($value)) { if ($methodName === 'setExtensionAttributes' && $value === null) { // Cannot pass a null value to a method with a typed parameter } else { $dataObject->{$methodName}($value); } } else { $getterMethodName = 'get' . $camelCaseKey; $this->setComplexValue($dataObject, $getterMethodName, $methodName, $value, $interfaceName); } } else { if ($dataObject instanceof ExtensibleDataInterface) { $dataObject->setCustomAttribute($key, $value); } } } return $this; }
/** * Initializes Data Object with the data from array * * @param array $data * @return $this */ protected function _setDataValues(array $data) { $dataObjectMethods = get_class_methods($this->_getDataObjectType()); foreach ($data as $key => $value) { /* First, verify is there any getter for the key on the Service Data Object */ $camelCaseKey = \Magento\Framework\Api\SimpleDataObjectConverter::snakeCaseToUpperCamelCase($key); $possibleMethods = ['get' . $camelCaseKey, 'is' . $camelCaseKey]; if ($key === ExtensibleDataInterface::CUSTOM_ATTRIBUTES && is_array($data[$key]) && !empty($data[$key])) { foreach ($data[$key] as $customAttribute) { $this->setCustomAttribute($customAttribute[AttributeInterface::ATTRIBUTE_CODE], $customAttribute[AttributeInterface::VALUE]); } } elseif ($methodName = array_intersect($possibleMethods, $dataObjectMethods)) { if (!is_array($value)) { $this->data[$key] = $value; } else { $this->setComplexValue($methodName[0], $key, $value); } } elseif (in_array($key, $this->getCustomAttributesCodes())) { $this->setCustomAttribute($key, $value); } } return $this; }
public function snakeCaseToUpperCamelCase($data) { $result = \Magento\Framework\Api\SimpleDataObjectConverter::snakeCaseToUpperCamelCase($data); return $result; }
/** * {@inheritdoc} */ public function getCustomAttributesMetadata($dataObjectClassName = self::DATA_INTERFACE_NAME) { $customAttributes = []; if (!$this->customerDataObjectMethods) { $dataObjectMethods = array_flip(get_class_methods($dataObjectClassName)); $baseClassDataObjectMethods = array_flip(get_class_methods('Magento\\Framework\\Api\\AbstractExtensibleObject')); $this->customerDataObjectMethods = array_diff_key($dataObjectMethods, $baseClassDataObjectMethods); } foreach ($this->getAllAttributesMetadata() as $attributeMetadata) { $attributeCode = $attributeMetadata->getAttributeCode(); $camelCaseKey = SimpleDataObjectConverter::snakeCaseToUpperCamelCase($attributeCode); $isDataObjectMethod = isset($this->customerDataObjectMethods['get' . $camelCaseKey]) || isset($this->customerDataObjectMethods['is' . $camelCaseKey]); /** Even though disable_auto_group_change is system attribute, it should be available to the clients */ if (!$isDataObjectMethod && (!$attributeMetadata->isSystem() || $attributeCode == 'disable_auto_group_change')) { $customAttributes[] = $attributeMetadata; } } return $customAttributes; }
/** * {@inheritdoc} */ public function getCustomAttributesMetadata($dataObjectClassName = AddressMetadataInterface::DATA_INTERFACE_NAME) { if (!$this->addressDataObjectMethods) { $dataObjectMethods = array_flip(get_class_methods($dataObjectClassName)); $baseClassDataObjectMethods = array_flip(get_class_methods('Magento\\Framework\\Api\\AbstractExtensibleObject')); $this->addressDataObjectMethods = array_diff_key($dataObjectMethods, $baseClassDataObjectMethods); } $customAttributes = []; foreach ($this->getAllAttributesMetadata() as $attributeMetadata) { $attributeCode = $attributeMetadata->getAttributeCode(); $camelCaseKey = SimpleDataObjectConverter::snakeCaseToUpperCamelCase($attributeCode); $isDataObjectMethod = isset($this->addressDataObjectMethods['get' . $camelCaseKey]) || isset($this->addressDataObjectMethods['is' . $camelCaseKey]); if (!$isDataObjectMethod && !$attributeMetadata->isSystem()) { $customAttributes[] = $attributeMetadata; } } return $customAttributes; }
private function snakeToUCamel($input) { return \Magento\Framework\Api\SimpleDataObjectConverter::snakeCaseToUpperCamelCase($input); }
/** * Convert custom attribute data array to array of AttributeValue Data Object * * @param array $customAttributesValueArray * @param string $dataObjectClassName * @return AttributeValue[] */ protected function convertCustomAttributeValue($customAttributesValueArray, $dataObjectClassName) { $result = []; $dataObjectClassName = ltrim($dataObjectClassName, '\\'); $camelCaseAttributeCodeKey = lcfirst(SimpleDataObjectConverter::snakeCaseToUpperCamelCase(AttributeValue::ATTRIBUTE_CODE)); foreach ($customAttributesValueArray as $key => $customAttribute) { if (!is_array($customAttribute)) { $customAttribute = [AttributeValue::ATTRIBUTE_CODE => $key, AttributeValue::VALUE => $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 string $type = $this->customAttributeTypeLocator->getType($customAttributeCode, $dataObjectClassName); $type = $type ? $type : 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) { $attributeValue = $customAttributeValue; } else { $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[$customAttributeCode] = $this->attributeValueFactory->create()->setAttributeCode($customAttributeCode)->setValue($attributeValue); } return $result; }
/** * Map criteria to Select Query Object * * @param CriteriaInterface $criteria * @return Select */ public function map(CriteriaInterface $criteria) { $criteriaParts = $criteria->toArray(); foreach ($criteriaParts as $key => $value) { $camelCaseKey = \Magento\Framework\Api\SimpleDataObjectConverter::snakeCaseToUpperCamelCase($key); $mapperMethod = 'map' . $camelCaseKey; if (method_exists($this, $mapperMethod)) { if (!is_array($value)) { $value = [$value]; } call_user_func_array([$this, $mapperMethod], $value); } } return $this->select; }
/** * @param string $field * @return string|null */ public function getConfigFieldValue($field) { $stockItem = $this->getStockItem(); if ($stockItem->getItemId()) { $method = 'getUseConfig' . SimpleDataObjectConverter::snakeCaseToUpperCamelCase($field); if (method_exists($stockItem, $method)) { return $stockItem->{$method}(); } } return $this->stockConfiguration->getDefaultConfigValue($field); }
/** * @param mixed $dataObject * @param string $getterMethodName * @param string $methodName * @param array $value * @param string $interfaceName * @return $this * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ protected function setComplexValue($dataObject, $getterMethodName, $methodName, array $value, $interfaceName) { if ($interfaceName == null) { $interfaceName = get_class($dataObject); } $returnType = $this->methodsMapProcessor->getMethodReturnType($interfaceName, $getterMethodName); if ($this->typeProcessor->isTypeSimple($returnType)) { $dataObject->{$methodName}($value); return $this; } if ($this->typeProcessor->isArrayType($returnType)) { $type = $this->typeProcessor->getArrayItemType($returnType); $objects = []; foreach ($value as $arrayElementData) { $object = $this->objectFactory->create($type, []); $this->populateWithArray($object, $arrayElementData, $type); $objects[] = $object; } $dataObject->{$methodName}($objects); return $this; } if (is_subclass_of($returnType, '\\Magento\\Framework\\Api\\ExtensibleDataInterface')) { $object = $this->objectFactory->create($returnType, []); $this->populateWithArray($object, $value, $returnType); } else { if (is_subclass_of($returnType, '\\Magento\\Framework\\Api\\ExtensionAttributesInterface')) { foreach ($value as $extensionAttributeKey => $extensionAttributeValue) { $extensionAttributeGetterMethodName = 'get' . \Magento\Framework\Api\SimpleDataObjectConverter::snakeCaseToUpperCamelCase($extensionAttributeKey); $methodReturnType = $this->methodsMapProcessor->getMethodReturnType($returnType, $extensionAttributeGetterMethodName); $extensionAttributeType = $this->typeProcessor->isArrayType($methodReturnType) ? $this->typeProcessor->getArrayItemType($methodReturnType) : $methodReturnType; if ($this->typeProcessor->isTypeSimple($extensionAttributeType)) { $value[$extensionAttributeKey] = $extensionAttributeValue; } else { if ($this->typeProcessor->isArrayType($methodReturnType)) { foreach ($extensionAttributeValue as $key => $extensionAttributeArrayValue) { $extensionAttribute = $this->objectFactory->create($extensionAttributeType, []); $this->populateWithArray($extensionAttribute, $extensionAttributeArrayValue, $extensionAttributeType); $value[$extensionAttributeKey][$key] = $extensionAttribute; } } else { $value[$extensionAttributeKey] = $this->objectFactory->create($extensionAttributeType, ['data' => $extensionAttributeValue]); } } } $object = $this->extensionFactory->create(get_class($dataObject), ['data' => $value]); } else { $object = $this->objectFactory->create($returnType, $value); } } $dataObject->{$methodName}($object); return $this; }
/** * Derive the custom attribute code and value. * * @param string[] $customAttribute * @return string[] */ private function processCustomAttribute($customAttribute) { $camelCaseAttributeCodeKey = lcfirst(SimpleDataObjectConverter::snakeCaseToUpperCamelCase(AttributeValue::ATTRIBUTE_CODE)); // attribute code key could be snake or camel case, depending on whether SOAP or REST is used. if (isset($customAttribute[AttributeValue::ATTRIBUTE_CODE])) { $customAttributeCode = $customAttribute[AttributeValue::ATTRIBUTE_CODE]; } elseif (isset($customAttribute[$camelCaseAttributeCodeKey])) { $customAttributeCode = $customAttribute[$camelCaseAttributeCodeKey]; } else { $customAttributeCode = null; } if (!$customAttributeCode && !isset($customAttribute[AttributeValue::VALUE])) { throw new SerializationException(new Phrase('There is an empty custom attribute specified.')); } else { if (!$customAttributeCode) { throw new SerializationException(new Phrase('A custom attribute is specified without an attribute code.')); } else { if (!isset($customAttribute[AttributeValue::VALUE])) { throw new SerializationException(new Phrase('Value is not set for attribute code "' . $customAttributeCode . '"')); } } } return [$customAttributeCode, $customAttribute[AttributeValue::VALUE]]; }
/** * 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; }