Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
28 / 28 |
|
100.00% |
9 / 9 |
CRAP | |
100.00% |
1 / 1 |
| FilterableTrait | |
100.00% |
28 / 28 |
|
100.00% |
9 / 9 |
16 | |
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 | |||
| hasFilter | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| removeFilter | |
100.00% |
5 / 5 |
|
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 | |||
| filterEach | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| filter | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | /** |
| 4 | * Pop PHP Framework (https://www.popphp.org/) |
| 5 | * |
| 6 | * @link https://github.com/popphp/popphp-framework |
| 7 | * @author Nick Sagona, III <nick@popphp.org> |
| 8 | * @copyright Copyright (c) 2009-2026 Nick Sagona, III |
| 9 | * @license https://www.popphp.org/license New BSD License |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * @namespace |
| 14 | */ |
| 15 | namespace Pop\Filter; |
| 16 | |
| 17 | use InvalidArgumentException; |
| 18 | |
| 19 | /** |
| 20 | * Filterable trait |
| 21 | * |
| 22 | * @category Pop |
| 23 | * @package Pop\Filter |
| 24 | * @author Nick Sagona, III <nick@popphp.org> |
| 25 | * @copyright Copyright (c) 2009-2026 Nick Sagona, III |
| 26 | * @license https://www.popphp.org/license New BSD License |
| 27 | * @version 5.0.0 |
| 28 | */ |
| 29 | trait FilterableTrait |
| 30 | { |
| 31 | |
| 32 | /** |
| 33 | * Form filters |
| 34 | * @var array |
| 35 | */ |
| 36 | protected array $filters = []; |
| 37 | |
| 38 | /** |
| 39 | * Add filter |
| 40 | * |
| 41 | * @param mixed $filter |
| 42 | * @throws InvalidArgumentException |
| 43 | * @return static |
| 44 | */ |
| 45 | public function addFilter(mixed $filter): static |
| 46 | { |
| 47 | if (!($filter instanceof FilterInterface) && is_callable($filter)) { |
| 48 | $filter = new Filter($filter); |
| 49 | } |
| 50 | if (!($filter instanceof FilterInterface)) { |
| 51 | throw new \InvalidArgumentException( |
| 52 | 'Error: The filter must be a callable or an instance of Pop\Filter\FilterInterface.' |
| 53 | ); |
| 54 | } |
| 55 | $this->filters[] = $filter; |
| 56 | return $this; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Add filters |
| 61 | * |
| 62 | * @param array $filters |
| 63 | * @return static |
| 64 | */ |
| 65 | public function addFilters(array $filters): static |
| 66 | { |
| 67 | foreach ($filters as $filter) { |
| 68 | $this->addFilter($filter); |
| 69 | } |
| 70 | return $this; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Has filters |
| 75 | * |
| 76 | * @return bool |
| 77 | */ |
| 78 | public function hasFilters(): bool |
| 79 | { |
| 80 | return (count($this->filters) > 0); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Has filter |
| 85 | * |
| 86 | * @param FilterInterface $filter |
| 87 | * @return bool |
| 88 | */ |
| 89 | public function hasFilter(FilterInterface $filter): bool |
| 90 | { |
| 91 | return in_array($filter, $this->filters, true); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Remove filter |
| 96 | * |
| 97 | * @param FilterInterface $filter |
| 98 | * @return static |
| 99 | */ |
| 100 | public function removeFilter(FilterInterface $filter): static |
| 101 | { |
| 102 | $this->filters = array_values(array_filter( |
| 103 | $this->filters, |
| 104 | fn($f) => $f !== $filter |
| 105 | )); |
| 106 | |
| 107 | return $this; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Get filters |
| 112 | * |
| 113 | * @return array |
| 114 | */ |
| 115 | public function getFilters(): array |
| 116 | { |
| 117 | return $this->filters; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Clear filters |
| 122 | * |
| 123 | * @return static |
| 124 | */ |
| 125 | public function clearFilters(): static |
| 126 | { |
| 127 | $this->filters = []; |
| 128 | return $this; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Filter all values, ignoring excludes |
| 133 | * |
| 134 | * @param array $values |
| 135 | * @return array |
| 136 | */ |
| 137 | public function filterAll(array $values): array |
| 138 | { |
| 139 | foreach ($this->filters as $filter) { |
| 140 | $values = array_map([$filter, 'filter'], $values); |
| 141 | } |
| 142 | |
| 143 | return $values; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Filter each value, keyed by name, honoring each filter's excludeByName rules |
| 148 | * |
| 149 | * @param array $values |
| 150 | * @return array |
| 151 | */ |
| 152 | public function filterEach(array $values): array |
| 153 | { |
| 154 | foreach ($this->filters as $filter) { |
| 155 | foreach ($values as $key => $value) { |
| 156 | $values[$key] = $filter->filter($value, (string)$key); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | return $values; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Filter values |
| 165 | * |
| 166 | * @param mixed $values |
| 167 | * @return mixed |
| 168 | */ |
| 169 | abstract public function filter(mixed $values): mixed; |
| 170 | |
| 171 | } |