forked from sstok/gush-experiments-sandbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFieldSet.php
297 lines (263 loc) · 7.07 KB
/
FieldSet.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<?php
/*
* This file is part of the RollerworksSearch package.
*
* (c) Sebastiaan Stok <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Rollerworks\Component\Search;
use Rollerworks\Component\Search\Exception\BadMethodCallException;
use Rollerworks\Component\Search\Exception\UnexpectedTypeException;
/**
* A FieldSet holds all the search fields and there configuration.
*
* @author Sebastiaan Stok <[email protected]>
*/
class FieldSet implements \Countable, \IteratorAggregate
{
/**
* Search fields.
*
* @var FieldConfigInterface[]
*/
private $fields = [];
/**
* Name of the FieldSet.
*
* @var string
*/
private $name;
/**
* @var bool
*/
private $locked = false;
/**
* Constructor.
*
* @param string|null $name FieldSet name, should start with a letter, digit or underscore
* and only contain letters, digits, numbers, underscores ("_") and
* hyphens ("-").
*
* @throws UnexpectedTypeException If the name is not a string or an integer
* @throws \InvalidArgumentException If the name contains invalid characters
*/
public function __construct($name = null)
{
self::validateName($name);
$this->name = $name;
}
/**
* Returns a new FieldSet instance.
*
* @param string|null $name
*
* @return FieldSet
*/
public static function create($name = null)
{
return new static($name);
}
/**
* Returns the name of the set.
*
* @return null|string
*/
public function getSetName()
{
return $this->name;
}
/**
* Add/replace a search field on the set.
*
* Any existing field with the same name will be overwritten.
*
* @param string $name
* @param FieldConfigInterface $config
*
* @return self
*/
public function set($name, FieldConfigInterface $config)
{
if ($this->locked) {
$this->throwLocked();
}
self::validateName($name);
$this->fields[$name] = $config;
return $this;
}
/**
* Replaces an existing search field on the set.
*
* Same as {@link FieldSet::set()}, but throws an exception when there is no
* field registered with that name.
*
* @param string $name
* @param FieldConfigInterface $config
*
* @throws \RuntimeException When the field is not registered at this fieldset
*
* @return self
*/
public function replace($name, FieldConfigInterface $config)
{
if ($this->locked) {
$this->throwLocked();
}
if (!isset($this->fields[$name])) {
throw new \RuntimeException(
sprintf('Unable to replace none existent field: %s', $name)
);
}
$this->fields[$name] = $config;
return $this;
}
/**
* Removes a field from the set.
*
* @param string $name
*
* @return self
*/
public function remove($name)
{
if ($this->locked) {
$this->throwLocked();
}
if (isset($this->fields[$name])) {
unset($this->fields[$name]);
}
return $this;
}
/**
* Returns the {@link FieldConfigInterface} object of the search field.
*
* @param string $name
*
* @throws \RuntimeException When the field is not registered at this Fieldset
*
* @return FieldConfigInterface
*/
public function get($name)
{
if (!isset($this->fields[$name])) {
throw new \RuntimeException(
sprintf('Unable to find none existent field: %s', $name)
);
}
return $this->fields[$name];
}
/**
* Returns all the registered fields in the set.
*
* @return FieldConfigInterface[] [name] => {FieldConfigInterface object})
*/
public function all()
{
return $this->fields;
}
/**
* Returns whether the field is registered in the set.
*
* @param string $name
*
* @return bool
*/
public function has($name)
{
return isset($this->fields[$name]);
}
/**
* Returns the current FieldSet as an Iterator that includes all Fields.
*
* @see all()
*
* @return \ArrayIterator An \ArrayIterator object for iterating over fields
*/
public function getIterator()
{
return new \ArrayIterator($this->fields);
}
/**
* Returns the number of fields in the set.
*
* @return int The number of fields
*/
public function count()
{
return count($this->fields);
}
/**
* Validates whether the given name is a valid set name.
*
* @param string|null $name The tested FieldSet name
*
* @throws UnexpectedTypeException If the name is not a string
* @throws \InvalidArgumentException If the name contains invalid characters
*/
public static function validateName($name)
{
if (null !== $name && !is_string($name)) {
throw new UnexpectedTypeException($name, ['string', 'null']);
}
if (!self::isValidName($name)) {
throw new \InvalidArgumentException(
sprintf(
'The name "%s" contains illegal characters. Names should start with a letter, digit or underscore '.
'and only contain letters, digits, numbers, underscores ("_") and hyphens ("-").',
$name
)
);
}
}
/**
* Returns whether the given name is a valid set name.
*
* A name is accepted if it:
*
* * is empty
* * starts with a letter, digit or underscore
* * contains only letters, digits, numbers, underscores ("_") and hyphens ("-")
*
* @param string $name The tested name
*
* @return bool Whether the name is valid
*/
final public static function isValidName($name)
{
return '' === $name || null === $name || preg_match('/^[a-zA-Z0-9_][a-zA-Z0-9_\-]*$/D', $name);
}
/**
* Sets the set's data is locked.
*
* After calling this method, setter methods can be no longer called.
*
* @throws BadMethodCallException when the data is locked
*/
public function lockConfig()
{
if ($this->locked) {
$this->throwLocked();
}
$this->locked = true;
}
/**
* Returns whether the set's data is locked.
*
* A FieldSet with locked data is restricted to the data currently
* configured.
*
* @return bool Whether the data is locked.
*/
public function isConfigLocked()
{
return $this->locked;
}
private function throwLocked()
{
throw new BadMethodCallException(
'FieldSet setter methods cannot be accessed anymore once the data is locked.'
);
}
}