/** * @param \Magento\Sales\Model\Quote\Address $address * @return \Magento\Checkout\Service\V1\Data\Cart\Address */ public function convertModelToDataObject(\Magento\Sales\Model\Quote\Address $address) { $data = [Address::KEY_COUNTRY_ID => $address->getCountryId(), Address::KEY_ID => $address->getId(), Address::KEY_CUSTOMER_ID => $address->getCustomerId(), Address::KEY_REGION => array(Region::REGION => $address->getRegion(), Region::REGION_ID => $address->getRegionId(), Region::REGION_CODE => $address->getRegionCode()), Address::KEY_STREET => $address->getStreet(), Address::KEY_COMPANY => $address->getCompany(), Address::KEY_TELEPHONE => $address->getTelephone(), Address::KEY_FAX => $address->getFax(), Address::KEY_POSTCODE => $address->getPostcode(), Address::KEY_CITY => $address->getCity(), Address::KEY_FIRSTNAME => $address->getFirstname(), Address::KEY_LASTNAME => $address->getLastname(), Address::KEY_MIDDLENAME => $address->getMiddlename(), Address::KEY_PREFIX => $address->getPrefix(), Address::KEY_SUFFIX => $address->getSuffix(), Address::KEY_EMAIL => $address->getEmail(), Address::KEY_VAT_ID => $address->getVatId()]; foreach ($this->metadataService->getCustomAttributesMetadata() as $attributeMetadata) { $attributeCode = $attributeMetadata->getAttributeCode(); $method = 'get' . SimpleDataObjectConverter::snakeCaseToUpperCamelCase($attributeCode); $data[Address::CUSTOM_ATTRIBUTES_KEY][] = [AttributeValue::ATTRIBUTE_CODE => $attributeCode, AttributeValue::VALUE => $address->{$method}()]; } return $this->addressBuilder->populateWithArray($data)->create(); }
/** * 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 */ $possibleMethods = array('get' . \Magento\Framework\Service\SimpleDataObjectConverter::snakeCaseToUpperCamelCase($key), 'is' . \Magento\Framework\Service\SimpleDataObjectConverter::snakeCaseToUpperCamelCase($key)); if (array_intersect($possibleMethods, $dataObjectMethods)) { $this->_data[$key] = $value; } } return $this; }
/** * {@inheritdoc} */ public function getCustomAttributesMetadata($dataObjectClassName = self::DATA_OBJECT_CLASS_NAME) { $customAttributes = []; if (!$this->customerDataObjectMethods) { $this->customerDataObjectMethods = array_flip(get_class_methods($dataObjectClassName)); } 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 array_merge($customAttributes, $this->metadataConfig->getCustomAttributesMetadata($dataObjectClassName)); }
/** * 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\Service\SimpleDataObjectConverter::snakeCaseToUpperCamelCase($key); $possibleMethods = array('get' . $camelCaseKey, 'is' . $camelCaseKey); if ($key == AbstractExtensibleObject::CUSTOM_ATTRIBUTES_KEY && is_array($data[$key]) && !empty($data[$key])) { foreach ($data[$key] as $customAttribute) { $this->setCustomAttribute($customAttribute[AttributeValue::ATTRIBUTE_CODE], $customAttribute[AttributeValue::VALUE]); } } elseif (array_intersect($possibleMethods, $dataObjectMethods)) { $this->_data[$key] = $value; } else { $this->setCustomAttribute($key, $value); } } return $this; }
/** * 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; }
/** * {@inheritdoc} */ public function getCustomAttributesMetadata($dataObjectClassName = self::DATA_OBJECT_CLASS_NAME) { $customAttributes = []; if (!$this->addressDataObjectMethods) { $this->addressDataObjectMethods = array_flip(get_class_methods($dataObjectClassName)); } 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 array_merge($customAttributes, $this->metadataConfig->getCustomAttributesMetadata($dataObjectClassName)); }
/** * @param string $field * @param StockItem $dataObject * @return mixed * @throws \BadMethodCallException */ public function getDoFieldData($field, StockItem $dataObject) { $possibleMethods = array('get' . \Magento\Framework\Service\SimpleDataObjectConverter::snakeCaseToUpperCamelCase($field), 'is' . \Magento\Framework\Service\SimpleDataObjectConverter::snakeCaseToUpperCamelCase($field)); foreach ($possibleMethods as $method) { if (method_exists($dataObject, $method)) { return $dataObject->{$method}(); } } throw new \BadMethodCallException(__('Field "%1" was not found in DO "%2".', $field, get_class($dataObject))); }