Skip to content

Commit

Permalink
Replace usages of AbstractFilter with the FilterInterface and mark th…
Browse files Browse the repository at this point in the history
…e filter as readonly
  • Loading branch information
marcelthole committed Dec 27, 2024
1 parent f7c2743 commit 182f4e4
Show file tree
Hide file tree
Showing 23 changed files with 482 additions and 2,211 deletions.
59 changes: 16 additions & 43 deletions src/AbstractDateDropdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,31 @@
use function array_reduce;
use function count;
use function is_array;
use function is_iterable;
use function ksort;
use function sprintf;
use function vsprintf;

/**
* @psalm-type Options = array{
* null_on_empty?: bool,
* null_on_all_empty?: bool,
* ...
* null_on_all_empty?: bool
* }
* @psalm-type InputArray = array<string, string>
* @template TOptions of Options
* @template-extends AbstractFilter<TOptions>
* @template-implements FilterInterface<string|null>
* @template TInput of array<array-key, numeric>
*/
abstract class AbstractDateDropdown extends AbstractFilter
abstract class AbstractDateDropdown implements FilterInterface
{
/**
* If true, the filter will return null if any date field is empty
*/
protected bool $nullOnEmpty = false;
protected readonly bool $nullOnEmpty;

/**
* If true, the filter will return null if all date fields are empty
*/
protected bool $nullOnAllEmpty = false;
protected readonly bool $nullOnAllEmpty;

/**
* Sprintf format string to use for formatting the date, fields will be used in alphabetical order.
Expand All @@ -43,42 +41,12 @@ abstract class AbstractDateDropdown extends AbstractFilter
protected int $expectedInputs = 0;

/**
* @param mixed $options If array or Traversable, passes value to
* setOptions().
* @param Options $options
*/
public function __construct(mixed $options = null)
public function __construct(array $options = [])
{
if (is_iterable($options)) {
$this->setOptions($options);
}
}

/**
* @return $this
*/
public function setNullOnAllEmpty(bool $nullOnAllEmpty): self
{
$this->nullOnAllEmpty = $nullOnAllEmpty;
return $this;
}

public function isNullOnAllEmpty(): bool
{
return $this->nullOnAllEmpty;
}

/**
* @return $this
*/
public function setNullOnEmpty(bool $nullOnEmpty): self
{
$this->nullOnEmpty = $nullOnEmpty;
return $this;
}

public function isNullOnEmpty(): bool
{
return $this->nullOnEmpty;
$this->nullOnEmpty = $options['null_on_empty'] ?? false;
$this->nullOnAllEmpty = $options['null_on_all_empty'] ?? false;
}

/**
Expand All @@ -97,14 +65,14 @@ public function filter(mixed $value): mixed

// Convert the date to a specific format
if (
$this->isNullOnEmpty()
$this->nullOnEmpty
&& array_reduce($value, self::reduce(...), false)
) {
return null;
}

if (
$this->isNullOnAllEmpty()
$this->nullOnAllEmpty
&& array_reduce($value, self::reduce(...), true)
) {
return null;
Expand All @@ -118,6 +86,11 @@ public function filter(mixed $value): mixed
return vsprintf($this->format, $value);
}

public function __invoke(mixed $value): mixed
{
return $this->filter($value);
}

/**
* Ensures there are enough inputs in the array to properly format the date.
*
Expand Down
122 changes: 0 additions & 122 deletions src/AbstractFilter.php

This file was deleted.

1 change: 0 additions & 1 deletion src/DateSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
* @psalm-type Options = array{
* null_on_empty?: bool,
* null_on_all_empty?: bool,
* ...
* }
* @psalm-type InputArray = array{
* year: numeric,
Expand Down
5 changes: 2 additions & 3 deletions src/DateTimeSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
* @psalm-type Options = array{
* null_on_empty?: bool,
* null_on_all_empty?: bool,
* ...
* }
* @psalm-type InputArray = array{
* year: numeric,
Expand Down Expand Up @@ -42,7 +41,7 @@ public function filter(mixed $value): mixed
}

if (
$this->isNullOnEmpty()
$this->nullOnEmpty
&& (
empty($value['year'])
|| empty($value['month'])
Expand All @@ -56,7 +55,7 @@ public function filter(mixed $value): mixed
}

if (
$this->isNullOnAllEmpty()
$this->nullOnAllEmpty
&& (
empty($value['year'])
&& empty($value['month'])
Expand Down
11 changes: 8 additions & 3 deletions src/Dir.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@

/**
* @psalm-type Options = array{}
* @extends AbstractFilter<Options>
* @implements FilterInterface<string>
*/
final class Dir extends AbstractFilter
final class Dir implements FilterInterface
{
/**
* Defined by Laminas\Filter\FilterInterface
*
* Returns dirname($value)
*
* @psalm-return ($value is scalar ? string : mixed)
* @psalm-return string
*/
public function filter(mixed $value): mixed
{
Expand All @@ -29,4 +29,9 @@ public function filter(mixed $value): mixed

return dirname($value);
}

public function __invoke(mixed $value): mixed
{
return $this->filter($value);
}
}
Loading

0 comments on commit 182f4e4

Please sign in to comment.