Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DataObjectHelper.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Framework\Api;
8 
10 
17 {
21  protected $objectFactory;
22 
26  protected $objectProcessor;
27 
31  protected $typeProcessor;
32 
36  protected $extensionFactory;
37 
41  protected $joinProcessor;
42 
47 
56  public function __construct(
58  \Magento\Framework\Reflection\DataObjectProcessor $objectProcessor,
59  \Magento\Framework\Reflection\TypeProcessor $typeProcessor,
61  \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $joinProcessor,
63  ) {
64  $this->objectFactory = $objectFactory;
65  $this->objectProcessor = $objectProcessor;
66  $this->typeProcessor = $typeProcessor;
67  $this->extensionFactory = $extensionFactory;
68  $this->joinProcessor = $joinProcessor;
69  $this->methodsMapProcessor = $methodsMapProcessor;
70  }
71 
80  public function populateWithArray($dataObject, array $data, $interfaceName)
81  {
82  if ($dataObject instanceof ExtensibleDataInterface) {
83  $data = $this->joinProcessor->extractExtensionAttributes(get_class($dataObject), $data);
84  }
85  $this->_setDataValues($dataObject, $data, $interfaceName);
86  return $this;
87  }
88 
98  protected function _setDataValues($dataObject, array $data, $interfaceName)
99  {
100  $dataObjectMethods = get_class_methods(get_class($dataObject));
101  foreach ($data as $key => $value) {
102  /* First, verify is there any setter for the key on the Service Data Object */
104  $possibleMethods = [
105  'set' . $camelCaseKey,
106  'setIs' . $camelCaseKey,
107  ];
109  && ($dataObject instanceof ExtensibleDataInterface)
110  && is_array($data[$key])
111  && !empty($data[$key])
112  ) {
113  foreach ($data[$key] as $customAttribute) {
114  $dataObject->setCustomAttribute(
115  $customAttribute[AttributeInterface::ATTRIBUTE_CODE],
116  $customAttribute[AttributeInterface::VALUE]
117  );
118  }
119  } elseif ($methodNames = array_intersect($possibleMethods, $dataObjectMethods)) {
120  $methodName = array_values($methodNames)[0];
121  if (!is_array($value)) {
122  if ($methodName === 'setExtensionAttributes' && $value === null) {
123  // Cannot pass a null value to a method with a typed parameter
124  } else {
125  $dataObject->$methodName($value);
126  }
127  } else {
128  $getterMethodName = 'get' . $camelCaseKey;
129  $this->setComplexValue($dataObject, $getterMethodName, $methodName, $value, $interfaceName);
130  }
131  } elseif ($dataObject instanceof CustomAttributesDataInterface) {
132  $dataObject->setCustomAttribute($key, $value);
133  }
134  }
135 
136  return $this;
137  }
138 
148  protected function setComplexValue(
149  $dataObject,
150  $getterMethodName,
151  $methodName,
152  array $value,
153  $interfaceName
154  ) {
155  if ($interfaceName == null) {
156  $interfaceName = get_class($dataObject);
157  }
158  $returnType = $this->methodsMapProcessor->getMethodReturnType($interfaceName, $getterMethodName);
159  if ($this->typeProcessor->isTypeSimple($returnType)) {
160  $dataObject->$methodName($value);
161  return $this;
162  }
163 
164  if ($this->typeProcessor->isArrayType($returnType)) {
165  $type = $this->typeProcessor->getArrayItemType($returnType);
166  $objects = [];
167  foreach ($value as $arrayElementData) {
168  $object = $this->objectFactory->create($type, []);
169  $this->populateWithArray($object, $arrayElementData, $type);
170  $objects[] = $object;
171  }
172  $dataObject->$methodName($objects);
173  return $this;
174  }
175 
176  if (is_subclass_of($returnType, \Magento\Framework\Api\ExtensibleDataInterface::class)) {
177  $object = $this->objectFactory->create($returnType, []);
178  $this->populateWithArray($object, $value, $returnType);
179  } elseif (is_subclass_of($returnType, \Magento\Framework\Api\ExtensionAttributesInterface::class)) {
180  foreach ($value as $extensionAttributeKey => $extensionAttributeValue) {
181  $extensionAttributeGetterMethodName
183  $extensionAttributeKey
184  );
185  $methodReturnType = $this->methodsMapProcessor->getMethodReturnType(
186  $returnType,
187  $extensionAttributeGetterMethodName
188  );
189  $extensionAttributeType = $this->typeProcessor->isArrayType($methodReturnType)
190  ? $this->typeProcessor->getArrayItemType($methodReturnType)
191  : $methodReturnType;
192  if ($this->typeProcessor->isTypeSimple($extensionAttributeType)) {
193  $value[$extensionAttributeKey] = $extensionAttributeValue;
194  } else {
195  if ($this->typeProcessor->isArrayType($methodReturnType)) {
196  foreach ($extensionAttributeValue as $key => $extensionAttributeArrayValue) {
197  $extensionAttribute = $this->objectFactory->create($extensionAttributeType, []);
198  $this->populateWithArray(
199  $extensionAttribute,
200  $extensionAttributeArrayValue,
201  $extensionAttributeType
202  );
203  $value[$extensionAttributeKey][$key] = $extensionAttribute;
204  }
205  } else {
206  $value[$extensionAttributeKey] = $this->objectFactory->create(
207  $extensionAttributeType,
208  ['data' => $extensionAttributeValue]
209  );
210  }
211  }
212  }
213  $object = $this->extensionFactory->create(get_class($dataObject), ['data' => $value]);
214  } else {
215  $object = $this->objectFactory->create($returnType, $value);
216  }
217  $dataObject->$methodName($object);
218  return $this;
219  }
220 
230  public function mergeDataObjects(
231  $interfaceName,
232  $firstDataObject,
233  $secondDataObject
234  ) {
235  if (!$firstDataObject instanceof $interfaceName || !$secondDataObject instanceof $interfaceName) {
236  throw new \LogicException('Wrong prototype object given. It can only be of "' . $interfaceName . '" type.');
237  }
238  $secondObjectArray = $this->objectProcessor->buildOutputDataArray($secondDataObject, $interfaceName);
239  $this->_setDataValues($firstDataObject, $secondObjectArray, $interfaceName);
240  return $this;
241  }
242 
251  {
252  $attributeValueArray = [];
253  if (empty($attributeValues)) {
254  return $attributeValueArray;
255  }
256  foreach ($attributeValues as $attributeValue) {
257  if ($attributeValue->getValue() instanceof $type) {
258  $attributeValueArray[] = $attributeValue;
259  }
260  }
261  return $attributeValueArray;
262  }
263 }
populateWithArray($dataObject, array $data, $interfaceName)
is_subclass_of($obj, $className)
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
getCustomAttributeValueByType(array $attributeValues, $type)
__construct(ObjectFactory $objectFactory, \Magento\Framework\Reflection\DataObjectProcessor $objectProcessor, \Magento\Framework\Reflection\TypeProcessor $typeProcessor, \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory, \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $joinProcessor, MethodsMap $methodsMapProcessor)
$type
Definition: item.phtml:13
$value
Definition: gender.phtml:16
_setDataValues($dataObject, array $data, $interfaceName)
setComplexValue( $dataObject, $getterMethodName, $methodName, array $value, $interfaceName)
mergeDataObjects( $interfaceName, $firstDataObject, $secondDataObject)