Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
46 / 46 |
|
100.00% |
14 / 14 |
CRAP | |
100.00% |
1 / 1 |
AbstractFilter | |
100.00% |
46 / 46 |
|
100.00% |
14 / 14 |
27 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
4 | |||
setCallable | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
setParams | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
setExcludeByName | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
setExcludeByType | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
getCallable | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getParams | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getExcludeByName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getExcludeByType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasCallable | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasParams | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasExcludeByName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasExcludeByType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
filter | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
7 |
1 | <?php |
2 | /** |
3 | * Pop PHP Framework (http://www.popphp.org/) |
4 | * |
5 | * @link https://github.com/popphp/popphp-framework |
6 | * @author Nick Sagona, III <dev@nolainteractive.com> |
7 | * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
8 | * @license http://www.popphp.org/license New BSD License |
9 | */ |
10 | |
11 | /** |
12 | * @namespace |
13 | */ |
14 | namespace Pop\Filter; |
15 | |
16 | use Pop\Utils\CallableObject; |
17 | |
18 | /** |
19 | * Abstract filter class |
20 | * |
21 | * @category Pop |
22 | * @package Pop\Filter |
23 | * @author Nick Sagona, III <dev@nolainteractive.com> |
24 | * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
25 | * @license http://www.popphp.org/license New BSD License |
26 | * @version 4.0.0 |
27 | */ |
28 | abstract class AbstractFilter implements FilterInterface |
29 | { |
30 | |
31 | /** |
32 | * Filter callable |
33 | * @var CallableObject |
34 | */ |
35 | protected ?CallableObject $callable = null; |
36 | |
37 | /** |
38 | * Exclude by type |
39 | * @var array |
40 | */ |
41 | protected array $excludeByType = []; |
42 | |
43 | /** |
44 | * Exclude by name |
45 | * @var array |
46 | */ |
47 | protected array $excludeByName = []; |
48 | |
49 | /** |
50 | * Constructor |
51 | * |
52 | * Instantiate the form filter object |
53 | * |
54 | * @param mixed $callable |
55 | * @param mixed $params |
56 | * @param mixed $excludeByName |
57 | * @param mixed $excludeByType |
58 | */ |
59 | public function __construct(mixed $callable, mixed $params = null, mixed $excludeByName = null, mixed $excludeByType = null) |
60 | { |
61 | $this->setCallable($callable); |
62 | |
63 | if ($params !== null) { |
64 | $this->setParams($params); |
65 | } |
66 | if ($excludeByName !== null) { |
67 | $this->setExcludeByName($excludeByName); |
68 | } |
69 | if ($excludeByType !== null) { |
70 | $this->setExcludeByType($excludeByType); |
71 | } |
72 | } |
73 | |
74 | /** |
75 | * Set callable |
76 | * |
77 | * @param mixed $callable |
78 | * @return AbstractFilter |
79 | */ |
80 | public function setCallable(mixed $callable): AbstractFilter |
81 | { |
82 | if (!($callable instanceof CallableObject)) { |
83 | $callable = new CallableObject($callable); |
84 | } |
85 | $this->callable = $callable; |
86 | return $this; |
87 | } |
88 | |
89 | /** |
90 | * Set params |
91 | * |
92 | * @param mixed $params |
93 | * @return AbstractFilter |
94 | */ |
95 | public function setParams(mixed $params): AbstractFilter |
96 | { |
97 | if (is_array($params)) { |
98 | $this->callable->setParameters($params); |
99 | } else { |
100 | $this->callable->setParameters([$params]); |
101 | } |
102 | |
103 | return $this; |
104 | } |
105 | |
106 | /** |
107 | * Set exclude by name |
108 | * |
109 | * @param mixed $excludeByName |
110 | * @return AbstractFilter |
111 | */ |
112 | public function setExcludeByName(mixed $excludeByName): AbstractFilter |
113 | { |
114 | if (!is_array($excludeByName)) { |
115 | $excludeByName = [$excludeByName]; |
116 | } |
117 | $this->excludeByName = $excludeByName; |
118 | |
119 | return $this; |
120 | } |
121 | |
122 | /** |
123 | * Set exclude by type |
124 | * |
125 | * @param mixed $excludeByType |
126 | * @return AbstractFilter |
127 | */ |
128 | public function setExcludeByType(mixed $excludeByType): AbstractFilter |
129 | { |
130 | if (!is_array($excludeByType)) { |
131 | $excludeByType = [$excludeByType]; |
132 | } |
133 | $this->excludeByType = $excludeByType; |
134 | |
135 | return $this; |
136 | } |
137 | |
138 | /** |
139 | * Get callable |
140 | * |
141 | * @return CallableObject |
142 | */ |
143 | public function getCallable(): CallableObject |
144 | { |
145 | return $this->callable; |
146 | } |
147 | |
148 | /** |
149 | * Get params |
150 | * |
151 | * @return array |
152 | */ |
153 | public function getParams(): array |
154 | { |
155 | return $this->callable->getParameters(); |
156 | } |
157 | |
158 | /** |
159 | * Get exclude by name |
160 | * |
161 | * @return array |
162 | */ |
163 | public function getExcludeByName(): array |
164 | { |
165 | return $this->excludeByName; |
166 | } |
167 | |
168 | /** |
169 | * Get exclude by type |
170 | * |
171 | * @return array |
172 | */ |
173 | public function getExcludeByType(): array |
174 | { |
175 | return $this->excludeByType; |
176 | } |
177 | |
178 | /** |
179 | * Has callable |
180 | * |
181 | * @return bool |
182 | */ |
183 | public function hasCallable(): bool |
184 | { |
185 | return ($this->callable !== null); |
186 | } |
187 | |
188 | /** |
189 | * Has params |
190 | * |
191 | * @return bool |
192 | */ |
193 | public function hasParams(): bool |
194 | { |
195 | return $this->callable->hasParameters(); |
196 | } |
197 | |
198 | /** |
199 | * Has exclude by name |
200 | * |
201 | * @return bool |
202 | */ |
203 | public function hasExcludeByName(): bool |
204 | { |
205 | return (!empty($this->excludeByName)); |
206 | } |
207 | |
208 | /** |
209 | * Has exclude by type |
210 | * |
211 | * @return bool |
212 | */ |
213 | public function hasExcludeByType(): bool |
214 | { |
215 | return (!empty($this->excludeByType)); |
216 | } |
217 | |
218 | /** |
219 | * Filter value |
220 | * |
221 | * @param mixed $value |
222 | * @param ?string $name |
223 | * @param mixed $type |
224 | * @return mixed |
225 | */ |
226 | public function filter(mixed $value, ?string $name = null, mixed $type = null): mixed |
227 | { |
228 | if ((($type === null) || (!in_array($type, $this->excludeByType))) && |
229 | (($name === null) || (!in_array($name, $this->excludeByName)))) { |
230 | if (is_array($value)) { |
231 | foreach ($value as $k => $v) { |
232 | $callableParams = $this->callable->getParameters(); |
233 | $params = array_merge([$v], $callableParams); |
234 | $this->callable->setParameters($params); |
235 | |
236 | $value[$k] = $this->callable->call(); |
237 | |
238 | $this->callable->setParameters($callableParams); |
239 | } |
240 | } else { |
241 | $callableParams = $this->callable->getParameters(); |
242 | $params = array_merge([$value], $callableParams); |
243 | $this->callable->setParameters($params); |
244 | |
245 | $value = $this->callable->call(); |
246 | |
247 | $this->callable->setParameters($callableParams); |
248 | } |
249 | } |
250 | |
251 | return $value; |
252 | } |
253 | |
254 | } |