From 3f0dc2f33e0ec25bb1092c8242a05a1c53e59714 Mon Sep 17 00:00:00 2001 From: Javier Esteban Date: Mon, 19 Sep 2022 16:43:12 +0200 Subject: [PATCH 1/3] Upgrade pccomponentes/criteria package requirement --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 68f0ad7..2c2e644 100644 --- a/composer.json +++ b/composer.json @@ -5,7 +5,7 @@ "license": "MIT", "require": { "php": "^7.4 | ^8.0", - "pccomponentes/criteria": "^0.3" + "pccomponentes/criteria": "^0" }, "autoload": { "psr-4": { From fe4633c73e775e8ed336aa469b585d6828da69bb Mon Sep 17 00:00:00 2001 From: Javier Esteban Date: Mon, 19 Sep 2022 16:53:27 +0200 Subject: [PATCH 2/3] implements IN filter --- src/ArrayCriteriaVisitor.php | 4 ++++ tests/ArrayBuilderCriteriaVisitorTest.php | 24 +++++++++++++++++++++++ tests/Mocks/Article.php | 9 ++++++++- tests/Mocks/ArticleObjectMother.php | 4 ++++ 4 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/ArrayCriteriaVisitor.php b/src/ArrayCriteriaVisitor.php index 184ce7c..b6d6d7c 100644 --- a/src/ArrayCriteriaVisitor.php +++ b/src/ArrayCriteriaVisitor.php @@ -70,6 +70,10 @@ static function ($item) use ($filter) { if (FilterOperator::CONTAINS === $filter->operator()->value()) { return false !== \strpos($itemValue, $filter->value()->value()); } + + if (FilterOperator::IN === $filter->operator()->value()) { + return false !== in_array($filter->value()->value(), $itemValue); + } } return false; diff --git a/tests/ArrayBuilderCriteriaVisitorTest.php b/tests/ArrayBuilderCriteriaVisitorTest.php index c9f31e5..8a58a28 100644 --- a/tests/ArrayBuilderCriteriaVisitorTest.php +++ b/tests/ArrayBuilderCriteriaVisitorTest.php @@ -309,4 +309,28 @@ public function test_not_equal_operator() $result = $this->repository->filter($criteria); $this->assertEquals($article, $result[0]); } + + + public function test_in_operator() + { + $article = ArticleObjectMother::random(); + $this->repository->save($article); + + $criteria = new Criteria( + new Filters( + new Filter( + FilterField::from('tags'), + FilterOperator::from(FilterOperator::IN), + FilterValue::from(ArticleObjectMother::TAG_INCLUDED), + ), + ), + null, + null, + null, + ); + + $result = $this->repository->filter($criteria); + $this->assertEquals($article, $result[0]); + + } } diff --git a/tests/Mocks/Article.php b/tests/Mocks/Article.php index b85ccdb..ce864aa 100644 --- a/tests/Mocks/Article.php +++ b/tests/Mocks/Article.php @@ -11,13 +11,15 @@ class Article private string $name; private float $stock; private \DateTimeInterface $date; + private array $tags; - public function __construct(Uuid $id, string $name, int $stock, \DateTimeImmutable $date) + public function __construct(Uuid $id, string $name, int $stock, \DateTimeImmutable $date, array $tags) { $this->id = $id; $this->name = $name; $this->stock = $stock; $this->date = $date; + $this->tags = $tags; } public function id(): Uuid @@ -39,4 +41,9 @@ public function date(): \DateTimeImmutable { return $this->date; } + + public function tags(): array + { + return $this->tags; + } } diff --git a/tests/Mocks/ArticleObjectMother.php b/tests/Mocks/ArticleObjectMother.php index 4424c7d..6250892 100644 --- a/tests/Mocks/ArticleObjectMother.php +++ b/tests/Mocks/ArticleObjectMother.php @@ -8,6 +8,9 @@ class ArticleObjectMother { + const TAG_INCLUDED = 'TAG1'; + const TAG_NOT_INCLUDED = 'TAG2'; + public static function random(): Article { $faker = Factory::create(); @@ -17,6 +20,7 @@ public static function random(): Article $faker->text(100), $faker->numberBetween(0, 1000), \DateTimeImmutable::createFromMutable($faker->dateTimeBetween('-10 year', 'now')), + [self::TAG_INCLUDED] ); } } From 98ea1f33edd5c4f048e2b5f6f1b6493d619cabc9 Mon Sep 17 00:00:00 2001 From: Javier Esteban Date: Mon, 19 Sep 2022 16:54:48 +0200 Subject: [PATCH 3/3] Implements NOT_IN filter --- src/ArrayCriteriaVisitor.php | 4 ++++ tests/ArrayBuilderCriteriaVisitorTest.php | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/ArrayCriteriaVisitor.php b/src/ArrayCriteriaVisitor.php index b6d6d7c..95460a1 100644 --- a/src/ArrayCriteriaVisitor.php +++ b/src/ArrayCriteriaVisitor.php @@ -74,6 +74,10 @@ static function ($item) use ($filter) { if (FilterOperator::IN === $filter->operator()->value()) { return false !== in_array($filter->value()->value(), $itemValue); } + + if (FilterOperator::NOT_IN === $filter->operator()->value()) { + return true !== in_array($filter->value()->value(), $itemValue); + } } return false; diff --git a/tests/ArrayBuilderCriteriaVisitorTest.php b/tests/ArrayBuilderCriteriaVisitorTest.php index 8a58a28..a83a7f8 100644 --- a/tests/ArrayBuilderCriteriaVisitorTest.php +++ b/tests/ArrayBuilderCriteriaVisitorTest.php @@ -333,4 +333,27 @@ public function test_in_operator() $this->assertEquals($article, $result[0]); } + + public function test_not_in_operator() + { + $article = ArticleObjectMother::random(); + $this->repository->save($article); + + $criteria = new Criteria( + new Filters( + new Filter( + FilterField::from('tags'), + FilterOperator::from(FilterOperator::NOT_IN), + FilterValue::from(ArticleObjectMother::TAG_NOT_INCLUDED), + ), + ), + null, + null, + null, + ); + + $result = $this->repository->filter($criteria); + $this->assertEquals($article, $result[0]); + + } }