Skip to content

Commit

Permalink
Merge pull request #2 from PcComponentes/ZT-00-implement-in-filter
Browse files Browse the repository at this point in the history
Implement IN and NOT_IN filter
  • Loading branch information
fjesteban authored Sep 19, 2022
2 parents 39870ca + 98ea1f3 commit d44928a
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 2 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "MIT",
"require": {
"php": "^7.4 | ^8.0",
"pccomponentes/criteria": "^0.3"
"pccomponentes/criteria": "^0"
},
"autoload": {
"psr-4": {
Expand Down
8 changes: 8 additions & 0 deletions src/ArrayCriteriaVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ 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);
}

if (FilterOperator::NOT_IN === $filter->operator()->value()) {
return true !== in_array($filter->value()->value(), $itemValue);
}
}

return false;
Expand Down
47 changes: 47 additions & 0 deletions tests/ArrayBuilderCriteriaVisitorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,4 +309,51 @@ 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]);

}

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]);

}
}
9 changes: 8 additions & 1 deletion tests/Mocks/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -39,4 +41,9 @@ public function date(): \DateTimeImmutable
{
return $this->date;
}

public function tags(): array
{
return $this->tags;
}
}
4 changes: 4 additions & 0 deletions tests/Mocks/ArticleObjectMother.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

class ArticleObjectMother
{
const TAG_INCLUDED = 'TAG1';
const TAG_NOT_INCLUDED = 'TAG2';

public static function random(): Article
{
$faker = Factory::create();
Expand All @@ -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]
);
}
}

0 comments on commit d44928a

Please sign in to comment.