1

Magento has 2 classes

\Magento\Framework\Api\ExtensibleDataObjectConverter

and

\Magento\Framework\Api\SimpleDataObjectConverter

What are the differences and scenario for using them?

1

\Magento\Framework\Api\ExtensibleDataObjectConverter is the converter used to convert classes that extends from \Magento\Framework\Api\AbstractExtensibleObject

For deep understand you can check the method toNestedArray() that is implemented in \Magento\Framework\Api\ExtensibleDataObjectConverter and not in \Magento\Framework\Api\SimpleDataObjectConverter

public function toNestedArray(
        ExtensibleDataInterface $dataObject,
        $skipAttributes = [],
        $dataObjectType = null
    ) {
        if ($dataObjectType == null) {
            $dataObjectType = get_class($dataObject);
        }
        $dataObjectArray = $this->dataObjectProcessor->buildOutputDataArray($dataObject, $dataObjectType);
        //process custom attributes if present
        if (!empty($dataObjectArray[AbstractExtensibleObject::CUSTOM_ATTRIBUTES_KEY])) {
            /** @var AttributeValue[] $customAttributes */
            $customAttributes = $dataObjectArray[AbstractExtensibleObject::CUSTOM_ATTRIBUTES_KEY];
            unset($dataObjectArray[AbstractExtensibleObject::CUSTOM_ATTRIBUTES_KEY]);
            foreach ($customAttributes as $attributeValue) {
                if (!in_array($attributeValue[AttributeValue::ATTRIBUTE_CODE], $skipAttributes)) {
                    $dataObjectArray[$attributeValue[AttributeValue::ATTRIBUTE_CODE]]
                        = $attributeValue[AttributeValue::VALUE];
                }
            }
        }
        if (!empty($dataObjectArray[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY])) {
            /** @var array $extensionAttributes */
            $extensionAttributes = $dataObjectArray[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY];
            unset($dataObjectArray[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]);
            foreach ($extensionAttributes as $attributeKey => $attributeValue) {
                if (!in_array($attributeKey, $skipAttributes)) {
                    $dataObjectArray[$attributeKey] = $attributeValue;
                }
            }
        }
        return $dataObjectArray;
    }

It gonna convert extras informations into the array (extension_attributes or custom_attributes - check devdocs for deeply explanation)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.