Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
- Chg #224: Change `$iterableFilterHandlers` to context object in `IterableFilterHandlerInterface::match()` (@vjik)
- New #224: Add filtering by nested values support in `IterableDataReader` (@vjik)
- Chg #225: Rename classes: `All` to `AndX`, `Any` to `OrX`. Remove `Group` class (@vjik)
- Chg #226: Refactor filter classes to use readonly properties instead of getters (@vjik)

## 1.0.1 January 25, 2023

Expand Down
21 changes: 3 additions & 18 deletions src/Reader/Filter/Between.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,9 @@ final class Between implements FilterInterface
* @param bool|DateTimeInterface|float|int|string $maxValue Maximal field value.
*/
public function __construct(
private readonly string $field,
private readonly bool|DateTimeInterface|float|int|string $minValue,
private readonly bool|DateTimeInterface|float|int|string $maxValue
public readonly string $field,
public readonly bool|DateTimeInterface|float|int|string $minValue,
public readonly bool|DateTimeInterface|float|int|string $maxValue
) {
}

public function getField(): string
{
return $this->field;
}

public function getMinValue(): float|DateTimeInterface|bool|int|string
{
return $this->minValue;
}

public function getMaxValue(): float|DateTimeInterface|bool|int|string
{
return $this->maxValue;
}
}
20 changes: 4 additions & 16 deletions src/Reader/Filter/Compare.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,17 @@ abstract class Compare implements FilterInterface
* @param string $field Name of the field to compare.
* @param bool|DateTimeInterface|float|int|string $value Value to compare to.
*/
public function __construct(
private readonly string $field,
private bool|DateTimeInterface|float|int|string $value,
final public function __construct(
public readonly string $field,
public readonly bool|DateTimeInterface|float|int|string $value,
) {
}

public function getField(): string
{
return $this->field;
}

public function getValue(): float|DateTimeInterface|bool|int|string
{
return $this->value;
}

/**
* @param bool|DateTimeInterface|float|int|string $value Value to compare to.
*/
final public function withValue(bool|DateTimeInterface|float|int|string $value): static
{
$new = clone $this;
$new->value = $value;
return $new;
return new static($this->field, $value);
}
}
7 changes: 1 addition & 6 deletions src/Reader/Filter/EqualsNull.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,7 @@ final class EqualsNull implements FilterInterface
* @param string $field Name of the field to check.
*/
public function __construct(
private readonly string $field,
public readonly string $field,
) {
}

