Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
18 / 18 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
| FilterableTrait | |
100.00% |
18 / 18 |
|
100.00% |
6 / 6 |
11 | |
100.00% |
1 / 1 |
| addFilter | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
4 | |||
| addFilters | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| hasFilters | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getFilters | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| clearFilters | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| filterAll | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| filter | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| 1 | <?php |
| 2 | /** |
| 3 | * Pop PHP Framework (https://www.popphp.org/) |
| 4 | * |
| 5 | * @link https://github.com/popphp/popphp-framework |
| 6 | * @author Nick Sagona, III <dev@noladev.com> |
| 7 | * @copyright Copyright (c) 2009-2025 NOLA Interactive, LLC. |
| 8 | * @license https://www.popphp.org/license New BSD License |
| 9 | */ |
| 10 | |
| 11 | /** |
| 12 | * @namespace |
| 13 | */ |
| 14 | namespace Pop\Filter; |
| 15 | |
| 16 | use InvalidArgumentException; |
| 17 | |
| 18 | /** |
| 19 | * Filterable trait |
| 20 | * |
| 21 | * @category Pop |
| 22 | * @package Pop\Filter |
| 23 | * @author Nick Sagona, III <dev@noladev.com> |
| 24 | * @copyright Copyright (c) 2009-2025 NOLA Interactive, LLC. |
| 25 | * @license https://www.popphp.org/license New BSD License |
| 26 | * @version 4.0.2 |
| 27 | */ |
| 28 | trait FilterableTrait |
| 29 | { |
| 30 | |
| 31 | /** |
| 32 | * Form filters |
| 33 | * @var array |
| 34 | */ |
| 35 | protected array $filters = []; |
| 36 | |
| 37 | /** |
| 38 | * Add filter |
| 39 | * |
| 40 | * @param mixed $filter |
| 41 | * @throws InvalidArgumentException |
| 42 | * @return static |
| 43 | */ |
| 44 | public function addFilter(mixed $filter): static |
| 45 | { |
| 46 | if (!($filter instanceof FilterInterface) && is_callable($filter)) { |
| 47 | $filter = new Filter($filter); |
| 48 | } |
| 49 | if (!($filter instanceof FilterInterface)) { |
| 50 | throw new \InvalidArgumentException( |
| 51 | 'Error: The filter must be a callable or an instance of Pop\Filter\FilterInterface.' |
| 52 | ); |
| 53 | } |
| 54 | $this->filters[] = $filter; |
| 55 | return $this; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Add filters |
| 60 | * |
| 61 | * @param array $filters |
| 62 | * @return static |
| 63 | */ |
| 64 | public function addFilters(array $filters): static |
| 65 | { |
| 66 | foreach ($filters as $filter) { |
| 67 | $this->addFilter($filter); |
| 68 | } |
| 69 | return $this; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Has filters |
| 74 | * |
| 75 | * @return bool |
| 76 | */ |
| 77 | public function hasFilters(): bool |
| 78 | { |
| 79 | return (count($this->filters) > 0); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Get filters |
| 84 | * |
| 85 | * @return array |
| 86 | */ |
| 87 | public function getFilters(): array |
| 88 | { |
| 89 | return $this->filters; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Clear filters |
| 94 | * |
| 95 | * @return static |
| 96 | */ |
| 97 | public function clearFilters(): static |
| 98 | { |
| 99 | $this->filters = []; |
| 100 | return $this; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Filter all values, ignoring excludes |
| 105 | * |
| 106 | * @param array $values |
| 107 | * @return array |
| 108 | */ |
| 109 | public function filterAll(array $values): array |
| 110 | { |
| 111 | foreach ($this->filters as $filter) { |
| 112 | $values = array_map([$filter, 'filter'], $values); |
| 113 | } |
| 114 | |
| 115 | return $values; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Filter values |
| 120 | * |
| 121 | * @param mixed $values |
| 122 | * @return mixed |
| 123 | */ |
| 124 | abstract public function filter(mixed $values): mixed; |
| 125 | |
| 126 | } |