Issues (3)

src/Collection.php (1 issue)

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 ArrayAccess;
14
use Countable;
15
use IteratorAggregate;
16
17
/**
18
 *
19
 * Represents a collection of fieldsets of a single type.
20
 *
21
 * @package Aura.Input
22
 *
23
 */
24
class Collection extends AbstractInput implements ArrayAccess, Countable, IteratorAggregate
25
{
26
    /**
27
     *
28
     * Factory to create a particular fieldset type.
29
     *
30
     * @var callable
31
     *
32
     */
33
    protected $factory;
34
35
    /**
36
     *
37
     * Fieldsets in the collection.
38
     *
39
     * @var array
40
     *
41
     */
42
    protected $fieldsets = [];
43
44
    /**
45
     *
46
     * Constructor.
47
     *
48
     * @param callable $factory A factory to create the fieldset objects for
49
     * this collection.
50
     *
51
     */
52
    public function __construct(callable $factory)
53
    {
54
        $this->factory = $factory;
55
    }
56
57
    /**
58
     *
59
     * Support for this input when addressed via Fieldset::__set().
60
     *
61
     * @param array $data The data for each fieldset in the collection.
62
     *
63
     */
64
    public function fill(array $data)
65
    {
66
        $this->fieldsets = [];
67
        foreach ($data as $key => $inputs) {
68
            $fieldset = $this->newFieldset($key);
69
            foreach ($inputs as $name => $value) {
70
                $fieldset->getInput($name)->fill($value);
0 ignored issues
show
The method fill() does not exist on Aura\Input\AbstractInput. Since it exists in all sub-types, consider adding an abstract or default implementation to Aura\Input\AbstractInput. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
                $fieldset->getInput($name)->/** @scrutinizer ignore-call */ fill($value);
Loading history...
71
            }
72
            $this->fieldsets[$key] = $fieldset;
73
        }
74
    }
75
76
    /**
77
     *
78
     * Applies each fieldset filter.
79
     *
80
     * @return bool True if all filters passed, false if one or more failed.
81
     *
82
     */
83
    public function filter()
84
    {
85
        $passed = true;
86
        foreach ($this->fieldsets as $key => $fieldset) {
87
            if (! $fieldset->filter()) {
88
                $passed = false;
89
            }
90
        }
91
        return $passed;
92
    }
93
94
    /**
95
     *
96
     * Returns the failures for the fieldset filters.
97
     *
98
     * @return array
99
     *
100
     */
101
    public function getFailures()
102
    {
103
        $failures = [];
104
        foreach ($this->fieldsets as $key => $fieldset) {
105
            $failures[$key] = $fieldset->getFailures()->getMessages();
106
        }
107
        return $failures;
108
    }
109
110
    /**
111
     *
112
     * IteratorAggregate: returns an external iterator for this collection.
113
     *
114
     * @return CollectionIterator
115
     *
116
     */
117
    #[\ReturnTypeWillChange]
118
    public function getIterator()
119
    {
120
        return new CollectionIterator($this);
121
    }
122
123
    /**
124
     *
125
     * Gets all the keys for all Fieldsets in this collection.
126
     *
127
     * @return array
128
     *
129
     */
130
    public function getKeys()
131
    {
132
        return array_keys($this->fieldsets);
133
    }
134
135
    /**
136
     *
137
     * Creates and returns a new fieldset.
138
     *
139
     * @param string $key The key for the new fieldset.
140
     *
141
     * @return Fieldset
142
     *
143
     */
144
    protected function newFieldset($key)
145
    {
146
        $factory = $this->factory;
147
        $fieldset = $factory();
148
        $fieldset->setName($key);
149
        return $fieldset;
150
    }
151
152
    /**
153
     *
154
     * ArrayAccess: returns the fieldset at a particular offset.
155
     *
156
     * @param mixed $offset The fieldset key.
157
     *
158
     * @return Fieldset
159
     *
160
     */
161
    #[\ReturnTypeWillChange]
162
    public function offsetGet($offset)
163
    {
164
        $fieldset = $this->fieldsets[$offset];
165
        $fieldset->setNamePrefix($this->getFullName());
166
        return $fieldset;
167
    }
168
169
    /**
170
     *
171
     * ArrayAccess: sets an offset as a Fieldset.
172
     *
173
     * @param mixed $offset The Fieldset key.
174
     *
175
     * @param Fieldset $fieldset The Fieldset for that key.
176
     *
177
     * @return void
178
     *
179
     */
180
    #[\ReturnTypeWillChange]
181
    public function offsetSet($offset, $fieldset)
182
    {
183
        $this->fieldsets[$offset] = $fieldset;
184
    }
185
186
    /**
187
     *
188
     * ArrayAccess: is a particular Fieldset key set?
189
     *
190
     * @param mixed $offset The Fieldset key.
191
     *
192
     * @return bool True if the Fielset key is set, false if not.
193
     *
194
     */
195
    #[\ReturnTypeWillChange]
196
    public function offsetExists($offset)
197
    {
198
        return isset($this->fieldsets[$offset]);
199
    }
200
201
    /**
202
     *
203
     * ArrayAccess: unsets a particular Fieldset key.
204
     *
205
     * @param mixed $offset The Fieldset key.
206
     *
207
     * @return void
208
     *
209
     */
210
    #[\ReturnTypeWillChange]
211
    public function offsetUnset($offset)
212
    {
213
        unset($this->fieldsets[$offset]);
214
    }
215
216
    /**
217
     *
218
     * Countable: returns the number of Fieldsets in this collection.
219
     *
220
     * @return int
221
     *
222
     */
223
    #[\ReturnTypeWillChange]
224
    public function count()
225
    {
226
        return count($this->fieldsets);
227
    }
228
229
    /**
230
     *
231
     * Returns the value of this input for use in arrays.
232
     *
233
     * @return array
234
     *
235
     */
236
    public function getValue()
237
    {
238
        $data = [];
239
        foreach ($this->fieldsets as $key => $fieldset) {
240
            $data[$key] = $fieldset->getValue();
241
        }
242
        return $data;
243
    }
244
}
245