Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add filters of Between, EqualsEmpty and EqualsNull #85

Merged
merged 2 commits into from
Feb 14, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ Filter could be composed with:

- `All`
- `Any`
- `Between`
- `Equals`
- `EqualsEmpty`
- `EqualsNull`
- `GreaterThan`
- `GreaterThanOrEqual`
- `In`
Expand Down
64 changes: 64 additions & 0 deletions src/Reader/Filter/Between.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Data\Reader\Filter;

use InvalidArgumentException;

use function gettype;
use function is_scalar;

final class Between implements FilterInterface
{
private string $field;

/**
* @var bool|float|int|string
*/
private $firstValue;

/**
* @var bool|float|int|string
*/
private $secondValue;

/**
* @param string $field
* @param bool|float|int|string $firstValue
* @param bool|float|int|string $secondValue
*/
public function __construct(string $field, $firstValue, $secondValue)
{
$this->field = $field;

$this->validateValue($firstValue);
$this->validateValue($secondValue);

$this->firstValue = $firstValue;
$this->secondValue = $secondValue;
}

public function toArray(): array
{
return [self::getOperator(), $this->field, $this->firstValue, $this->secondValue];
}

public static function getOperator(): string
{
return 'between';
}

/**
* @param mixed $value
*/
private function validateValue($value): void
{
if (!is_scalar($value)) {
throw new InvalidArgumentException(sprintf(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about datetimeInterface?
Is it possible to support this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that would be great. @roxblnfk would you please add an issue about it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the same filter? Or can create a separate BetweenDate class?

Copy link
Member Author

@devanych devanych Feb 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created issue: #88

'The value should be scalar. The %s is received.',
gettype($value),
devanych marked this conversation as resolved.
Show resolved Hide resolved
));
}
}
}
25 changes: 25 additions & 0 deletions src/Reader/Filter/EqualsEmpty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Data\Reader\Filter;

final class EqualsEmpty implements FilterInterface
{
private string $field;

public function __construct(string $field)
{
$this->field = $field;
}

public function toArray(): array
{
return [self::getOperator(), $this->field];
}

public static function getOperator(): string
{
return 'empty';
}
}
25 changes: 25 additions & 0 deletions src/Reader/Filter/EqualsNull.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Data\Reader\Filter;

final class EqualsNull implements FilterInterface
{
private string $field;

public function __construct(string $field)
{
$this->field = $field;
}

public function toArray(): array
{
return [self::getOperator(), $this->field];
}

public static function getOperator(): string
{
return 'null';
}
}
28 changes: 28 additions & 0 deletions src/Reader/Iterable/Processor/Between.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Data\Reader\Iterable\Processor;

use InvalidArgumentException;
use Yiisoft\Data\Reader\Filter\FilterProcessorInterface;

use function array_key_exists;

class Between implements IterableProcessorInterface, FilterProcessorInterface
{
public function getOperator(): string
{
return \Yiisoft\Data\Reader\Filter\Between::getOperator();
}

public function match(array $item, array $arguments, array $filterProcessors): bool
{
if (count($arguments) !== 3) {
throw new InvalidArgumentException('$arguments should contain exactly three elements.');
}

[$field, $firstValue, $secondValue] = $arguments;
return array_key_exists($field, $item) && $item[$field] >= $firstValue && $item[$field] <= $secondValue;
}
}
26 changes: 26 additions & 0 deletions src/Reader/Iterable/Processor/EqualsEmpty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Data\Reader\Iterable\Processor;

use InvalidArgumentException;
use Yiisoft\Data\Reader\Filter\FilterProcessorInterface;

class EqualsEmpty implements IterableProcessorInterface, FilterProcessorInterface
{
public function getOperator(): string
{
return \Yiisoft\Data\Reader\Filter\EqualsEmpty::getOperator();
}

public function match(array $item, array $arguments, array $filterProcessors): bool
{
if (count($arguments) !== 1) {
throw new InvalidArgumentException('$arguments should contain exactly one element.');
}

[$field] = $arguments;
return empty($item[$field]);
}
}
28 changes: 28 additions & 0 deletions src/Reader/Iterable/Processor/EqualsNull.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Data\Reader\Iterable\Processor;

use InvalidArgumentException;
use Yiisoft\Data\Reader\Filter\FilterProcessorInterface;

use function array_key_exists;

