Completed
Push — 3.x ( eb6949...ce948d )
by Hari
9s
created

Fieldset::filter()   C

Complexity

Conditions 8
Paths 16

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 13
nc 16
nop 0
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
16
/**
17
 *
18
 * A fieldset of inputs, where the inputs themselves may be values, other
19
 * fieldsets, or other collections.
20
 *
21
 * @package Aura.Input
22
 *
23
 */
24
class Fieldset extends AbstractInput
25
{
26
    /**
27
     *
28
     * A builder to create input objects.
29
     *
30
     * @var BuilderInterface
31
     *
32
     */
33
    protected $builder;
34
35
    /**
36
     *
37
     * A filter for the fieldset values.
38
     *
39
     * @var FilterInterface
40
     *
41
     */
42
    protected $filter;
43
44
    /**
45
     *
46
     * Inputs in the fieldset.
47
     *
48
     * @var array
49
     *
50
     */
51
    protected $inputs = [];
52
53
    /**
54
     *
55
     * Object for retaining information about options available to the form
56
     * inputs.
57
     *
58
     * @var mixed
59
     *
60
     */
61
    protected $options;
62
63
    /**
64
     *
65
     * Property for storing the result of the last filter() call.
66
     *
67
     * @var bool
68
     *
69
     */
70
    protected $success;
71
72
    /**
73
     *
74
     * Failures in the fieldset.
75
     *
76
     * @var ArrayObject
77
     *
78
     */
79
    protected $failures;
80
81
    /**
82
     *
83
     * Constructor.
84
     *
85
     * @param BuilderInterface $builder An object to build input objects.
86
     *
87
     * @param FilterInterface $filter A filter object for this fieldset.
88
     *
89
     * @param object $options An arbitrary options object for use when setting
90
     * up inputs and filters.
91
     *
92
     */
93
    public function __construct(
94
        BuilderInterface $builder,
95
        FilterInterface  $filter,
96
        $options = null
97
    ) {
98
        $this->builder  = $builder;
99
        $this->filter   = $filter;
100
        $this->options  = $options;
101
        $this->init();
102
    }
103
104
    /**
105
     *
106
     * Gets an input value from this fieldset.
107
     *
108
     * @param string $name The input name.
109
     *
110
     * @return mixed The input value.
111
     *
112
     */
113
    public function __get($name)
114
    {
115
        return $this->getInput($name)->read();
116
    }
117
118
    /**
119
     *
120
     * Sets an input value on this fieldset.
121
     *
122
     * @param string $name The input name.
123
     *
124
     * @param mixed $value The input value.
125
     *
126
     * @return void
127
     *
128
     */
129
    public function __set($name, $value)
130
    {
131
        $this->getInput($name)->fill($value);
132
    }
133
134
    /**
135
     *
136
     * Checks if a value is set on an input in this fieldset.
137
     *
138
     * @param string $name The input name.
139
     *
140
     * @return bool
141
     *
142
     */
143
    public function __isset($name)
144
    {
145
        if (! isset($this->inputs[$name])) {
146
            return false;
147
        }
148
149
        return $this->getInput($name)->read() !== null;
150
    }
151
152
    /**
153
     *
154
     * Sets the value of an input in this fieldset to null.
155
     *
156
     * @param string $name The input name.
157
     *
158
     * @return void
159
     *
160
     */
161
    public function __unset($name)
162
    {
163
        if (isset($this->inputs[$name])) {
164
            $this->getInput($name)->fill(null);
165
        }
166
    }
167
168
    /**
169
     *
170
     * Returns the filter object.
171
     *
172
     * @return FilterInterface
173
     *
174
     */
175
    public function getFilter()
176
    {
177
        return $this->filter;
178
    }
179
180
    /**
181
     *
182
     * Returns an individual input object by name.
183
     *
184
     * @param string $name The name of the input object.
185
     *
186
     * @return AbstractInput
187
     *
188
     */
189
    public function getInput($name)
190
    {
191
        if (! isset($this->inputs[$name])) {
192
            throw new Exception\NoSuchInput($name);
193
        }
194
195
        $input = $this->inputs[$name];
196
        $input->setNamePrefix($this->getFullName());
197
        return $input;
198
    }
199
200
    /**
201
     *
202
     * Returns the names of all input objects in this fieldset.
203
     *
204
     * @return array
205
     *
206
     */
207
    public function getInputNames()
208
    {
209
        return array_keys($this->inputs);
210
    }
211
212
    /**
213
     *
214
     * Returns the input builder.
215
     *
216
     * @return BuilderInterface
217
     *
218
     */
219
    public function getBuilder()
220
    {
221
        return $this->builder;
222
    }
223
224
    /**
225
     *
226
     * Returns the options object
227
     *
228
     * @return mixed
229
     *
230
     */
231
    public function getOptions()
232
    {
233
        return $this->options;
234
    }
235
236
    /**
237
     *
238
     * Fills this fieldset with input values.
239
     *
240
     * @param array $data The values for this fieldset.
241
     *
242
     * @return void
243
     *
244
     */
245
    public function fill(array $data)
246
    {
247
        $this->success = null;
248
        foreach ($this->inputs as $key => $input) {
249
            if (array_key_exists($key, $data)) {
250
                $input->fill($data[$key]);
251
            }
252
        }
253
    }
254
255
    /**
256
     *
257
     * Initializes the inputs and filter.
258
     *
259
     * @return void
260
     *
261
     */
262
    public function init()
263
    {
264
    }
265
266
    /**
267
     *
268
     * Did the input data pass the filter rules?
269
     *
270
     * @return null|bool
271
     *
272
     */
273
    public function isSuccess()
274
    {
275
        return $this->success;
276
    }
277
278
    /**
279
     *
280
     * Sets a new Field input.
281
     *
282
     * @param string $name The Field name.
283
     *
284
     * @param string $type A Field of this type; defaults to 'text'.
285
     *
286
     * @return Field
287
     *
288
     */
289
    public function setField($name, $type = null)
290
    {
291
        $this->inputs[$name] = $this->builder->newField($name, $type);
292
        return $this->inputs[$name];
293
    }
294
295
    /**
296
     *
297
     * Sets a new Fieldset input.
298
     *
299
     * @param string $name The Fieldset name.
300
     *
301
     * @param string $type A Fieldset of this type; defaults to $name.
302
     *
303
     * @return Fieldset
304
     *
305
     */
306
    public function setFieldset($name, $type = null)
307
    {
308
        $this->inputs[$name] = $this->builder->newFieldset($name, $type);
309
        return $this->inputs[$name];
310
    }
311
312
    /**
313
     *
314
     * Sets a new Collection input.
315
     *
316
     * @param string $name The Collection name.
317
     *
318
     * @param string $type A Collection of this type of Fieldset; defaults to
319
     * $name.
320
     *
321
     * @return Collection
322
     *
323
     */
324
    public function setCollection($name, $type = null)
325
    {
326
        $this->inputs[$name] = $this->builder->newCollection($name, $type);
327
        return $this->inputs[$name];
328
    }
329
330
    /**
331
     *
332
     * Returns an input in a format suitable for a view.
333
     *
334
     * @param string $name The input name.
335
     *
336
     * @return mixed
337
     *
338
     */
339
    public function get($name = null)
340
    {
341
        if ($name === null) {
342
            return $this;
343
        }
344
345
        $input = $this->getInput($name);
346
        return $input->get();
347
    }
348
349
    /**
350
     *
351
     * Filters the inputs on this fieldset.
352
     *
353
     * @return bool True if all the filter rules pass, false if not.
354
     *
355
     */
356
     public function filter()
357
     {
358
        $success = $this->filter->apply($this);
359
        if (! $success) {
360
            $this->success = $success;
361
        }
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
                    $this->failures->offsetSet($name, $input->getFailures());
370
                }
371
            }
372
        }
373
374
         // If there is no failure, then it is a success on all Filters
375
        if (! isset($this->success) && $success) {
376
            $this->success = true;
377
        }
378
379
        return $this->success;
380
    }
381
382
    /**
383
     *
384
     * Returns the failures.
385
     *
386
     * @return ArrayObject
387
     *
388
     */
389
    public function getFailures()
390
    {
391
        return $this->failures;
392
    }
393
394
    /**
395
     *
396
     * Returns the value of this input for use in arrays.
397
     *
398
     * @return array
399
     *
400
     */
401
    public function getValue()
402
    {
403
        $data = [];
404
        foreach ($this->inputs as $name => $input) {
405
            $data[$name] = $input->getValue();
406
        }
407
        return $data;
408
    }
409
}
410