Skip to content

Commit 90ae75b

Browse files
committed
Merge branch 'ACP2E-3545' of https://github.com/adobe-commerce-tier-4/magento2ce into Tier4-03-03-2025
2 parents 9c05217 + 10c9c4a commit 90ae75b

File tree

2 files changed

+59
-24
lines changed

2 files changed

+59
-24
lines changed

app/code/Magento/Rule/Model/Condition/Combine.php

Lines changed: 55 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2011 Adobe
4+
* All Rights Reserved.
55
*/
66
namespace Magento\Rule\Model\Condition;
77

88
/**
9-
* Combine
10-
*
119
* @api
1210
* @since 100.0.2
1311
*/
@@ -227,28 +225,65 @@ public function asXml($containerKey = 'conditions', $itemKey = 'condition')
227225
* @param array $arr
228226
* @param string $key
229227
* @return $this
230-
* @SuppressWarnings(PHPMD.NPathComplexity)
231228
*/
232229
public function loadArray($arr, $key = 'conditions')
233230
{
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+
}
239264

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);
249268
}
250269
}
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+
}
252287
}
253288

254289
/**

app/code/Magento/Rule/Test/Unit/Model/Condition/CombineTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2014 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

@@ -137,12 +137,12 @@ public function testLoadArrayLoggerCatchException()
137137
$this->conditionObjectMock->expects($this->never())
138138
->method('loadArray');
139139

140-
$this->conditionFactoryMock->expects($this->once())
140+
$this->conditionFactoryMock->expects($this->never())
141141
->method('create')
142142
->with($array['conditions'][0]['type'])
143143
->willThrowException(new \Exception('everything is fine, it is test'));
144144

145-
$this->loggerMock->expects($this->once())
145+
$this->loggerMock->expects($this->never())
146146
->method('critical')
147147
->with();
148148

0 commit comments

Comments
 (0)