diff --git a/src/Operation/Strategies/AddOperation.php b/src/Operation/Strategies/AddOperation.php index 0c48cbf..60c766c 100644 --- a/src/Operation/Strategies/AddOperation.php +++ b/src/Operation/Strategies/AddOperation.php @@ -21,8 +21,14 @@ public function apply(array &$data, string $path, mixed $value = null): void if (Arr::isArray($arrData) && !empty($arrData)) { if (Arr::isArray($value)) { - foreach ($value as $key => $item) { - $arrData[$key] = $item; + if (Arr::isList($value)) { + foreach ($value as $item) { + $arrData[] = $item; + } + } else { + foreach ($value as $key => $item) { + $arrData[$key] = $item; + } } } else { // Add new key-value pair to the array diff --git a/src/Utils/Arr.php b/src/Utils/Arr.php index f1448aa..e99f808 100644 --- a/src/Utils/Arr.php +++ b/src/Utils/Arr.php @@ -15,6 +15,7 @@ use function array_key_exists; use function is_null; use function is_float; +use function array_is_list; class Arr { @@ -28,6 +29,16 @@ public static function isArray(mixed $data): bool return is_array($data); } + /** + * @param array $data + * + * @return bool + */ + public static function isList(array $data): bool + { + return array_is_list($data); + } + // Get a reference to the value at a given path public static function &getReference(&$array, $key) { diff --git a/tests/Unit/Operation/Strategies/AddOperationStrategyTest.php b/tests/Unit/Operation/Strategies/AddOperationStrategyTest.php index 710aa7a..d6bb325 100644 --- a/tests/Unit/Operation/Strategies/AddOperationStrategyTest.php +++ b/tests/Unit/Operation/Strategies/AddOperationStrategyTest.php @@ -57,6 +57,30 @@ public function testAddOperation_WhenGivenNewKey_ReturnsAddedData(): void $this->assertEquals('myValue', $data['params']['newSettings']); } + public function testAddOperation_WhenGivenArrayData_ReturnsAddedData(): void + { + $data = $this->applyOperation('params.settings', + [ + [ + 'value' => ['id' => 1, 'type' => 'internal'] + ] + ], + [ + 'type' => 'button', + 'params' => [ + 'variantType' => 'FORM_GIFT_CARD_DETAILS', + 'settings' => [ + [ + 'value' => ['id' => 1, 'type' => 'internal'] + ] + ], + 'variantsStyles' => [] + ] + ]); + + $this->assertEquals(1, $data['params']['settings'][1]['value']['id']); + } + protected function applyOperation(string $path, mixed $value, array $data = null): array { $data ??= [ diff --git a/tests/Unit/QueryEvaluatorTest.php b/tests/Unit/QueryEvaluatorTest.php index 2145cf5..1bd181c 100644 --- a/tests/Unit/QueryEvaluatorTest.php +++ b/tests/Unit/QueryEvaluatorTest.php @@ -98,6 +98,26 @@ public function testEvaluate_WhenGivenWrongOperator_ReturnsFalse(): void $this->assertFalse($result); } + public function testEvaluate_WhenGivenWrongOperatorInDetails_ReturnsFalse(): void + { + $query = [ + "findBy" => [ + "AND" => [ + [ + "key" => "type", + "value" => "input", + "operator" => "ABC" + ], + ] + ] + ]; + + $queryEvaluator = $this->getInstance(); + $result = $queryEvaluator->evaluate($this->getData(), $query['findBy']); + + $this->assertFalse($result); + } + /** * @return array */