class EqualsNull implements IterableProcessorInterface, FilterProcessorInterface
{
public function getOperator(): string
{
return \Yiisoft\Data\Reader\Filter\EqualsNull::getOperator();
}

public function match(array $item, array $arguments, array $filterProcessors): bool
{
if (count($arguments) !== 1) {
throw new InvalidArgumentException('$arguments should contain exactly one element.');
}

[$field] = $arguments;
return array_key_exists($field, $item) && $item[$field] === null;
}
}
6 changes: 6 additions & 0 deletions tests/Reader/FilterProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
use InvalidArgumentException;
use Yiisoft\Data\Reader\Iterable\IterableDataReader;
use Yiisoft\Data\Reader\Iterable\Processor\All;
use Yiisoft\Data\Reader\Iterable\Processor\Between;
use Yiisoft\Data\Reader\Iterable\Processor\Equals;
use Yiisoft\Data\Reader\Iterable\Processor\EqualsEmpty;
use Yiisoft\Data\Reader\Iterable\Processor\EqualsNull;
use Yiisoft\Data\Reader\Iterable\Processor\GreaterThan;
use Yiisoft\Data\Reader\Iterable\Processor\GreaterThanOrEqual;
use Yiisoft\Data\Reader\Iterable\Processor\In;
Expand Down Expand Up @@ -77,7 +80,10 @@ public function match(array $item, array $arguments, array $filterProcessors): b
public function invalidFiltersArrayDataProvider(): array
{
return [
'betweenArgumentsTooSmall' => [new Between(), ['id'], []],
'equalsArgumentsTooSmall' => [new Equals(), ['id'], []],
'equalsEmptyArgumentsTooSmall' => [new EqualsEmpty(), [], []],
'equalsNullArgumentsTooSmall' => [new EqualsNull(), [], []],
'greaterThanArgumentsTooSmall' => [new GreaterThan(), ['id'], []],
'greaterThanOrEqualArgumentsTooSmall' => [new GreaterThanOrEqual(), ['id'], []],
'lessThanArgumentsTooSmall' => [new LessThan(), ['id'], []],
Expand Down
15 changes: 15 additions & 0 deletions tests/Reader/FilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
use InvalidArgumentException;
use Yiisoft\Data\Reader\Filter\All;
use Yiisoft\Data\Reader\Filter\Any;
use Yiisoft\Data\Reader\Filter\Between;
use Yiisoft\Data\Reader\Filter\Equals;
use Yiisoft\Data\Reader\Filter\EqualsEmpty;
use Yiisoft\Data\Reader\Filter\EqualsNull;
use Yiisoft\Data\Reader\Filter\FilterInterface;
use Yiisoft\Data\Reader\Filter\GreaterThan;
use Yiisoft\Data\Reader\Filter\GreaterThanOrEqual;
Expand All @@ -23,10 +26,22 @@ final class FilterTest extends TestCase
public function filterDataProvider(): array
{
return [
'Between' => [
new Between('test', 42, 44),
['between', 'test', 42, 44],
],
'Equals' => [
new Equals('test', 42),
['=', 'test', 42],
],
'EqualsEmpty' => [
new EqualsEmpty('test'),
['empty', 'test'],
],
'EqualsNull' => [
new EqualsNull('test'),
['null', 'test'],
],
'In' => [
new In('test', [1, 2, 3]),
['in', 'test', [1, 2, 3]],
Expand Down
38 changes: 38 additions & 0 deletions tests/Reader/Iterable/Processor/BetweenTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Data\Tests\Reader\Iterable\Processor;

use PHPUnit\Framework\TestCase;
use Yiisoft\Data\Reader\Iterable\Processor\Between;

final class BetweenTest extends TestCase
{
public function dataBase(): array
{
return [
[true, ['weight', 42, 47]],
[true, ['weight', 45, 45]],
[true, ['weight', 45, 46]],
[false, ['weight', 46, 47]],
[false, ['weight', 46, 45]],
[false, ['age', 42, 47]],
];
}

/**
* @dataProvider dataBase
*/
public function testBase(bool $expected, array $arguments): void
{
$processor = new Between();

$item = [
'id' => 1,
'weight' => 45,
];

$this->assertSame($expected, $processor->match($item, $arguments, []));
}
}
33 changes: 33 additions & 0 deletions tests/Reader/Iterable/Processor/EqualsEmptyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Data\Tests\Reader\Iterable\Processor;

use PHPUnit\Framework\TestCase;
use Yiisoft\Data\Reader\Iterable\Processor\EqualsEmpty;

final class EqualsEmptyTest extends TestCase
{
public function dataBase(): array
{
return [
[true, ['value' => null]],
[true, ['value' => false]],
[true, ['value' => 0]],
[true, ['value' => 0.0]],
devanych marked this conversation as resolved.
Show resolved Hide resolved
[true, ['value' => '']],
[false, ['value' => 42]],
[false, ['value' => true]],
[true, ['not-exist' => 42]],
];
}

/**
* @dataProvider dataBase
*/
public function testBase(bool $expected, array $item): void
{
$this->assertSame($expected, (new EqualsEmpty())->match($item, ['value'], []));
}
}
34 changes: 34 additions & 0 deletions tests/Reader/Iterable/Processor/EqualsNullTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Data\Tests\Reader\Iterable\Processor;

use PHPUnit\Framework\TestCase;
use Yiisoft\Data\Reader\Iterable\Processor\EqualsNull;

final class EqualsNullTest extends TestCase
{
public function dataBase(): array
{
return [
[true, ['value' => null]],
[false, ['value' => false]],
[false, ['value' => true]],
[false, ['value' => 0]],
[false, ['value' => 0.0]],
[false, ['value' => 42]],
[false, ['value' => '']],
[false, ['value' => 'null']],
[false, ['not-exist' => null]],
];
}

/**
* @dataProvider dataBase
*/
public function testBase(bool $expected, array $item): void
{
$this->assertSame($expected, (new EqualsNull())->match($item, ['value'], []));
}
}