generated from yiisoft/package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 18
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
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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( | ||
'The value should be scalar. The %s is received.', | ||
gettype($value), | ||
devanych marked this conversation as resolved.
Show resolved
Hide resolved
|
||
)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, [])); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'], [])); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'], [])); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Created issue: #88