Skip to content

Commit

Permalink
added empty not empty operators
Browse files Browse the repository at this point in the history
  • Loading branch information
miqayelsrapionyan committed Nov 8, 2024
1 parent 8e7e424 commit 1b16537
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/ConditionEvaluator/ConditionEvaluator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function evaluate(array $data, array $condition): bool
{
// Extract condition details
$key = $condition['key'];
$value = $condition['value'];
$value = $condition['value'] ?? null;
$operator = $condition['operator'];

// Get the actual value from the data using the Arr helper
Expand Down
6 changes: 6 additions & 0 deletions src/Enums/ConditionalOperatorEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@
* ConditionalOperatorEnum defines conditional operator types.
* EQ - equals, the conditions on this operator must be equal.
* NEQ - not equal, the conditions on this operator must not be equal.
* EMPTY - empty, the conditions on this operator must be empty.
* NEMPTY - not empty, the conditions on this operator must not be empty.
*/
enum ConditionalOperatorEnum : string
{
case EQ = 'EQ';
case NEQ = 'NEQ';
case EMPTY = 'EMPTY';
case NEMPTY = 'NEMPTY';

/**
* @param mixed $actualValue
Expand All @@ -25,6 +29,8 @@ public function compare(mixed $actualValue, mixed $value): bool
return match($this) {
self::EQ => $actualValue === $value,
self::NEQ => $actualValue !== $value,
self::EMPTY => empty($actualValue),
self::NEMPTY => !empty($actualValue),
};
}
}
56 changes: 56 additions & 0 deletions tests/Unit/ConditionEvaluatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,62 @@ public function testEvaluate_WhenGivenNEQOperatorAndWrongKey_ReturnsFalse(): voi
$this->assertFalse($result);
}

public function testEvaluate_WhenGivenEMPTYOperatorAndValidValue_ReturnsTrue(): void
{
$data = $this->getData();

$conditions = [
'key' => 'params.variantsStyles',
'operator' => ConditionalOperatorEnum::EMPTY->value,
];

$result = $this->createInstance()->evaluate($data, $conditions);

$this->assertTrue($result);
}

public function testEvaluate_WhenGivenEMPTYOperatorAndNotValidValue_ReturnsFalse(): void
{
$data = $this->getData();

$conditions = [
'key' => 'params.settings.name',
'operator' => ConditionalOperatorEnum::EMPTY->value,
];

$result = $this->createInstance()->evaluate($data, $conditions);

$this->assertFalse($result);
}

public function testEvaluate_WhenGivenNEMPTYOperatorAndValidValue_ReturnsTrue(): void
{
$data = $this->getData();

$conditions = [
'key' => 'params.settings.name',
'operator' => ConditionalOperatorEnum::NEMPTY->value,
];

$result = $this->createInstance()->evaluate($data, $conditions);

$this->assertTrue($result);
}

public function testEvaluate_WhenGivenNEMPTYOperatorAndNotValidValue_ReturnsFalse(): void
{
$data = $this->getData();

$conditions = [
'key' => 'params.variantsStyles',
'operator' => ConditionalOperatorEnum::NEMPTY->value,
];

$result = $this->createInstance()->evaluate($data, $conditions);

$this->assertFalse($result);
}

/**
* @return array
*/
Expand Down

0 comments on commit 1b16537

Please sign in to comment.