public function getField(): string
{
return $this->field;
}
}
30 changes: 8 additions & 22 deletions src/Reader/Filter/In.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@
*/
final class In implements FilterInterface
{
/**
* @var bool[]|float[]|int[]|string[] Values to check against.
*/
private array $values;

/**
* @param string $field Name of the field to compare.
* @param bool[]|float[]|int[]|string[] $values Values to check against.
*/
public function __construct(
private readonly string $field,
array $values
public readonly string $field,
/** @var bool[]|float[]|int[]|string[] Values to check against. */
public readonly array $values
) {
$this->assertValues($values);
}

private function assertValues(array $values): void
{
foreach ($values as $value) {
/** @psalm-suppress DocblockTypeContradiction */
if (!is_scalar($value)) {
throw new InvalidArgumentException(
sprintf(
Expand All @@ -39,19 +39,5 @@ public function __construct(
);
}
}
$this->values = $values;
}

public function getField(): string
{
return $this->field;
}

/**
* @return bool[]|float[]|int[]|string[]
*/
public function getValues(): array
{
return $this->values;
}
}
21 changes: 3 additions & 18 deletions src/Reader/Filter/Like.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,9 @@ final class Like implements FilterInterface
* - `false` - case-insensitive.
*/
public function __construct(
private readonly string $field,
private readonly string $value,
private readonly ?bool $caseSensitive = null,
public readonly string $field,
public readonly string $value,
public readonly ?bool $caseSensitive = null,
) {
}

public function getField(): string
{
return $this->field;
}

public function getValue(): string
{
return $this->value;
}

public function getCaseSensitive(): ?bool
{
return $this->caseSensitive;
}
}
7 changes: 1 addition & 6 deletions src/Reader/Filter/Not.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,7 @@ final class Not implements FilterInterface
* @param FilterInterface $filter Filter to negate.
*/
public function __construct(
private readonly FilterInterface $filter,
public readonly FilterInterface $filter,
) {
}

public function getFilter(): FilterInterface
{
return $this->filter;
}
}
6 changes: 3 additions & 3 deletions src/Reader/Iterable/FilterHandler/BetweenHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public function match(object|array $item, FilterInterface $filter, Context $cont
{
/** @var Between $filter */

$value = $context->readValue($item, $filter->getField());
$min = $filter->getMinValue();
$max = $filter->getMaxValue();
$value = $context->readValue($item, $filter->field);
$min = $filter->minValue;
$max = $filter->maxValue;

if (!$value instanceof DateTimeInterface) {
return $value >= $min && $value <= $max;
Expand Down
4 changes: 2 additions & 2 deletions src/Reader/Iterable/FilterHandler/EqualsHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public function match(object|array $item, FilterInterface $filter, Context $cont
{
/** @var Equals $filter */

$itemValue = $context->readValue($item, $filter->getField());
$argumentValue = $filter->getValue();
$itemValue = $context->readValue($item, $filter->field);
$argumentValue = $filter->value;

if (!$itemValue instanceof DateTimeInterface) {
return $itemValue == $argumentValue;
Expand Down
2 changes: 1 addition & 1 deletion src/Reader/Iterable/FilterHandler/EqualsNullHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ public function match(object|array $item, FilterInterface $filter, Context $cont
{
/** @var EqualsNull $filter */

return $context->readValue($item, $filter->getField()) === null;
return $context->readValue($item, $filter->field) === null;
}
}
4 changes: 2 additions & 2 deletions src/Reader/Iterable/FilterHandler/GreaterThanHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public function match(object|array $item, FilterInterface $filter, Context $cont
{
/** @var GreaterThan $filter */

$itemValue = $context->readValue($item, $filter->getField());
$argumentValue = $filter->getValue();
$itemValue = $context->readValue($item, $filter->field);
$argumentValue = $filter->value;

if (!$itemValue instanceof DateTimeInterface) {
return $itemValue > $argumentValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public function match(object|array $item, FilterInterface $filter, Context $cont
{
/** @var GreaterThanOrEqual $filter */

$itemValue = $context->readValue($item, $filter->getField());
$argumentValue = $filter->getValue();
$itemValue = $context->readValue($item, $filter->field);
$argumentValue = $filter->value;

if (!$itemValue instanceof DateTimeInterface) {
return $itemValue >= $argumentValue;
Expand Down
4 changes: 2 additions & 2 deletions src/Reader/Iterable/FilterHandler/InHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public function match(object|array $item, FilterInterface $filter, Context $cont
{
/** @var In $filter */

$itemValue = $context->readValue($item, $filter->getField());
$argumentValue = $filter->getValues();
$itemValue = $context->readValue($item, $filter->field);
$argumentValue = $filter->values;

return in_array($itemValue, $argumentValue);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Reader/Iterable/FilterHandler/LessThanHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public function match(object|array $item, FilterInterface $filter, Context $cont
{
/** @var LessThan $filter */

$itemValue = $context->readValue($item, $filter->getField());
$argumentValue = $filter->getValue();
$itemValue = $context->readValue($item, $filter->field);
$argumentValue = $filter->value;

if (!$itemValue instanceof DateTimeInterface) {
return $itemValue < $argumentValue;
Expand Down
4 changes: 2 additions & 2 deletions src/Reader/Iterable/FilterHandler/LessThanOrEqualHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public function match(object|array $item, FilterInterface $filter, Context $cont
{
/** @var LessThanOrEqual $filter */

$itemValue = $context->readValue($item, $filter->getField());
$argumentValue = $filter->getValue();
$itemValue = $context->readValue($item, $filter->field);
$argumentValue = $filter->value;

if (!$itemValue instanceof DateTimeInterface) {
return $itemValue <= $argumentValue;
Expand Down
8 changes: 4 additions & 4 deletions src/Reader/Iterable/FilterHandler/LikeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ public function match(object|array $item, FilterInterface $filter, Context $cont
{
/** @var Like $filter */

$itemValue = $context->readValue($item, $filter->getField());
$itemValue = $context->readValue($item, $filter->field);
if (!is_string($itemValue)) {
return false;
}

return $filter->getCaseSensitive() === true
? str_contains($itemValue, $filter->getValue())
: mb_stripos($itemValue, $filter->getValue()) !== false;
return $filter->caseSensitive === true
? str_contains($itemValue, $filter->value)
: mb_stripos($itemValue, $filter->value) !== false;
}
}
2 changes: 1 addition & 1 deletion src/Reader/Iterable/FilterHandler/NotHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function match(object|array $item, FilterInterface $filter, Context $cont
{
/** @var Not $filter */

$subFilter = $filter->getFilter();
$subFilter = $filter->filter;

$filterHandler = $context->getFilterHandler($subFilter::class);
return !$filterHandler->match($item, $subFilter, $context);
Expand Down
14 changes: 10 additions & 4 deletions tests/Reader/Filter/CompareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@
use PHPUnit\Framework\TestCase;
use Yiisoft\Data\Reader\Filter\LessThan;

use function PHPUnit\Framework\assertNotSame;
use function PHPUnit\Framework\assertSame;

final class CompareTest extends TestCase
{
public function testWithValue()
public function testWithValue(): void
{
$filter = new LessThan('field', 1);
$sourceFilter = new LessThan('field', 1);

$filter = $sourceFilter->withValue(2);

$this->assertNotSame($filter, $filter->withValue(1));
$this->assertSame(2, $filter->withValue(2)->getValue());
assertNotSame($sourceFilter, $filter);
assertSame('field', $filter->field);
assertSame(2, $filter->value);
}
}
29 changes: 0 additions & 29 deletions tests/Reader/Filter/LikeTest.php

This file was deleted.

2 changes: 1 addition & 1 deletion tests/Reader/Iterable/IterableDataReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ public function match(
Context $context,
): bool {
/** @var Equals $filter */
return $item[$filter->getField()] === 2;
return $item[$filter->field] === 2;
}
}
);
Expand Down
Loading