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
|
|
|
* @param mixed $options Optional: configuration options for the fieldset. |
305
|
|
|
* |
306
|
|
|
* @return Fieldset |
307
|
|
|
* |
308
|
|
|
*/ |
309
|
|
|
public function setFieldset($name, $type = null, $options = null) |
310
|
|
|
{ |
311
|
|
|
$this->inputs[$name] = $this->builder->newFieldset($name, $type, $options); |
312
|
|
|
return $this->inputs[$name]; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* |
317
|
|
|
* Sets a new Collection input. |
318
|
|
|
* |
319
|
|
|
* @param string $name The Collection name. |
320
|
|
|
* |
321
|
|
|
* @param string $type A Collection of this type of Fieldset; defaults to |
322
|
|
|
* $name. |
323
|
|
|
* |
324
|
|
|
* @return Collection |
325
|
|
|
* |
326
|
|
|
*/ |
327
|
|
|
public function setCollection($name, $type = null) |
328
|
|
|
{ |
329
|
|
|
$this->inputs[$name] = $this->builder->newCollection($name, $type); |
330
|
|
|
return $this->inputs[$name]; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* |
335
|
|
|
* Returns an input in a format suitable for a view. |
336
|
|
|
* |
337
|
|
|
* @param string $name The input name. |
338
|
|
|
* |
339
|
|
|
* @return mixed |
340
|
|
|
* |
341
|
|
|
*/ |
342
|
|
|
public function get($name = null) |
343
|
|
|
{ |
344
|
|
|
if ($name === null) { |
345
|
|
|
return $this; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
$input = $this->getInput($name); |
349
|
|
|
return $input->get(); |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* |
354
|
|
|
* Filters the inputs on this fieldset. |
355
|
|
|
* |
356
|
|
|
* @return bool True if all the filter rules pass, false if not. |
357
|
|
|
* |
358
|
|
|
*/ |
359
|
|
|
public function filter() |
360
|
|
|
{ |
361
|
|
|
$this->success = $this->filter->apply($this); |
362
|
|
|
$this->failures = $this->filter->getFailures(); |
363
|
|
|
|
364
|
|
|
// Iterate on fieldset or collection and get failures |
365
|
|
|
foreach ($this->inputs as $name => $input) { |
366
|
|
|
if ($input instanceof Fieldset || $input instanceof Collection) { |
367
|
|
|
if (! $input->filter()) { |
368
|
|
|
$this->success = false; |
369
|
|
|
$failures = $input->getFailures(); |
370
|
|
|
if ($failures instanceof FailureCollectionInterface) { |
371
|
|
|
$failures = $failures->getMessages(); |
372
|
|
|
} |
373
|
|
|
$this->failures->addMessagesForField($name, $failures); |
374
|
|
|
} |
375
|
|
|
} |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
return $this->success; |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
/** |
382
|
|
|
* |
383
|
|
|
* Returns the failures. |
384
|
|
|
* |
385
|
|
|
* @return FailureCollectionInterface |
386
|
|
|
* |
387
|
|
|
*/ |
388
|
|
|
public function getFailures() |
389
|
|
|
{ |
390
|
|
|
return $this->failures; |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
/** |
394
|
|
|
* |
395
|
|
|
* Returns the value of this input for use in arrays. |
396
|
|
|
* |
397
|
|
|
* @return array |
398
|
|
|
* |
399
|
|
|
*/ |
400
|
|
|
public function getValue() |
401
|
|
|
{ |
402
|
|
|
$data = []; |
403
|
|
|
foreach ($this->inputs as $name => $input) { |
404
|
|
|
$data[$name] = $input->getValue(); |
405
|
|
|
} |
406
|
|
|
return $data; |
407
|
|
|
} |
408
|
|
|
} |
409
|
|
|
|