Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
DataObjectHelper Class Reference

Public Member Functions

 __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)
 
 populateWithArray ($dataObject, array $data, $interfaceName)
 
 mergeDataObjects ( $interfaceName, $firstDataObject, $secondDataObject)
 
 getCustomAttributeValueByType (array $attributeValues, $type)
 

Protected Member Functions

 _setDataValues ($dataObject, array $data, $interfaceName)
 
 setComplexValue ( $dataObject, $getterMethodName, $methodName, array $value, $interfaceName)
 

Protected Attributes

 $objectFactory
 
 $objectProcessor
 
 $typeProcessor
 
 $extensionFactory
 
 $joinProcessor
 
 $methodsMapProcessor
 

Detailed Description

Data object helper.

@SuppressWarnings(PHPMD.CouplingBetweenObjects)

Definition at line 16 of file DataObjectHelper.php.

Constructor & Destructor Documentation

◆ __construct()

Parameters
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

Definition at line 56 of file DataObjectHelper.php.

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  }

Member Function Documentation

◆ _setDataValues()

_setDataValues (   $dataObject,
array  $data,
  $interfaceName 
)
protected

Update Data Object with the data from array

Parameters
mixed$dataObject
array$data
string$interfaceName
Returns
$this @SuppressWarnings(PHPMD.CyclomaticComplexity)

Definition at line 98 of file DataObjectHelper.php.

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  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$value
Definition: gender.phtml:16
setComplexValue( $dataObject, $getterMethodName, $methodName, array $value, $interfaceName)

◆ getCustomAttributeValueByType()

getCustomAttributeValueByType ( array  $attributeValues,
  $type 
)

Filter attribute value objects for a provided data interface type from an array of custom attribute value objects

Parameters
AttributeValue[]$attributeValues Array of custom attribute
string$typeData interface type
Returns
AttributeValue[]

Definition at line 250 of file DataObjectHelper.php.

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  }
$type
Definition: item.phtml:13

◆ mergeDataObjects()

mergeDataObjects (   $interfaceName,
  $firstDataObject,
  $secondDataObject 
)

Merges second object onto the first

Parameters
string$interfaceName
mixed$firstDataObject
mixed$secondDataObject
Returns
$this
Exceptions

Definition at line 230 of file DataObjectHelper.php.

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  }
_setDataValues($dataObject, array $data, $interfaceName)

◆ populateWithArray()

populateWithArray (   $dataObject,
array  $data,
  $interfaceName 
)

Populate data object using data in array format.

Parameters
mixed$dataObject
array$data
string$interfaceName
Returns
$this

Definition at line 80 of file DataObjectHelper.php.

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  }
_setDataValues($dataObject, array $data, $interfaceName)

◆ setComplexValue()

setComplexValue (   $dataObject,
  $getterMethodName,
  $methodName,
array  $value,
  $interfaceName 
)
protected
Parameters
mixed$dataObject
string$getterMethodName
string$methodName
array$value
string$interfaceName
Returns
$this @SuppressWarnings(PHPMD.CyclomaticComplexity)

Definition at line 148 of file DataObjectHelper.php.

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  }
populateWithArray($dataObject, array $data, $interfaceName)
is_subclass_of($obj, $className)
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$type
Definition: item.phtml:13
$value
Definition: gender.phtml:16

Field Documentation

◆ $extensionFactory

$extensionFactory
protected

Definition at line 36 of file DataObjectHelper.php.

◆ $joinProcessor

$joinProcessor
protected

Definition at line 41 of file DataObjectHelper.php.

◆ $methodsMapProcessor

$methodsMapProcessor
protected

Definition at line 46 of file DataObjectHelper.php.

◆ $objectFactory

$objectFactory
protected

Definition at line 21 of file DataObjectHelper.php.

◆ $objectProcessor

$objectProcessor
protected

Definition at line 26 of file DataObjectHelper.php.

◆ $typeProcessor

$typeProcessor
protected

Definition at line 31 of file DataObjectHelper.php.


The documentation for this class was generated from the following file: