diff --git a/README.md b/README.md index 69b9cdcc..c7b22071 100644 --- a/README.md +++ b/README.md @@ -94,7 +94,10 @@ Filter could be composed with: - `All` - `Any` +- `Between` - `Equals` +- `EqualsEmpty` +- `EqualsNull` - `GreaterThan` - `GreaterThanOrEqual` - `In` diff --git a/src/Reader/Filter/Between.php b/src/Reader/Filter/Between.php new file mode 100644 index 00000000..ed83a45b --- /dev/null +++ b/src/Reader/Filter/Between.php @@ -0,0 +1,67 @@ +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( + 'The value should be scalar. The %s is received.', + is_object($value) ? get_class($value) : gettype($value), + )); + } + } +} diff --git a/src/Reader/Filter/EqualsEmpty.php b/src/Reader/Filter/EqualsEmpty.php new file mode 100644 index 00000000..f3aba419 --- /dev/null +++ b/src/Reader/Filter/EqualsEmpty.php @@ -0,0 +1,25 @@ +field = $field; + } + + public function toArray(): array + { + return [self::getOperator(), $this->field]; + } + + public static function getOperator(): string + { + return 'empty'; + } +} diff --git a/src/Reader/Filter/EqualsNull.php b/src/Reader/Filter/EqualsNull.php new file mode 100644 index 00000000..3b87484f --- /dev/null +++ b/src/Reader/Filter/EqualsNull.php @@ -0,0 +1,25 @@ +field = $field; + } + + public function toArray(): array + { + return [self::getOperator(), $this->field]; + } + + public static function getOperator(): string + { + return 'null'; + } +} diff --git a/src/Reader/Iterable/Processor/Between.php b/src/Reader/Iterable/Processor/Between.php new file mode 100644 index 00000000..71462db1 --- /dev/null +++ b/src/Reader/Iterable/Processor/Between.php @@ -0,0 +1,28 @@ += $firstValue && $item[$field] <= $secondValue; + } +} diff --git a/src/Reader/Iterable/Processor/EqualsEmpty.php b/src/Reader/Iterable/Processor/EqualsEmpty.php new file mode 100644 index 00000000..eeca9f8f --- /dev/null +++ b/src/Reader/Iterable/Processor/EqualsEmpty.php @@ -0,0 +1,26 @@ + [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'], []], diff --git a/tests/Reader/FilterTest.php b/tests/Reader/FilterTest.php index 4e7b3a10..87fd08c8 100644 --- a/tests/Reader/FilterTest.php +++ b/tests/Reader/FilterTest.php @@ -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; @@ -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]], diff --git a/tests/Reader/Iterable/Processor/BetweenTest.php b/tests/Reader/Iterable/Processor/BetweenTest.php new file mode 100644 index 00000000..6fc976f0 --- /dev/null +++ b/tests/Reader/Iterable/Processor/BetweenTest.php @@ -0,0 +1,38 @@ + 1, + 'weight' => 45, + ]; + + $this->assertSame($expected, $processor->match($item, $arguments, [])); + } +} diff --git a/tests/Reader/Iterable/Processor/EqualsEmptyTest.php b/tests/Reader/Iterable/Processor/EqualsEmptyTest.php new file mode 100644 index 00000000..c3e2b4e2 --- /dev/null +++ b/tests/Reader/Iterable/Processor/EqualsEmptyTest.php @@ -0,0 +1,33 @@ + null]], + [true, ['value' => false]], + [true, ['value' => 0]], + [true, ['value' => 0.0]], + [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'], [])); + } +} diff --git a/tests/Reader/Iterable/Processor/EqualsNullTest.php b/tests/Reader/Iterable/Processor/EqualsNullTest.php new file mode 100644 index 00000000..3f0618e9 --- /dev/null +++ b/tests/Reader/Iterable/Processor/EqualsNullTest.php @@ -0,0 +1,34 @@ + 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'], [])); + } +}