CustomAttributesProcessor.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Reflection;
  7. use Magento\Framework\Api\AttributeInterface;
  8. use Magento\Framework\Api\AttributeValue;
  9. use Magento\Framework\Api\CustomAttributesDataInterface;
  10. use Magento\Framework\Api\AttributeTypeResolverInterface;
  11. /**
  12. * Processes custom attributes and produces an array for the data.
  13. */
  14. class CustomAttributesProcessor
  15. {
  16. /**
  17. * @var DataObjectProcessor
  18. */
  19. private $dataObjectProcessor;
  20. /**
  21. * @var AttributeTypeResolverInterface
  22. */
  23. private $attributeTypeResolver;
  24. /**
  25. * @param DataObjectProcessor $dataObjectProcessor
  26. * @param AttributeTypeResolverInterface $typeResolver
  27. */
  28. public function __construct(
  29. DataObjectProcessor $dataObjectProcessor,
  30. AttributeTypeResolverInterface $typeResolver
  31. ) {
  32. $this->dataObjectProcessor = $dataObjectProcessor;
  33. $this->attributeTypeResolver = $typeResolver;
  34. }
  35. /**
  36. * Writes out the custom attributes for a given object into an array.
  37. *
  38. * @param CustomAttributesDataInterface $objectWithCustomAttributes
  39. * @param string $dataObjectType
  40. * @return array
  41. */
  42. public function buildOutputDataArray(CustomAttributesDataInterface $objectWithCustomAttributes, $dataObjectType)
  43. {
  44. $customAttributes = $objectWithCustomAttributes->getCustomAttributes();
  45. $result = [];
  46. foreach ($customAttributes as $customAttribute) {
  47. $result[] = $this->convertCustomAttribute($customAttribute, $dataObjectType);
  48. }
  49. return $result;
  50. }
  51. /**
  52. * Convert custom_attribute object to use flat array structure
  53. *
  54. * @param AttributeInterface $customAttribute
  55. * @param string $dataObjectType
  56. * @return array
  57. */
  58. private function convertCustomAttribute(AttributeInterface $customAttribute, $dataObjectType)
  59. {
  60. $data = [];
  61. $data[AttributeValue::ATTRIBUTE_CODE] = $customAttribute->getAttributeCode();
  62. $value = $customAttribute->getValue();
  63. if (is_object($value)) {
  64. $type = $this->attributeTypeResolver->resolveObjectType(
  65. $customAttribute->getAttributeCode(),
  66. $value,
  67. $dataObjectType
  68. );
  69. $value = $this->dataObjectProcessor->buildOutputDataArray($value, $type);
  70. } elseif (is_array($value)) {
  71. $valueResult = [];
  72. foreach ($value as $singleValue) {
  73. if (is_object($singleValue)) {
  74. $type = $this->attributeTypeResolver->resolveObjectType(
  75. $customAttribute->getAttributeCode(),
  76. $singleValue,
  77. $dataObjectType
  78. );
  79. $singleValue = $this->dataObjectProcessor->buildOutputDataArray($singleValue, $type);
  80. }
  81. // Cannot cast to a type because the type is unknown
  82. $valueResult[] = $singleValue;
  83. }
  84. $value = $valueResult;
  85. }
  86. $data[AttributeValue::VALUE] = $value;
  87. return $data;
  88. }
  89. }