Skip to content

Commit

Permalink
update ADD operation
Browse files Browse the repository at this point in the history
  • Loading branch information
miqayelsrapionyan committed Nov 1, 2024
1 parent ef6fba3 commit 8e7e424
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/Operation/Strategies/AddOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions src/Utils/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use function array_key_exists;
use function is_null;
use function is_float;
use function array_is_list;

class Arr
{
Expand All @@ -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)
{
Expand Down
24 changes: 24 additions & 0 deletions tests/Unit/Operation/Strategies/AddOperationStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ??= [
Expand Down
20 changes: 20 additions & 0 deletions tests/Unit/QueryEvaluatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit 8e7e424

Please sign in to comment.