Skip to content

Commit

Permalink
Fix formatter for predicates
Browse files Browse the repository at this point in the history
  • Loading branch information
demyan112rv committed Jul 22, 2024
1 parent c7cdf85 commit 657e159
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 18 deletions.
6 changes: 3 additions & 3 deletions src/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ private function predicateToArray(Predicate $predicate, bool $withAdditionalFiel
case Predicate::OPERATOR_OR:
case Predicate::OPERATOR_NOT:
$value = [];
foreach ($predicate->getConfig() as $predicateOperator => $predicateConfig) {
foreach ($predicate->getConfig() as $predicateConfig) {
if ($predicateConfig instanceof Predicate) {
$value = array_merge($value, $this->predicateToArray($predicateConfig, false));
$value[] = $this->predicateToArray($predicateConfig, false);
} else {
$value[$predicateOperator] = $predicateConfig;
$value[] = $predicateConfig;
}
}
break;
Expand Down
6 changes: 3 additions & 3 deletions src/Predicate.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Predicate
private string $operator;

/**
* @var array<string, mixed>
* @var array<string|int, mixed>
*/
private array $config = [];

Expand Down Expand Up @@ -102,15 +102,15 @@ public function setOperator(string $operator): self
}

/**
* @return array<string, mixed>
* @return array<string|int, mixed>
*/
public function getConfig(): array
{
return $this->config;
}

/**
* @param array<string, mixed> $config
* @param array<string|int, mixed> $config
*/
public function setConfig(array $config): self
{
Expand Down
47 changes: 35 additions & 12 deletions tests/Unit/FormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,14 @@ public function testPredicateWithPredicates(): void
$predicate2 = new Predicate(Predicate::OPERATOR_START_WITH);
$predicate2->setConfig(['path' => '/test2']);

$predicate3 = new Predicate(Predicate::OPERATOR_START_WITH);
$predicate3->setConfig(['path' => '/test3']);

$predicate = new Predicate(Predicate::OPERATOR_AND);
$predicate->setConfig([
$predicate1->getOperator() => $predicate1,
$predicate2->getOperator() => $predicate2,
$predicate1,
$predicate2,
$predicate3,
]);

$stub = new Stub();
Expand All @@ -177,11 +181,20 @@ public function testPredicateWithPredicates(): void
$this->assertSame([
[
'and' => [
'equals' => [
'path' => '/test1',
[
'equals' => [
'path' => '/test1',
],
],
[
'startsWith' => [
'path' => '/test2',
],
],
'startsWith' => [
'path' => '/test2',
[
'startsWith' => [
'path' => '/test3',
],
],
],
'caseSensitive' => false,
Expand All @@ -194,8 +207,9 @@ public function testPredicateWithPredicatesAsArray(): void
{
$predicate = new Predicate(Predicate::OPERATOR_AND);
$predicate->setConfig([
Predicate::OPERATOR_EQUALS => ['path' => '/test1'],
Predicate::OPERATOR_START_WITH => ['path' => '/test2'],
[Predicate::OPERATOR_EQUALS => ['path' => '/test1']],
[Predicate::OPERATOR_START_WITH => ['path' => '/test2']],
[Predicate::OPERATOR_START_WITH => ['path' => '/test3']],
]);

$stub = new Stub();
Expand All @@ -212,11 +226,20 @@ public function testPredicateWithPredicatesAsArray(): void
$this->assertSame([
[
'and' => [
'equals' => [
'path' => '/test1',
[
'equals' => [
'path' => '/test1',
],
],
[
'startsWith' => [
'path' => '/test2',
],
],
'startsWith' => [
'path' => '/test2',
[
'startsWith' => [
'path' => '/test3',
],
],
],
'caseSensitive' => false,
Expand Down

0 comments on commit 657e159

Please sign in to comment.