|
9 | 9 | use Magento\Framework\Reflection\MethodsMap;
|
10 | 10 |
|
11 | 11 | /**
|
12 |
| - * Data object helper. |
| 12 | + * Service class allow populating object from array data |
13 | 13 | *
|
14 | 14 | * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
15 | 15 | */
|
@@ -94,49 +94,69 @@ public function populateWithArray($dataObject, array $data, $interfaceName)
|
94 | 94 | * @param string $interfaceName
|
95 | 95 | * @return $this
|
96 | 96 | * @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
| 97 | + * @SuppressWarnings(PHPMD.NPathComplexity) |
97 | 98 | */
|
98 | 99 | protected function _setDataValues($dataObject, array $data, $interfaceName)
|
99 | 100 | {
|
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 */ |
103 |
| - $camelCaseKey = \Magento\Framework\Api\SimpleDataObjectConverter::snakeCaseToUpperCamelCase($key); |
104 |
| - $possibleMethods = [ |
105 |
| - 'set' . $camelCaseKey, |
106 |
| - 'setIs' . $camelCaseKey, |
107 |
| - ]; |
108 |
| - if ($key === CustomAttributesDataInterface::CUSTOM_ATTRIBUTES |
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 |
| 101 | + if (empty($data)) { |
| 102 | + return $this; |
| 103 | + } |
| 104 | + $setMethods = $this->getSetters($dataObject); |
| 105 | + if ($dataObject instanceof ExtensibleDataInterface |
| 106 | + && !empty($data[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES]) |
| 107 | + ) { |
| 108 | + foreach ($data[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES] as $customAttribute) { |
| 109 | + $dataObject->setCustomAttribute( |
| 110 | + $customAttribute[AttributeInterface::ATTRIBUTE_CODE], |
| 111 | + $customAttribute[AttributeInterface::VALUE] |
| 112 | + ); |
| 113 | + } |
| 114 | + unset($data[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES]); |
| 115 | + } |
| 116 | + if ($dataObject instanceof \Magento\Framework\Model\AbstractModel) { |
| 117 | + $simpleData = array_filter($data, static function ($e) { |
| 118 | + return is_scalar($e) || is_null($e); |
| 119 | + }); |
| 120 | + if (isset($simpleData['id'])) { |
| 121 | + $dataObject->setId($simpleData['id']); |
| 122 | + unset($simpleData['id']); |
| 123 | + } |
| 124 | + $simpleData = array_intersect_key($simpleData, $setMethods); |
| 125 | + $dataObject->addData($simpleData); |
| 126 | + $data = array_diff_key($data, $simpleData); |
| 127 | + if (\count($data) === 0) { |
| 128 | + return $this; |
| 129 | + } |
| 130 | + } |
| 131 | + foreach (array_intersect_key($data, $setMethods) as $key => $value) { |
| 132 | + $methodName = SimpleDataObjectConverter::snakeCaseToUpperCamelCase($key); |
| 133 | + |
| 134 | + if (!is_array($value)) { |
| 135 | + if ($methodName !== 'ExtensionAttributes' || $value !== null) { |
| 136 | + if (method_exists($dataObject, 'set' . $methodName)) { |
| 137 | + $dataObject->{'set' . $methodName}($value); |
124 | 138 | } else {
|
125 |
| - $dataObject->$methodName($value); |
| 139 | + $dataObject->{'setIs' . $methodName}($value); |
126 | 140 | }
|
127 |
| - } else { |
128 |
| - $getterMethodName = 'get' . $camelCaseKey; |
129 |
| - $this->setComplexValue($dataObject, $getterMethodName, $methodName, $value, $interfaceName); |
130 | 141 | }
|
131 |
| - } elseif ($dataObject instanceof CustomAttributesDataInterface) { |
132 |
| - $dataObject->setCustomAttribute($key, $value); |
| 142 | + } else { |
| 143 | + $getterMethodName = 'get' . $methodName; |
| 144 | + $this->setComplexValue($dataObject, $getterMethodName, 'set' . $methodName, $value, $interfaceName); |
133 | 145 | }
|
| 146 | + unset($data[$key]); |
134 | 147 | }
|
135 | 148 |
|
| 149 | + if ($dataObject instanceof CustomAttributesDataInterface) { |
| 150 | + foreach ($data as $key => $value) { |
| 151 | + $dataObject->setCustomAttribute($key, $value); |
| 152 | + } |
| 153 | + } |
136 | 154 | return $this;
|
137 | 155 | }
|
138 | 156 |
|
139 | 157 | /**
|
| 158 | + * Set complex (like object) value using $methodName based on return type of $getterMethodName |
| 159 | + * |
140 | 160 | * @param mixed $dataObject
|
141 | 161 | * @param string $getterMethodName
|
142 | 162 | * @param string $methodName
|
@@ -179,7 +199,7 @@ protected function setComplexValue(
|
179 | 199 | } elseif (is_subclass_of($returnType, \Magento\Framework\Api\ExtensionAttributesInterface::class)) {
|
180 | 200 | foreach ($value as $extensionAttributeKey => $extensionAttributeValue) {
|
181 | 201 | $extensionAttributeGetterMethodName
|
182 |
| - = 'get' . \Magento\Framework\Api\SimpleDataObjectConverter::snakeCaseToUpperCamelCase( |
| 202 | + = 'get' . SimpleDataObjectConverter::snakeCaseToUpperCamelCase( |
183 | 203 | $extensionAttributeKey
|
184 | 204 | );
|
185 | 205 | $methodReturnType = $this->methodsMapProcessor->getMethodReturnType(
|
@@ -260,4 +280,40 @@ public function getCustomAttributeValueByType(array $attributeValues, $type)
|
260 | 280 | }
|
261 | 281 | return $attributeValueArray;
|
262 | 282 | }
|
| 283 | + |
| 284 | + /** @var array */ |
| 285 | + private array $settersCache = []; |
| 286 | + |
| 287 | + /** |
| 288 | + * Get list of setters for object |
| 289 | + * |
| 290 | + * @param object $dataObject |
| 291 | + * @return array |
| 292 | + */ |
| 293 | + private function getSetters(object $dataObject): array |
| 294 | + { |
| 295 | + $class = get_class($dataObject); |
| 296 | + if (!isset($this->settersCache[$class])) { |
| 297 | + $dataObjectMethods = get_class_methods($class); |
| 298 | + // use regexp to manipulate with method list as it use jit starting with PHP 7.3 |
| 299 | + $setters = array_filter( |
| 300 | + explode( |
| 301 | + ',', |
| 302 | + strtolower( |
| 303 | + // (0) remove all not setter |
| 304 | + // (1) add _ before upper letter |
| 305 | + // (2) remove set_ in start of name |
| 306 | + // (3) add name without is_ prefix |
| 307 | + preg_replace( |
| 308 | + ['/(^|,)(?!set)[^,]*/S','/(.)([A-Z])/S', '/(^|,)set_/iS', '/(^|,)is_([^,]+)/is'], |
| 309 | + ['', '$1_$2', '$1', '$1$2,is_$2'], |
| 310 | + implode(',', $dataObjectMethods) |
| 311 | + ) |
| 312 | + ) |
| 313 | + ) |
| 314 | + ); |
| 315 | + $this->settersCache[$class] = array_flip($setters); |
| 316 | + } |
| 317 | + return $this->settersCache[$class]; |
| 318 | + } |
263 | 319 | }
|
0 commit comments