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