Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CustomAttributesProcessor.php
Go to the documentation of this file.
1 <?php
8 
13 
18 {
22  private $dataObjectProcessor;
23 
27  private $attributeTypeResolver;
28 
33  public function __construct(
34  DataObjectProcessor $dataObjectProcessor,
35  AttributeTypeResolverInterface $typeResolver
36  ) {
37  $this->dataObjectProcessor = $dataObjectProcessor;
38  $this->attributeTypeResolver = $typeResolver;
39  }
40 
48  public function buildOutputDataArray(CustomAttributesDataInterface $objectWithCustomAttributes, $dataObjectType)
49  {
50  $customAttributes = $objectWithCustomAttributes->getCustomAttributes();
51  $result = [];
52  foreach ($customAttributes as $customAttribute) {
53  $result[] = $this->convertCustomAttribute($customAttribute, $dataObjectType);
54  }
55  return $result;
56  }
57 
65  private function convertCustomAttribute(AttributeInterface $customAttribute, $dataObjectType)
66  {
67  $data = [];
69  $value = $customAttribute->getValue();
70  if (is_object($value)) {
71  $type = $this->attributeTypeResolver->resolveObjectType(
72  $customAttribute->getAttributeCode(),
73  $value,
74  $dataObjectType
75  );
76  $value = $this->dataObjectProcessor->buildOutputDataArray($value, $type);
77  } elseif (is_array($value)) {
78  $valueResult = [];
79  foreach ($value as $singleValue) {
80  if (is_object($singleValue)) {
81  $type = $this->attributeTypeResolver->resolveObjectType(
82  $customAttribute->getAttributeCode(),
83  $singleValue,
84  $dataObjectType
85  );
86  $singleValue = $this->dataObjectProcessor->buildOutputDataArray($singleValue, $type);
87  }
88  // Cannot cast to a type because the type is unknown
89  $valueResult[] = $singleValue;
90  }
91  $value = $valueResult;
92  }
94  return $data;
95  }
96 }
buildOutputDataArray(CustomAttributesDataInterface $objectWithCustomAttributes, $dataObjectType)
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$type
Definition: item.phtml:13
$value
Definition: gender.phtml:16
__construct(DataObjectProcessor $dataObjectProcessor, AttributeTypeResolverInterface $typeResolver)