Skip to content

Commit

Permalink
Fix #115: Rename filter processor to handler (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
samdark committed Jan 8, 2023
1 parent 951d77b commit 1f27ac5
Show file tree
Hide file tree
Showing 47 changed files with 169 additions and 173 deletions.
26 changes: 13 additions & 13 deletions README.md
Expand Up @@ -127,16 +127,16 @@ $dataReader->withFilter((new All())->withFiltersArray([

In order to have your own filter:
- Implement at least `FilterInterface`, which includes:
- `getOperator()` method that returns a string that represents a filter operation
- `getOperator()` method that returns a string that represents a filter operation.
- `toArray()` method that returns an array with filtering parameters.
- If you want to create a filter processor for a specific data reader type, then you need to implement at least
`FilterProcessorInterface`. It has a single `getOperator()` method that returns a string representing a filter operation.
In addition, each data reader specifies an extended interface required for processing or building the operation.
*For example, `IterableDataFilter` defines `IterableProcessorInterface`, which contains additional `match()`
- If you want to create a filter handler for a specific data reader type, then you need to implement at least
`FilterHandlerInterface`. It has a single `getOperator()` method that returns a string representing a filter operation.
In addition, each data reader specifies an extended interface required for handling or building the operation.
*For example, `IterableDataFilter` defines `IterableHandlerInterface`, which contains additional `match()`
method to execute a filter on PHP variables.*

You can add your own filter processors to the data reader using the `withFilterProcessors()` method. You can add any filter
processor to Reader. If reader is not able to use a filter, filter is ignored.
You can add your own filter handlers to the data reader using the `withFilterHandlers()` method. You can add any filter
handler to Reader. If reader is not able to use a filter, filter is ignored.

```php
// own filter for filtering
Expand All @@ -158,15 +158,15 @@ class OwnNotTwoFilter implenents FilterInterface
}
}

// own iterable filter processor for matching
class OwnIterableNotTwoFilterProcessor implements
// own iterable filter handler for matching
class OwnIterableNotTwoFilterHandler implements IterableHandlerInterface
{
public function getOperator(): string
{
return OwnNotTwoFilter::getOperator();
}

public function match(array $item, array $arguments, array $filterProcessors): bool
public function match(array $item, array $arguments, array $filterHandlers): bool
{
[$field] = $arguments;
return $item[$field] != 2;
Expand All @@ -181,9 +181,9 @@ $filter = new All(

$reader = (new MyDataReader(...))
->withFilter($filter)
->withFilterProcessors(
new OwnIterableNotTwoFilterProcessor()
new OwnSqlNotTwoFilterProcessor() // for SQL
->withFilterHandlers(
new OwnIterableNotTwoFilter()
new OwnSqlNotTwoFilter() // for SQL
// and for any supported readers...
);

Expand Down
6 changes: 3 additions & 3 deletions src/Paginator/KeysetPaginator.php
Expand Up @@ -7,7 +7,7 @@
use InvalidArgumentException;
use RuntimeException;
use Yiisoft\Arrays\ArrayHelper;
use Yiisoft\Data\Reader\Filter\CompareFilter;
use Yiisoft\Data\Reader\Filter\Compare;
use Yiisoft\Data\Reader\Filter\GreaterThan;
use Yiisoft\Data\Reader\Filter\GreaterThanOrEqual;
use Yiisoft\Data\Reader\Filter\LessThan;
Expand Down Expand Up @@ -297,15 +297,15 @@ private function getValueFromItem(array|object $item, string $field): mixed
return ArrayHelper::getValue($item, $field);
}

private function getFilter(Sort $sort): CompareFilter
private function getFilter(Sort $sort): Compare
{
$value = $this->getValue();
/** @var string $field */
[$field, $sorting] = $this->getFieldAndSortingFromSort($sort);
return $sorting === 'asc' ? new GreaterThan($field, $value) : new LessThan($field, $value);
}

private function getReverseFilter(Sort $sort): CompareFilter
private function getReverseFilter(Sort $sort): Compare
{
$value = $this->getValue();
/** @var string $field */
Expand Down
2 changes: 1 addition & 1 deletion src/Reader/Filter/All.php
Expand Up @@ -4,7 +4,7 @@

namespace Yiisoft\Data\Reader\Filter;

final class All extends GroupFilter
final class All extends Group
{
public static function getOperator(): string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Reader/Filter/Any.php
Expand Up @@ -4,7 +4,7 @@

namespace Yiisoft\Data\Reader\Filter;

final class Any extends GroupFilter
final class Any extends Group
{
public static function getOperator(): string
{
Expand Down
Expand Up @@ -7,7 +7,7 @@
use DateTimeInterface;
use Yiisoft\Data\Reader\FilterDataValidationHelper;

abstract class CompareFilter implements FilterInterface
abstract class Compare implements FilterInterface
{
private bool|\DateTimeInterface|float|int|string $value;

Expand Down
2 changes: 1 addition & 1 deletion src/Reader/Filter/Equals.php
Expand Up @@ -4,7 +4,7 @@

namespace Yiisoft\Data\Reader\Filter;

final class Equals extends CompareFilter
final class Equals extends Compare
{
public static function getOperator(): string
{
Expand Down
Expand Up @@ -4,7 +4,7 @@

namespace Yiisoft\Data\Reader\Filter;

interface FilterProcessorInterface
interface FilterHandlerInterface
{
public function getOperator(): string;
}
2 changes: 1 addition & 1 deletion src/Reader/Filter/GreaterThan.php
Expand Up @@ -4,7 +4,7 @@

namespace Yiisoft\Data\Reader\Filter;

final class GreaterThan extends CompareFilter
final class GreaterThan extends Compare
{
public static function getOperator(): string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Reader/Filter/GreaterThanOrEqual.php
Expand Up @@ -4,7 +4,7 @@

namespace Yiisoft\Data\Reader\Filter;

final class GreaterThanOrEqual extends CompareFilter
final class GreaterThanOrEqual extends Compare
{
public static function getOperator(): string
{
Expand Down
Expand Up @@ -11,7 +11,7 @@
use function is_string;
use function sprintf;

abstract class GroupFilter implements FilterInterface
abstract class Group implements FilterInterface
{
/**
* @var array[]|FilterInterface[]
Expand Down
2 changes: 1 addition & 1 deletion src/Reader/Filter/LessThan.php
Expand Up @@ -4,7 +4,7 @@

namespace Yiisoft\Data\Reader\Filter;

final class LessThan extends CompareFilter
final class LessThan extends Compare
{
public static function getOperator(): string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Reader/Filter/LessThanOrEqual.php
Expand Up @@ -4,7 +4,7 @@

namespace Yiisoft\Data\Reader\Filter;

final class LessThanOrEqual extends CompareFilter
final class LessThanOrEqual extends Compare
{
public static function getOperator(): string
{
Expand Down
12 changes: 6 additions & 6 deletions src/Reader/FilterDataValidationHelper.php
Expand Up @@ -6,7 +6,7 @@

use DateTimeInterface;
use InvalidArgumentException;
use Yiisoft\Data\Reader\Iterable\Processor\IterableProcessorInterface;
use Yiisoft\Data\Reader\Iterable\Handler\IterableHandlerInterface;

use function is_scalar;
use function is_string;
Expand All @@ -24,13 +24,13 @@ public static function assertFieldIsString(mixed $field): void
}
}

public static function assertFilterProcessorIsIterable(mixed $filterProcessor): void
public static function assertFilterHandlerIsIterable(mixed $filterHandler): void
{
if (!$filterProcessor instanceof IterableProcessorInterface) {
if (!$filterHandler instanceof IterableHandlerInterface) {
throw new InvalidArgumentException(sprintf(
'The filter processor should be an object and implement "%s". The %s is received.',
IterableProcessorInterface::class,
self::getValueType($filterProcessor),
'The filter handler should be an object and implement "%s". The %s is received.',
IterableHandlerInterface::class,
self::getValueType($filterHandler),
));
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Reader/FilterableDataInterface.php
Expand Up @@ -5,11 +5,11 @@
namespace Yiisoft\Data\Reader;

use Yiisoft\Data\Reader\Filter\FilterInterface;
use Yiisoft\Data\Reader\Filter\FilterProcessorInterface;
use Yiisoft\Data\Reader\Filter\FilterHandlerInterface;

interface FilterableDataInterface
{
public function withFilter(FilterInterface $filter): static;

public function withFilterProcessors(FilterProcessorInterface ...$filterProcessors): static;
public function withFilterHandlers(FilterHandlerInterface ...$filterHandlers): static;
}
Expand Up @@ -2,11 +2,11 @@

declare(strict_types=1);

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

use function in_array;

final class All extends GroupProcessor
final class All extends Group
{
public function getOperator(): string
{
Expand Down
Expand Up @@ -2,11 +2,11 @@

declare(strict_types=1);

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

use function in_array;

final class Any extends GroupProcessor
final class Any extends Group
{
public function getOperator(): string
{
Expand Down
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

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

use DateTimeInterface;
use InvalidArgumentException;
Expand All @@ -11,14 +11,14 @@

use function count;

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

public function match(array|object $item, array $arguments, array $filterProcessors): bool
public function match(array|object $item, array $arguments, array $filterHandlers): bool
{
if (count($arguments) !== 3) {
throw new InvalidArgumentException('$arguments should contain exactly three elements.');
Expand Down
Expand Up @@ -2,23 +2,19 @@

declare(strict_types=1);

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

use InvalidArgumentException;
use Yiisoft\Arrays\ArrayHelper;
use Yiisoft\Data\Reader\FilterDataValidationHelper;

use function count;

abstract class CompareProcessor implements IterableProcessorInterface
abstract class Compare implements IterableHandlerInterface
{
/**
* @param mixed $itemValue
* @param mixed $argumentValue
*/
abstract protected function compare($itemValue, $argumentValue): bool;
abstract protected function compare(mixed $itemValue, mixed $argumentValue): bool;

public function match(array|object $item, array $arguments, array $filterProcessors): bool
public function match(array|object $item, array $arguments, array $filterHandlers): bool
{
if (count($arguments) !== 2) {
throw new InvalidArgumentException('$arguments should contain exactly two elements.');
Expand Down
Expand Up @@ -2,18 +2,18 @@

declare(strict_types=1);

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

use DateTimeInterface;

final class Equals extends CompareProcessor
final class Equals extends Compare
{
public function getOperator(): string
{
return \Yiisoft\Data\Reader\Filter\Equals::getOperator();
}

protected function compare($itemValue, $argumentValue): bool
protected function compare(mixed $itemValue, mixed $argumentValue): bool
{
if (!$itemValue instanceof DateTimeInterface) {
return $itemValue == $argumentValue;
Expand Down
Expand Up @@ -2,21 +2,21 @@

declare(strict_types=1);

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

use InvalidArgumentException;
use Yiisoft\Data\Reader\FilterDataValidationHelper;

use function count;

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

public function match(array|object $item, array $arguments, array $filterProcessors): bool
public function match(array|object $item, array $arguments, array $filterHandlers): bool
{
if (count($arguments) !== 1) {
throw new InvalidArgumentException('$arguments should contain exactly one element.');
Expand Down
Expand Up @@ -2,22 +2,22 @@

declare(strict_types=1);

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

use InvalidArgumentException;
use Yiisoft\Arrays\ArrayHelper;
use Yiisoft\Data\Reader\FilterDataValidationHelper;

use function count;

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

public function match(array|object $item, array $arguments, array $filterProcessors): bool
public function match(array|object $item, array $arguments, array $filterHandlers): bool
{
if (count($arguments) !== 1) {
throw new InvalidArgumentException('$arguments should contain exactly one element.');
Expand Down
Expand Up @@ -2,18 +2,18 @@

declare(strict_types=1);

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

use DateTimeInterface;

final class GreaterThan extends CompareProcessor
final class GreaterThan extends Compare
{
public function getOperator(): string
{
return \Yiisoft\Data\Reader\Filter\GreaterThan::getOperator();
}

protected function compare($itemValue, $argumentValue): bool
protected function compare(mixed $itemValue, mixed $argumentValue): bool
{
if (!$itemValue instanceof DateTimeInterface) {
return $itemValue > $argumentValue;
Expand Down

0 comments on commit 1f27ac5

Please sign in to comment.