|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Jnjxp\Form |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 5 |
|
6
|
|
|
* |
|
7
|
|
|
* Copyright (C) 2016 Jake Johns |
|
8
|
|
|
* |
|
9
|
|
|
* This software may be modified and distributed under the terms |
|
10
|
|
|
* of the MIT license. See the LICENSE file for details. |
|
11
|
|
|
* |
|
12
|
|
|
* @category Collection |
|
13
|
|
|
* @package Jnjxp\Form |
|
14
|
|
|
* @author Jake Johns <[email protected]> |
|
15
|
|
|
* @copyright 2016 Jake Johns |
|
16
|
|
|
* @license http://jnj.mit-license.org/2016 MIT License |
|
17
|
|
|
* @link http://github.com/jnjxp/jnjxp.form |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
namespace Jnjxp\Form; |
|
21
|
|
|
|
|
22
|
|
|
use ArrayIterator; |
|
23
|
|
|
use IteratorAggregate; |
|
24
|
|
|
use JsonSerializable; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* FieldCollection |
|
28
|
|
|
* |
|
29
|
|
|
* @category Collection |
|
30
|
|
|
* @package Jnjxp\Form |
|
31
|
|
|
* @author Jake Johns <[email protected]> |
|
32
|
|
|
* @license http://jnj.mit-license.org/ MIT License |
|
33
|
|
|
* @link http://github.com/jnjxp/jnjxp.form |
|
34
|
|
|
* |
|
35
|
|
|
* @see IteratorAggregate |
|
36
|
|
|
*/ |
|
37
|
|
|
class FieldCollection implements IteratorAggregate, JsonSerializable |
|
38
|
|
|
{ |
|
39
|
|
|
/** |
|
40
|
|
|
* Fields |
|
41
|
|
|
* |
|
42
|
|
|
* @var array |
|
43
|
|
|
* |
|
44
|
|
|
* @access protected |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $fields = []; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Factory |
|
50
|
|
|
* |
|
51
|
|
|
* @var FieldFactory |
|
52
|
|
|
* |
|
53
|
|
|
* @access protected |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $factory; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Create a collection of fields |
|
59
|
|
|
* |
|
60
|
|
|
* @param FieldFactory $factory Field Factory |
|
61
|
|
|
* |
|
62
|
|
|
* @access public |
|
63
|
|
|
*/ |
|
64
|
9 |
|
public function __construct(FieldFactory $factory = null) |
|
65
|
|
|
{ |
|
66
|
9 |
|
$this->factory = $factory ?: new FieldFactory; |
|
67
|
9 |
|
$this->init(); |
|
68
|
9 |
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Initialize turn-key |
|
72
|
|
|
* |
|
73
|
|
|
* @return void |
|
74
|
|
|
* |
|
75
|
|
|
* @access protected |
|
76
|
|
|
*/ |
|
77
|
9 |
|
protected function init() |
|
78
|
|
|
{ |
|
79
|
9 |
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Get Iterator of fields |
|
83
|
|
|
* |
|
84
|
|
|
* @return ArrayIterator |
|
85
|
|
|
* |
|
86
|
|
|
* @access public |
|
87
|
|
|
*/ |
|
88
|
1 |
|
public function getIterator() |
|
89
|
|
|
{ |
|
90
|
1 |
|
return new ArrayIterator($this->fields); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Add a field |
|
95
|
|
|
* |
|
96
|
|
|
* @param string $name name of field |
|
97
|
|
|
* @param string $class Field class |
|
98
|
|
|
* |
|
99
|
|
|
* @return Field |
|
100
|
|
|
* |
|
101
|
|
|
* @throws Exception if field already exists |
|
102
|
|
|
* |
|
103
|
|
|
* @access public |
|
104
|
|
|
*/ |
|
105
|
6 |
|
public function add($name, $class = Field::class) |
|
106
|
|
|
{ |
|
107
|
6 |
|
if ($this->has($name)) { |
|
108
|
1 |
|
throw new \Exception("Field '$name' already exists"); |
|
109
|
|
|
} |
|
110
|
6 |
|
$this->fields[$name] = $this->factory->newField($name, $class); |
|
111
|
6 |
|
return $this->fields[$name]; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Remove a field |
|
116
|
|
|
* |
|
117
|
|
|
* @param string $name name of field |
|
118
|
|
|
* |
|
119
|
|
|
* @return $this |
|
120
|
|
|
* |
|
121
|
|
|
* @access public |
|
122
|
|
|
*/ |
|
123
|
1 |
|
public function remove($name) |
|
124
|
|
|
{ |
|
125
|
1 |
|
if ($this->has($name)) { |
|
126
|
1 |
|
unset($this->fields[$name]); |
|
127
|
1 |
|
} |
|
128
|
1 |
|
return $this; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Does the collection have a field? |
|
133
|
|
|
* |
|
134
|
|
|
* @param string $name name of the field for which to check |
|
135
|
|
|
* |
|
136
|
|
|
* @return bool |
|
137
|
|
|
* |
|
138
|
|
|
* @access public |
|
139
|
|
|
*/ |
|
140
|
7 |
|
public function has($name) |
|
141
|
|
|
{ |
|
142
|
7 |
|
return isset($this->fields[$name]); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Get a field |
|
147
|
|
|
* |
|
148
|
|
|
* @param string $name name of field to get |
|
149
|
|
|
* |
|
150
|
|
|
* @return Field |
|
151
|
|
|
* |
|
152
|
|
|
* @throws Exception if field does not exist |
|
153
|
|
|
* |
|
154
|
|
|
* @access public |
|
155
|
|
|
*/ |
|
156
|
4 |
|
public function get($name) |
|
157
|
|
|
{ |
|
158
|
4 |
|
if (! $this->has($name)) { |
|
159
|
1 |
|
throw new \Exception("Field '$name' not found"); |
|
160
|
|
|
} |
|
161
|
3 |
|
return $this->fields[$name]; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Fill fields with values |
|
166
|
|
|
* |
|
167
|
|
|
* @param array $values values to fill |
|
168
|
|
|
* |
|
169
|
|
|
* @return $this |
|
170
|
|
|
* |
|
171
|
|
|
* @access public |
|
172
|
|
|
*/ |
|
173
|
1 |
|
public function fill(array $values) |
|
174
|
|
|
{ |
|
175
|
1 |
|
foreach ($values as $name => $value) { |
|
176
|
1 |
|
if ($this->has($name)) { |
|
177
|
1 |
|
$this->get($name)->fill($value); |
|
178
|
1 |
|
} |
|
179
|
1 |
|
} |
|
180
|
1 |
|
return $this; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Set error messages |
|
185
|
|
|
* |
|
186
|
|
|
* @param array $errors error messages |
|
187
|
|
|
* |
|
188
|
|
|
* @return $this |
|
189
|
|
|
* |
|
190
|
|
|
* @access public |
|
191
|
|
|
*/ |
|
192
|
1 |
|
public function errors(array $errors) |
|
193
|
|
|
{ |
|
194
|
1 |
|
foreach ($errors as $name => $error) { |
|
195
|
1 |
|
if ($this->has($name)) { |
|
196
|
1 |
|
$this->get($name)->errors($error); |
|
197
|
1 |
|
} |
|
198
|
1 |
|
} |
|
199
|
1 |
|
return $this; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* JsonSerialize |
|
204
|
|
|
* |
|
205
|
|
|
* @return array |
|
206
|
|
|
* |
|
207
|
|
|
* @access public |
|
208
|
|
|
*/ |
|
209
|
|
|
public function jsonSerialize() |
|
210
|
|
|
{ |
|
211
|
|
|
return $this->fields; |
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
|