Skip to content

Commit a4d7079

Browse files
authored
Refactor filters (#226)
1 parent 8490a47 commit a4d7079

20 files changed

+53
-141
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
- Chg #224: Change `$iterableFilterHandlers` to context object in `IterableFilterHandlerInterface::match()` (@vjik)
5757
- New #224: Add filtering by nested values support in `IterableDataReader` (@vjik)
5858
- Chg #225: Rename classes: `All` to `AndX`, `Any` to `OrX`. Remove `Group` class (@vjik)
59+
- Chg #226: Refactor filter classes to use readonly properties instead of getters (@vjik)
5960

6061
## 1.0.1 January 25, 2023
6162

src/Reader/Filter/Between.php

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,9 @@ final class Between implements FilterInterface
1919
* @param bool|DateTimeInterface|float|int|string $maxValue Maximal field value.
2020
*/
2121
public function __construct(
22-
private readonly string $field,
23-
private readonly bool|DateTimeInterface|float|int|string $minValue,
24-
private readonly bool|DateTimeInterface|float|int|string $maxValue
22+
public readonly string $field,
23+
public readonly bool|DateTimeInterface|float|int|string $minValue,
24+
public readonly bool|DateTimeInterface|float|int|string $maxValue
2525
) {
2626
}
27-
28-
public function getField(): string
29-
{
30-
return $this->field;
31-
}
32-
33-
public function getMinValue(): float|DateTimeInterface|bool|int|string
34-
{
35-
return $this->minValue;
36-
}
37-
38-
public function getMaxValue(): float|DateTimeInterface|bool|int|string
39-
{
40-
return $this->maxValue;
41-
}
4227
}

src/Reader/Filter/Compare.php

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,17 @@ abstract class Compare implements FilterInterface
1616
* @param string $field Name of the field to compare.
1717
* @param bool|DateTimeInterface|float|int|string $value Value to compare to.
1818
*/
19-
public function __construct(
20-
private readonly string $field,
21-
private bool|DateTimeInterface|float|int|string $value,
19+
final public function __construct(
20+
public readonly string $field,
21+
public readonly bool|DateTimeInterface|float|int|string $value,
2222
) {
2323
}
2424

25-
public function getField(): string
26-
{
27-
return $this->field;
28-
}
29-
30-
public function getValue(): float|DateTimeInterface|bool|int|string
31-
{
32-
return $this->value;
33-
}
34-
3525
/**
3626
* @param bool|DateTimeInterface|float|int|string $value Value to compare to.
3727
*/
3828
final public function withValue(bool|DateTimeInterface|float|int|string $value): static
3929
{
40-
$new = clone $this;
41-
$new->value = $value;
42-
return $new;
30+
return new static($this->field, $value);
4331
}
4432
}

src/Reader/Filter/EqualsNull.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,7 @@ final class EqualsNull implements FilterInterface
1515
* @param string $field Name of the field to check.
1616
*/
1717
public function __construct(
18-
private readonly string $field,
18+
public readonly string $field,
1919
) {
2020
}
21-
22-
public function getField(): string
23-
{
24-
return $this->field;
25-
}
2621
}

src/Reader/Filter/In.php

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,21 @@
1515
*/
1616
final class In implements FilterInterface
1717
{
18-
/**
19-
* @var bool[]|float[]|int[]|string[] Values to check against.
20-
*/
21-
private array $values;
22-
2318
/**
2419
* @param string $field Name of the field to compare.
2520
* @param bool[]|float[]|int[]|string[] $values Values to check against.
2621
*/
2722
public function __construct(
28-
private readonly string $field,
29-
array $values
23+
public readonly string $field,
24+
/** @var bool[]|float[]|int[]|string[] Values to check against. */
25+
public readonly array $values
3026
) {
27+
$this->assertValues($values);
28+
}
29+
30+
private function assertValues(array $values): void
31+
{
3132
foreach ($values as $value) {
32-
/** @psalm-suppress DocblockTypeContradiction */
3333
if (!is_scalar($value)) {
3434
throw new InvalidArgumentException(
3535
sprintf(
@@ -39,19 +39,5 @@ public function __construct(
3939
);
4040
}
4141
}
42-
$this->values = $values;
43-
}
44-
45-
public function getField(): string
46-
{
47-
return $this->field;
48-
}
49-
50-
/**
51-
* @return bool[]|float[]|int[]|string[]
52-
*/
53-
public function getValues(): array
54-
{
55-
return $this->values;
5642
}
5743
}

src/Reader/Filter/Like.php

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,9 @@ final class Like implements FilterInterface
2121
* - `false` - case-insensitive.
2222
*/
2323
public function __construct(
24-
private readonly string $field,
25-
private readonly string $value,
26-
private readonly ?bool $caseSensitive = null,
24+
public readonly string $field,
25+
public readonly string $value,
26+
public readonly ?bool $caseSensitive = null,
2727
) {
2828
}
29-
30-
public function getField(): string
31-
{
32-
return $this->field;
33-
}
34-
35-
public function getValue(): string
36-
{
37-
return $this->value;
38-
}
39-
40-
public function getCaseSensitive(): ?bool
41-
{
42-
return $this->caseSensitive;
43-
}
4429
}

src/Reader/Filter/Not.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,7 @@ final class Not implements FilterInterface
1515
* @param FilterInterface $filter Filter to negate.
1616
*/
1717
public function __construct(
18-
private readonly FilterInterface $filter,
18+
public readonly FilterInterface $filter,
1919
) {
2020
}
21-
22-
public function getFilter(): FilterInterface
23-
{
24-
return $this->filter;
25-
}
2621
}

src/Reader/Iterable/FilterHandler/BetweenHandler.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ public function match(object|array $item, FilterInterface $filter, Context $cont
2525
{
2626
/** @var Between $filter */
2727

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

3232
if (!$value instanceof DateTimeInterface) {
3333
return $value >= $min && $value <= $max;

src/Reader/Iterable/FilterHandler/EqualsHandler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ public function match(object|array $item, FilterInterface $filter, Context $cont
2424
{
2525
/** @var Equals $filter */
2626

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

3030
if (!$itemValue instanceof DateTimeInterface) {
3131
return $itemValue == $argumentValue;

src/Reader/Iterable/FilterHandler/EqualsNullHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ public function match(object|array $item, FilterInterface $filter, Context $cont
2323
{
2424
/** @var EqualsNull $filter */
2525

26-
return $context->readValue($item, $filter->getField()) === null;
26+
return $context->readValue($item, $filter->field) === null;
2727
}
2828
}

0 commit comments

Comments
 (0)