These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * |
||
4 | * This file is part of the Aura project for PHP. |
||
5 | * |
||
6 | * @package Aura.Input |
||
7 | * |
||
8 | * @license http://opensource.org/licenses/MIT-license.php MIT |
||
9 | * |
||
10 | */ |
||
11 | namespace Aura\Input; |
||
12 | |||
13 | use ArrayObject; |
||
14 | use Aura\Filter_Interface\FilterInterface; |
||
15 | use Aura\Filter_Interface\FailureCollectionInterface; |
||
16 | |||
17 | /** |
||
18 | * |
||
19 | * A fieldset of inputs, where the inputs themselves may be values, other |
||
20 | * fieldsets, or other collections. |
||
21 | * |
||
22 | * @package Aura.Input |
||
23 | * |
||
24 | */ |
||
25 | class Fieldset extends AbstractInput |
||
26 | { |
||
27 | /** |
||
28 | * |
||
29 | * A builder to create input objects. |
||
30 | * |
||
31 | * @var BuilderInterface |
||
32 | * |
||
33 | */ |
||
34 | protected $builder; |
||
35 | |||
36 | /** |
||
37 | * |
||
38 | * A filter for the fieldset values. |
||
39 | * |
||
40 | * @var FilterInterface |
||
41 | * |
||
42 | */ |
||
43 | protected $filter; |
||
44 | |||
45 | /** |
||
46 | * |
||
47 | * Inputs in the fieldset. |
||
48 | * |
||
49 | * @var array |
||
50 | * |
||
51 | */ |
||
52 | protected $inputs = []; |
||
53 | |||
54 | /** |
||
55 | * |
||
56 | * Object for retaining information about options available to the form |
||
57 | * inputs. |
||
58 | * |
||
59 | * @var mixed |
||
60 | * |
||
61 | */ |
||
62 | protected $options; |
||
63 | |||
64 | /** |
||
65 | * |
||
66 | * Property for storing the result of the last filter() call. |
||
67 | * |
||
68 | * @var bool |
||
69 | * |
||
70 | */ |
||
71 | protected $success; |
||
72 | |||
73 | /** |
||
74 | * |
||
75 | * Failures in the fieldset. |
||
76 | * |
||
77 | * @var FailureCollectionInterface |
||
78 | * |
||
79 | */ |
||
80 | protected $failures; |
||
81 | |||
82 | /** |
||
83 | * |
||
84 | * Constructor. |
||
85 | * |
||
86 | * @param BuilderInterface $builder An object to build input objects. |
||
87 | * |
||
88 | * @param FilterInterface $filter A filter object for this fieldset. |
||
89 | * |
||
90 | * @param object $options An arbitrary options object for use when setting |
||
91 | * up inputs and filters. |
||
92 | * |
||
93 | */ |
||
94 | public function __construct( |
||
95 | BuilderInterface $builder, |
||
96 | FilterInterface $filter, |
||
97 | $options = null |
||
98 | ) { |
||
99 | $this->builder = $builder; |
||
100 | $this->filter = $filter; |
||
101 | $this->options = $options; |
||
102 | $this->init(); |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * |
||
107 | * Gets an input value from this fieldset. |
||
108 | * |
||
109 | * @param string $name The input name. |
||
110 | * |
||
111 | * @return mixed The input value. |
||
112 | * |
||
113 | */ |
||
114 | public function __get($name) |
||
115 | { |
||
116 | return $this->getInput($name)->read(); |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * |
||
121 | * Sets an input value on this fieldset. |
||
122 | * |
||
123 | * @param string $name The input name. |
||
124 | * |
||
125 | * @param mixed $value The input value. |
||
126 | * |
||
127 | * @return void |
||
128 | * |
||
129 | */ |
||
130 | public function __set($name, $value) |
||
131 | { |
||
132 | $this->getInput($name)->fill($value); |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * |
||
137 | * Checks if a value is set on an input in this fieldset. |
||
138 | * |
||
139 | * @param string $name The input name. |
||
140 | * |
||
141 | * @return bool |
||
142 | * |
||
143 | */ |
||
144 | public function __isset($name) |
||
145 | { |
||
146 | if (! isset($this->inputs[$name])) { |
||
147 | return false; |
||
148 | } |
||
149 | |||
150 | return $this->getInput($name)->read() !== null; |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * |
||
155 | * Sets the value of an input in this fieldset to null. |
||
156 | * |
||
157 | * @param string $name The input name. |
||
158 | * |
||
159 | * @return void |
||
160 | * |
||
161 | */ |
||
162 | public function __unset($name) |
||
163 | { |
||
164 | if (isset($this->inputs[$name])) { |
||
165 | $this->getInput($name)->fill(null); |
||
166 | } |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * |
||
171 | * Returns the filter object. |
||
172 | * |
||
173 | * @return FilterInterface |
||
174 | * |
||
175 | */ |
||
176 | public function getFilter() |
||
177 | { |
||
178 | return $this->filter; |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * |
||
183 | * Returns an individual input object by name. |
||
184 | * |
||
185 | * @param string $name The name of the input object. |
||
186 | * |
||
187 | * @return AbstractInput |
||
188 | * |
||
189 | */ |
||
190 | public function getInput($name) |
||
191 | { |
||
192 | if (! isset($this->inputs[$name])) { |
||
193 | throw new Exception\NoSuchInput($name); |
||
194 | } |
||
195 | |||
196 | $input = $this->inputs[$name]; |
||
197 | $input->setNamePrefix($this->getFullName()); |
||
198 | return $input; |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * |
||
203 | * Returns the names of all input objects in this fieldset. |
||
204 | * |
||
205 | * @return array |
||
206 | * |
||
207 | */ |
||
208 | public function getInputNames() |
||
209 | { |
||
210 | return array_keys($this->inputs); |
||
211 | } |
||
212 | |||
213 | /** |
||
214 | * |
||
215 | * Returns the input builder. |
||
216 | * |
||
217 | * @return BuilderInterface |
||
218 | * |
||
219 | */ |
||
220 | public function getBuilder() |
||
221 | { |
||
222 | return $this->builder; |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * |
||
227 | * Returns the options object |
||
228 | * |
||
229 | * @return mixed |
||
230 | * |
||
231 | */ |
||
232 | public function getOptions() |
||
233 | { |
||
234 | return $this->options; |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * |
||
239 | * Fills this fieldset with input values. |
||
240 | * |
||
241 | * @param array $data The values for this fieldset. |
||
242 | * |
||
243 | * @return void |
||
244 | * |
||
245 | */ |
||
246 | public function fill(array $data) |
||
247 | { |
||
248 | $this->success = null; |
||
249 | foreach ($this->inputs as $key => $input) { |
||
250 | if (array_key_exists($key, $data)) { |
||
251 | $input->fill($data[$key]); |
||
252 | } |
||
253 | } |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * |
||
258 | * Initializes the inputs and filter. |
||
259 | * |
||
260 | * @return void |
||
261 | * |
||
262 | */ |
||
263 | public function init() |
||
264 | { |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * |
||
269 | * Did the input data pass the filter rules? |
||
270 | * |
||
271 | * @return null|bool |
||
272 | * |
||
273 | */ |
||
274 | public function isSuccess() |
||
275 | { |
||
276 | return $this->success; |
||
277 | } |
||
278 | |||
279 | /** |
||
280 | * |
||
281 | * Sets a new Field input. |
||
282 | * |
||
283 | * @param string $name The Field name. |
||
284 | * |
||
285 | * @param string $type A Field of this type; defaults to 'text'. |
||
286 | * |
||
287 | * @return Field |
||
288 | * |
||
289 | */ |
||
290 | public function setField($name, $type = null) |
||
291 | { |
||
292 | $this->inputs[$name] = $this->builder->newField($name, $type); |
||
293 | return $this->inputs[$name]; |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * |
||
298 | * Sets a new Fieldset input. |
||
299 | * |
||
300 | * @param string $name The Fieldset name. |
||
301 | * |
||
302 | * @param string $type A Fieldset of this type; defaults to $name. |
||
303 | * |
||
304 | * @return Fieldset |
||
305 | * |
||
306 | */ |
||
307 | public function setFieldset($name, $type = null) |
||
308 | { |
||
309 | $this->inputs[$name] = $this->builder->newFieldset($name, $type); |
||
310 | return $this->inputs[$name]; |
||
311 | } |
||
312 | |||
313 | /** |
||
314 | * |
||
315 | * Sets a new Collection input. |
||
316 | * |
||
317 | * @param string $name The Collection name. |
||
318 | * |
||
319 | * @param string $type A Collection of this type of Fieldset; defaults to |
||
320 | * $name. |
||
321 | * |
||
322 | * @return Collection |
||
323 | * |
||
324 | */ |
||
325 | public function setCollection($name, $type = null) |
||
326 | { |
||
327 | $this->inputs[$name] = $this->builder->newCollection($name, $type); |
||
328 | return $this->inputs[$name]; |
||
329 | } |
||
330 | |||
331 | /** |
||
332 | * |
||
333 | * Returns an input in a format suitable for a view. |
||
334 | * |
||
335 | * @param string $name The input name. |
||
336 | * |
||
337 | * @return mixed |
||
338 | * |
||
339 | */ |
||
340 | public function get($name = null) |
||
341 | { |
||
342 | if ($name === null) { |
||
343 | return $this; |
||
344 | } |
||
345 | |||
346 | $input = $this->getInput($name); |
||
347 | return $input->get(); |
||
348 | } |
||
349 | |||
350 | /** |
||
351 | * |
||
352 | * Filters the inputs on this fieldset. |
||
353 | * |
||
354 | * @return bool True if all the filter rules pass, false if not. |
||
355 | * |
||
356 | */ |
||
357 | public function filter() |
||
358 | { |
||
359 | $this->success = $this->filter->apply($this); |
||
360 | $this->failures = $this->filter->getFailures(); |
||
0 ignored issues
–
show
|
|||
361 | |||
362 | // Iterate on fieldset or collection and get failures |
||
363 | foreach ($this->inputs as $name => $input) { |
||
364 | if ($input instanceof Fieldset || $input instanceof Collection) { |
||
365 | if (! $input->filter()) { |
||
366 | $this->success = false; |
||
367 | $failures = $input->getFailures(); |
||
368 | if ($failures instanceof FailureCollectionInterface) { |
||
0 ignored issues
–
show
The class
Aura\Filter_Interface\FailureCollectionInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?
This error could be the result of: 1. Missing dependenciesPHP Analyzer uses your Are you sure this class is defined by one of your dependencies, or did you maybe
not list a dependency in either the 2. Missing use statementPHP does not complain about undefined classes in if ($x instanceof DoesNotExist) {
// Do something.
}
If you have not tested against this specific condition, such errors might go unnoticed.
Loading history...
|
|||
369 | $failures = $failures->getMessages(); |
||
370 | } |
||
371 | $this->failures->addMessagesForField($name, $failures); |
||
372 | } |
||
373 | } |
||
374 | } |
||
375 | |||
376 | return $this->success; |
||
377 | } |
||
378 | |||
379 | /** |
||
380 | * |
||
381 | * Returns the failures. |
||
382 | * |
||
383 | * @return FailureCollectionInterface |
||
384 | * |
||
385 | */ |
||
386 | public function getFailures() |
||
387 | { |
||
388 | return $this->failures; |
||
389 | } |
||
390 | |||
391 | /** |
||
392 | * |
||
393 | * Returns the value of this input for use in arrays. |
||
394 | * |
||
395 | * @return array |
||
396 | * |
||
397 | */ |
||
398 | public function getValue() |
||
399 | { |
||
400 | $data = []; |
||
401 | foreach ($this->inputs as $name => $input) { |
||
402 | $data[$name] = $input->getValue(); |
||
403 | } |
||
404 | return $data; |
||
405 | } |
||
406 | } |
||
407 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..