1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace drupol\htmltag\Attributes; |
4
|
|
|
|
5
|
|
|
use drupol\htmltag\AbstractBaseHtmlTagObject; |
6
|
|
|
use drupol\htmltag\Attribute\AttributeFactoryInterface; |
7
|
|
|
use drupol\htmltag\Attribute\AttributeInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Attributes. |
11
|
|
|
*/ |
12
|
|
|
class Attributes extends AbstractBaseHtmlTagObject implements AttributesInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Stores the attribute data. |
16
|
|
|
* |
17
|
|
|
* @var \drupol\htmltag\Attribute\AttributeInterface[] |
18
|
|
|
*/ |
19
|
|
|
private $storage = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The attribute factory. |
23
|
|
|
* |
24
|
|
|
* @var \drupol\htmltag\Attribute\AttributeFactoryInterface |
25
|
|
|
*/ |
26
|
|
|
private $attributeFactory; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Attributes constructor. |
30
|
|
|
* |
31
|
|
|
* @param \drupol\htmltag\Attribute\AttributeFactoryInterface $attributeFactory |
32
|
|
|
* The attribute factory. |
33
|
|
|
* @param mixed[] $attributes |
34
|
|
|
* The input attributes. |
35
|
27 |
|
*/ |
36
|
|
|
public function __construct(AttributeFactoryInterface $attributeFactory, $attributes = []) |
37
|
27 |
|
{ |
38
|
27 |
|
$this->attributeFactory = $attributeFactory; |
39
|
27 |
|
$this->import($attributes); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
27 |
|
*/ |
45
|
|
|
public function import($attributes) |
46
|
27 |
|
{ |
47
|
4 |
|
foreach ($attributes as $name => $value) { |
48
|
|
|
$this->set($name, $value); |
49
|
|
|
} |
50
|
27 |
|
|
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
8 |
|
*/ |
57
|
|
|
public function set($name, ...$value) |
58
|
8 |
|
{ |
59
|
|
|
$this->storage[$name] = $this->attributeFactory->getInstance($name, $value); |
60
|
8 |
|
|
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
2 |
|
*/ |
67
|
|
|
public function offsetGet($name) |
68
|
2 |
|
{ |
69
|
2 |
|
if (!isset($this->storage[$name])) { |
70
|
|
|
$this->set($name); |
71
|
|
|
} |
72
|
2 |
|
|
73
|
|
|
return $this->storage[$name]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
1 |
|
*/ |
79
|
|
|
public function offsetSet($name, $value = null) |
80
|
1 |
|
{ |
81
|
1 |
|
$this->set($name, $value); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
1 |
|
*/ |
87
|
|
|
public function offsetUnset($name) |
88
|
1 |
|
{ |
89
|
1 |
|
unset($this->storage[$name]); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritdoc} |
94
|
2 |
|
*/ |
95
|
|
|
public function offsetExists($name) |
96
|
2 |
|
{ |
97
|
|
|
return isset($this->storage[$name]); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritdoc} |
102
|
14 |
|
*/ |
103
|
|
|
public function append($key, ...$value) |
104
|
|
|
{ |
105
|
14 |
|
$this->storage += array( |
106
|
|
|
$key => $this->attributeFactory->getInstance($key), |
107
|
|
|
); |
108
|
14 |
|
|
109
|
|
|
$this->storage[$key]->append($value); |
110
|
14 |
|
|
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* {@inheritdoc} |
116
|
2 |
|
*/ |
117
|
|
|
public function remove($key, ...$value) |
118
|
2 |
|
{ |
119
|
1 |
|
if (isset($this->storage[$key])) { |
120
|
|
|
$this->storage[$key]->remove($value); |
121
|
|
|
} |
122
|
2 |
|
|
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* {@inheritdoc} |
128
|
2 |
|
*/ |
129
|
|
|
public function delete(...$name) |
130
|
2 |
|
{ |
131
|
2 |
|
foreach ($this->normalizeValue($name) as $attribute_name) { |
132
|
|
|
unset($this->storage[$attribute_name]); |
133
|
|
|
} |
134
|
2 |
|
|
135
|
|
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* {@inheritdoc} |
140
|
1 |
|
*/ |
141
|
|
|
public function without(...$key) |
142
|
1 |
|
{ |
143
|
|
|
$attributes = clone $this; |
144
|
1 |
|
|
145
|
|
|
return $attributes->delete($key); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* {@inheritdoc} |
150
|
1 |
|
*/ |
151
|
|
|
public function replace($key, $value, ...$replacement) |
152
|
1 |
|
{ |
153
|
1 |
|
if (!isset($this->storage[$key])) { |
154
|
|
|
return $this; |
155
|
|
|
} |
156
|
1 |
|
|
157
|
1 |
|
if (!$this->contains($key, $value)) { |
158
|
|
|
return $this; |
159
|
|
|
} |
160
|
1 |
|
|
161
|
|
|
$this->storage[$key]->replace($value, $replacement); |
162
|
1 |
|
|
163
|
|
|
return $this; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* {@inheritdoc} |
168
|
1 |
|
*/ |
169
|
|
|
public function merge(array $data = []) |
170
|
1 |
|
{ |
171
|
1 |
|
foreach ($data as $key => $value) { |
172
|
|
|
$this->append($key, $value); |
173
|
|
|
} |
174
|
1 |
|
|
175
|
|
|
return $this; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* {@inheritdoc} |
180
|
1 |
|
*/ |
181
|
|
|
public function exists($key, $value = null) |
182
|
1 |
|
{ |
183
|
1 |
|
if (!isset($this->storage[$key])) { |
184
|
|
|
return false; |
185
|
|
|
} |
186
|
1 |
|
|
187
|
1 |
|
if (null !== $value) { |
188
|
|
|
return $this->contains($key, $value); |
189
|
|
|
} |
190
|
1 |
|
|
191
|
|
|
return true; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* {@inheritdoc} |
196
|
3 |
|
*/ |
197
|
|
|
public function contains($key, ...$value) |
198
|
3 |
|
{ |
199
|
1 |
|
if (!isset($this->storage[$key])) { |
200
|
|
|
return false; |
201
|
|
|
} |
202
|
3 |
|
|
203
|
|
|
return $this->storage[$key]->contains($value); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* {@inheritdoc} |
208
|
5 |
|
*/ |
209
|
|
|
public function __toString() |
210
|
5 |
|
{ |
211
|
|
|
return $this->render(); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* {@inheritdoc} |
216
|
16 |
|
*/ |
217
|
|
|
public function render() |
218
|
16 |
|
{ |
219
|
|
|
$attributes = implode( |
220
|
16 |
|
' ', |
221
|
|
|
$this->ensureArray( |
222
|
|
|
array_reduce( |
223
|
|
|
$this->toArray(), |
224
|
|
|
function ($carry, $key) |
225
|
|
|
{ |
|
|
|
|
226
|
1 |
|
$carry[] = $key->render(); |
227
|
|
|
|
228
|
1 |
|
return $carry; |
229
|
|
|
} |
230
|
|
|
) |
231
|
|
|
) |
232
|
|
|
); |
233
|
|
|
|
234
|
1 |
|
return $attributes ? ' ' . $attributes : ''; |
235
|
|
|
} |
236
|
1 |
|
|
237
|
|
|
/** |
238
|
|
|
* {@inheritdoc} |
239
|
|
|
*/ |
240
|
|
|
public function getStorage() |
241
|
|
|
{ |
242
|
1 |
|
return $this->storage; |
243
|
|
|
} |
244
|
1 |
|
|
245
|
|
|
/** |
246
|
|
|
* {@inheritdoc} |
247
|
|
|
*/ |
248
|
|
|
public function getIterator() |
249
|
|
|
{ |
250
|
2 |
|
return new \ArrayIterator($this->toArray()); |
251
|
|
|
} |
252
|
2 |
|
|
253
|
|
|
/** |
254
|
2 |
|
* {@inheritdoc} |
255
|
1 |
|
*/ |
256
|
|
|
public function count() |
257
|
|
|
{ |
258
|
2 |
|
return count($this->storage); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* {@inheritdoc} |
263
|
|
|
*/ |
264
|
|
|
public function toArray() |
265
|
|
|
{ |
266
|
|
|
// If empty, just return an empty array. |
267
|
18 |
|
if ([] === $this->storage) { |
268
|
|
|
return []; |
269
|
|
|
} |
270
|
18 |
|
|
271
|
|
|
$result = []; |
272
|
|
|
|
273
|
18 |
|
foreach ($this->preprocess($this->storage) as $attribute) { |
274
|
8 |
|
$result[$attribute->getName()] = $attribute; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
return $result; |
278
|
14 |
|
} |
279
|
|
|
|
280
|
14 |
|
/** |
281
|
|
|
* {@inheritdoc} |
282
|
14 |
|
*/ |
283
|
14 |
|
public function getValuesAsArray() |
284
|
12 |
|
{ |
285
|
12 |
|
return array_map( |
286
|
12 |
|
function (AttributeInterface $attribute) { |
287
|
12 |
|
return $attribute->getValuesAsArray(); |
288
|
|
|
}, |
289
|
|
|
$this->toArray() |
290
|
14 |
|
); |
291
|
14 |
|
} |
292
|
14 |
|
|
293
|
|
|
/** |
294
|
|
|
* {@inheritdoc} |
295
|
|
|
*/ |
296
|
|
|
protected function preprocess(array $values, $name = null) |
297
|
|
|
{ |
298
|
|
|
return $values; |
299
|
|
|
} |
300
|
|
|
} |
301
|
|
|
|