|
1 | 1 | <?php |
2 | 2 | /** |
3 | | - * Copyright © Magento, Inc. All rights reserved. |
4 | | - * See COPYING.txt for license details. |
| 3 | + * Copyright 2011 Adobe |
| 4 | + * All Rights Reserved. |
5 | 5 | */ |
6 | 6 | namespace Magento\Rule\Model\Condition; |
7 | 7 |
|
8 | 8 | /** |
9 | | - * Combine |
10 | | - * |
11 | 9 | * @api |
12 | 10 | * @since 100.0.2 |
13 | 11 | */ |
@@ -227,28 +225,65 @@ public function asXml($containerKey = 'conditions', $itemKey = 'condition') |
227 | 225 | * @param array $arr |
228 | 226 | * @param string $key |
229 | 227 | * @return $this |
230 | | - * @SuppressWarnings(PHPMD.NPathComplexity) |
231 | 228 | */ |
232 | 229 | public function loadArray($arr, $key = 'conditions') |
233 | 230 | { |
234 | | - $this->setAggregator( |
235 | | - isset($arr['aggregator']) ? $arr['aggregator'] : (isset($arr['attribute']) ? $arr['attribute'] : null) |
236 | | - )->setValue( |
237 | | - isset($arr['value']) ? $arr['value'] : (isset($arr['operator']) ? $arr['operator'] : null) |
238 | | - ); |
| 231 | + $this->setAggregatorAndValue($arr); |
| 232 | + $this->loadConditions($arr[$key] ?? [], $key); |
| 233 | + |
| 234 | + return $this; |
| 235 | + } |
| 236 | + |
| 237 | + /** |
| 238 | + * Set the aggregator and value from the array. |
| 239 | + * |
| 240 | + * @param array $arr |
| 241 | + */ |
| 242 | + private function setAggregatorAndValue(array $arr): void |
| 243 | + { |
| 244 | + $aggregator = $arr['aggregator'] ?? $arr['attribute'] ?? null; |
| 245 | + $value = $arr['value'] ?? $arr['operator'] ?? null; |
| 246 | + |
| 247 | + $this->setAggregator($aggregator) |
| 248 | + ->setValue($value); |
| 249 | + } |
| 250 | + |
| 251 | + /** |
| 252 | + * Load the conditions from the array. |
| 253 | + * |
| 254 | + * @param array $conditions |
| 255 | + * @param string $key |
| 256 | + */ |
| 257 | + private function loadConditions(array $conditions, string $key): void |
| 258 | + { |
| 259 | + foreach ($conditions as $conditionArr) { |
| 260 | + if (!empty($conditionArr['type'])) { |
| 261 | + $this->createAndAddCondition($conditionArr, $key); |
| 262 | + continue; |
| 263 | + } |
239 | 264 |
|
240 | | - if (!empty($arr[$key]) && is_array($arr[$key])) { |
241 | | - foreach ($arr[$key] as $conditionArr) { |
242 | | - try { |
243 | | - $condition = $this->_conditionFactory->create($conditionArr['type']); |
244 | | - $this->addCondition($condition); |
245 | | - $condition->loadArray($conditionArr, $key); |
246 | | - } catch (\Exception $e) { |
247 | | - $this->_logger->critical($e); |
248 | | - } |
| 265 | + // Recursively load conditions if there are nested conditions |
| 266 | + if (!empty($conditionArr[$key])) { |
| 267 | + $this->loadConditions($conditionArr[$key], $key); |
249 | 268 | } |
250 | 269 | } |
251 | | - return $this; |
| 270 | + } |
| 271 | + |
| 272 | + /** |
| 273 | + * Create and add a condition to the object. |
| 274 | + * |
| 275 | + * @param array $conditionArr |
| 276 | + * @param string $key |
| 277 | + */ |
| 278 | + private function createAndAddCondition(array $conditionArr, string $key): void |
| 279 | + { |
| 280 | + try { |
| 281 | + $condition = $this->_conditionFactory->create($conditionArr['type']); |
| 282 | + $this->addCondition($condition); |
| 283 | + $condition->loadArray($conditionArr, $key); |
| 284 | + } catch (\Exception $e) { |
| 285 | + $this->_logger->critical($e); |
| 286 | + } |
252 | 287 | } |
253 | 288 |
|
254 | 289 | /** |
|
0 commit comments