|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace drupol\htmltag; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class Attributes. |
|
7
|
|
|
*/ |
|
8
|
|
|
class Attributes implements AttributesInterface |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Stores the attribute data. |
|
12
|
|
|
* |
|
13
|
|
|
* @var \drupol\htmltag\Attribute[] |
|
14
|
|
|
*/ |
|
15
|
|
|
private $storage = array(); |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* {@inheritdoc} |
|
19
|
|
|
*/ |
|
20
|
26 |
|
public function __construct(array $attributes = array()) |
|
21
|
|
|
{ |
|
22
|
26 |
|
$this->import($attributes); |
|
23
|
26 |
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* {@inheritdoc} |
|
27
|
|
|
*/ |
|
28
|
26 |
|
public function import($attributes = array()) |
|
29
|
|
|
{ |
|
30
|
26 |
|
foreach ($attributes as $name => $value) { |
|
31
|
4 |
|
$this->set($name, $value); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
26 |
|
return $this; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* {@inheritdoc} |
|
39
|
|
|
*/ |
|
40
|
7 |
|
public function set($name, $value = null) |
|
41
|
|
|
{ |
|
42
|
7 |
|
$this->storage[$name] = new Attribute( |
|
43
|
7 |
|
$name, |
|
44
|
7 |
|
$this->ensureString($value) |
|
45
|
|
|
); |
|
46
|
|
|
|
|
47
|
7 |
|
return $this; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* {@inheritdoc} |
|
52
|
|
|
*/ |
|
53
|
2 |
|
public function offsetGet($name) |
|
54
|
|
|
{ |
|
55
|
2 |
|
if (!isset($this->storage[$name])) { |
|
56
|
2 |
|
$this->set($name); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
2 |
|
return $this->storage[$name]; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* {@inheritdoc} |
|
64
|
|
|
*/ |
|
65
|
1 |
|
public function offsetSet($name, $value = null) |
|
66
|
|
|
{ |
|
67
|
1 |
|
$this->set($name, $value); |
|
68
|
1 |
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* {@inheritdoc} |
|
72
|
|
|
*/ |
|
73
|
1 |
|
public function offsetUnset($name) |
|
74
|
|
|
{ |
|
75
|
1 |
|
unset($this->storage[$name]); |
|
76
|
1 |
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* {@inheritdoc} |
|
80
|
|
|
*/ |
|
81
|
2 |
|
public function offsetExists($name) |
|
82
|
|
|
{ |
|
83
|
2 |
|
return isset($this->storage[$name]); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* {@inheritdoc} |
|
88
|
|
|
*/ |
|
89
|
14 |
|
public function append($key, $value = '') |
|
90
|
|
|
{ |
|
91
|
|
|
$this->storage += array( |
|
92
|
14 |
|
$key => new Attribute( |
|
93
|
14 |
|
$key, |
|
94
|
14 |
|
$this->ensureString($value) |
|
95
|
|
|
) |
|
96
|
|
|
); |
|
97
|
|
|
|
|
98
|
14 |
|
foreach ($this->normalizeValue($value) as $value_item) { |
|
99
|
14 |
|
$this->storage[$key]->append($value_item); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
14 |
|
return $this; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* {@inheritdoc} |
|
107
|
|
|
*/ |
|
108
|
2 |
|
public function remove($key, $value = '') |
|
109
|
|
|
{ |
|
110
|
2 |
|
if (!isset($this->storage[$key])) { |
|
111
|
1 |
|
return $this; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
1 |
|
foreach ($this->normalizeValue($value) as $value_item) { |
|
115
|
1 |
|
$this->storage[$key]->remove($value_item); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
1 |
|
return $this; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* {@inheritdoc} |
|
123
|
|
|
*/ |
|
124
|
2 |
|
public function delete($name) |
|
125
|
|
|
{ |
|
126
|
2 |
|
foreach ($this->normalizeValue($name) as $attribute_name) { |
|
127
|
2 |
|
unset($this->storage[$attribute_name]); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
2 |
|
return $this; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* {@inheritdoc} |
|
135
|
|
|
*/ |
|
136
|
1 |
|
public function without($key) |
|
137
|
|
|
{ |
|
138
|
1 |
|
$attributes = clone $this; |
|
139
|
|
|
|
|
140
|
1 |
|
return $attributes->delete($key); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* {@inheritdoc} |
|
145
|
|
|
*/ |
|
146
|
1 |
|
public function replace($key, $value, $replacement) |
|
147
|
|
|
{ |
|
148
|
1 |
|
if (!isset($this->storage[$key])) { |
|
149
|
1 |
|
return $this; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
1 |
|
if (!$this->contains($key, $value)) { |
|
153
|
1 |
|
return $this; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
1 |
|
$this->storage[$key]->remove($value); |
|
157
|
1 |
|
foreach ($this->normalizeValue($replacement) as $replacement_value) { |
|
158
|
1 |
|
$this->storage[$key]->append($replacement_value); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
1 |
|
return $this; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* {@inheritdoc} |
|
166
|
|
|
*/ |
|
167
|
1 |
|
public function merge(array $data = array()) |
|
168
|
|
|
{ |
|
169
|
1 |
|
foreach ($data as $key => $value) { |
|
170
|
|
|
$this->storage += array( |
|
171
|
1 |
|
$key => new Attribute( |
|
172
|
1 |
|
$key |
|
173
|
|
|
) |
|
174
|
|
|
); |
|
175
|
|
|
|
|
176
|
1 |
|
$this->storage[$key]->merge( |
|
177
|
1 |
|
$this->normalizeValue($value) |
|
178
|
|
|
); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
1 |
|
return $this; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* {@inheritdoc} |
|
186
|
|
|
*/ |
|
187
|
1 |
|
public function exists($key, $value = null) |
|
188
|
|
|
{ |
|
189
|
1 |
|
if (!isset($this->storage[$key])) { |
|
190
|
1 |
|
return false; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
1 |
|
if (null !== $value) { |
|
194
|
1 |
|
return $this->contains($key, $value); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
1 |
|
return true; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* {@inheritdoc} |
|
202
|
|
|
*/ |
|
203
|
3 |
|
public function contains($key, $value) |
|
204
|
|
|
{ |
|
205
|
3 |
|
if (!isset($this->storage[$key])) { |
|
206
|
1 |
|
return false; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
3 |
|
return $this->storage[$key]->contains($value); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* {@inheritdoc} |
|
214
|
|
|
*/ |
|
215
|
7 |
|
public function __toString() |
|
216
|
|
|
{ |
|
217
|
7 |
|
return $this->render(); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* {@inheritdoc} |
|
222
|
|
|
*/ |
|
223
|
15 |
|
public function render() |
|
224
|
|
|
{ |
|
225
|
15 |
|
$attributes = implode(' ', $this->prepareValues()); |
|
226
|
|
|
|
|
227
|
15 |
|
return $attributes ? ' ' . $attributes : ''; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* {@inheritdoc} |
|
232
|
|
|
*/ |
|
233
|
2 |
|
public function toArray() |
|
234
|
|
|
{ |
|
235
|
2 |
|
$attributes = $this->storage; |
|
236
|
|
|
|
|
237
|
|
|
// If empty, just return an empty array. |
|
238
|
2 |
|
if (empty($attributes)) { |
|
239
|
2 |
|
return array(); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
1 |
|
$result = []; |
|
243
|
|
|
|
|
244
|
1 |
|
foreach ($this->prepareValues() as $attribute) { |
|
245
|
1 |
|
$result[$attribute->getName()] = $attribute->getValueAsArray(); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
1 |
|
return $result; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* {@inheritdoc} |
|
253
|
|
|
*/ |
|
254
|
1 |
|
public function getStorage() |
|
255
|
|
|
{ |
|
256
|
1 |
|
return $this->storage; |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* {@inheritdoc} |
|
261
|
|
|
*/ |
|
262
|
1 |
|
public function getIterator() |
|
263
|
|
|
{ |
|
264
|
1 |
|
return new \ArrayIterator($this->toArray()); |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* {@inheritdoc} |
|
269
|
|
|
*/ |
|
270
|
1 |
|
public function count() |
|
271
|
|
|
{ |
|
272
|
1 |
|
return count($this->storage); |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
/** |
|
276
|
|
|
* Returns all storage elements as an array. |
|
277
|
|
|
* |
|
278
|
|
|
* @return \drupol\htmltag\Attribute[] |
|
279
|
|
|
* An associative array of attributes. |
|
280
|
|
|
*/ |
|
281
|
16 |
|
private function prepareValues() |
|
282
|
|
|
{ |
|
283
|
16 |
|
$attributes = $this->storage; |
|
284
|
|
|
|
|
285
|
|
|
// If empty, just return an empty array. |
|
286
|
16 |
|
if (empty($attributes)) { |
|
287
|
5 |
|
return array(); |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
// Sort the attributes. |
|
291
|
14 |
|
ksort($attributes); |
|
292
|
|
|
|
|
293
|
14 |
|
$result = []; |
|
294
|
|
|
|
|
295
|
14 |
|
foreach ($attributes as $attribute_name => $attribute) { |
|
296
|
14 |
|
switch ($attribute_name) { |
|
297
|
14 |
|
case 'class': |
|
298
|
12 |
|
$classes = $attribute->getValueAsArray(); |
|
299
|
12 |
|
asort($classes); |
|
300
|
12 |
|
$result[$attribute->getName()] = $attribute->set( |
|
301
|
12 |
|
implode(' ', $classes) |
|
302
|
|
|
); |
|
303
|
12 |
|
break; |
|
304
|
|
|
|
|
305
|
|
|
default: |
|
306
|
14 |
|
$result[$attribute->getName()] = $attribute; |
|
307
|
|
|
} |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
14 |
|
return $result; |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
|
|
/** |
|
314
|
|
|
* Normalize a value. |
|
315
|
|
|
* |
|
316
|
|
|
* @param mixed $value |
|
317
|
|
|
* The value to normalize. |
|
318
|
|
|
* |
|
319
|
|
|
* @return array |
|
320
|
|
|
* The value normalized. |
|
321
|
|
|
*/ |
|
322
|
18 |
|
private function normalizeValue($value) |
|
323
|
|
|
{ |
|
324
|
18 |
|
return $this->ensureFlatArray($value); |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
|
|
/** |
|
328
|
|
|
* Todo. |
|
329
|
|
|
* |
|
330
|
|
|
* @param mixed $value |
|
331
|
|
|
* Todo. |
|
332
|
|
|
* |
|
333
|
|
|
* @return array |
|
334
|
|
|
* The array, flattened. |
|
335
|
|
|
*/ |
|
336
|
18 |
|
private function ensureFlatArray($value) |
|
337
|
|
|
{ |
|
338
|
18 |
|
$type = gettype($value); |
|
339
|
|
|
|
|
340
|
18 |
|
$return = array(); |
|
341
|
|
|
|
|
342
|
18 |
|
switch ($type) { |
|
343
|
18 |
|
case 'string': |
|
344
|
18 |
|
$return = explode( |
|
345
|
18 |
|
' ', |
|
346
|
18 |
|
$this->ensureString($value) |
|
347
|
|
|
); |
|
348
|
18 |
|
break; |
|
349
|
|
|
|
|
350
|
7 |
|
case 'array': |
|
351
|
6 |
|
$flat_array = iterator_to_array( |
|
352
|
6 |
|
new \RecursiveIteratorIterator( |
|
353
|
6 |
|
new \RecursiveArrayIterator( |
|
354
|
6 |
|
$value |
|
355
|
|
|
) |
|
356
|
|
|
), |
|
357
|
6 |
|
false |
|
358
|
|
|
); |
|
359
|
|
|
|
|
360
|
6 |
|
$return = []; |
|
361
|
6 |
|
foreach ($flat_array as $item) { |
|
362
|
6 |
|
$return = array_merge( |
|
363
|
6 |
|
$return, |
|
364
|
6 |
|
$this->normalizeValue($item) |
|
365
|
|
|
); |
|
366
|
|
|
} |
|
367
|
6 |
|
break; |
|
368
|
|
|
|
|
369
|
2 |
|
case 'double': |
|
370
|
2 |
|
case 'integer': |
|
371
|
1 |
|
$return = array($value); |
|
372
|
1 |
|
break; |
|
373
|
1 |
|
case 'object': |
|
374
|
1 |
|
case 'boolean': |
|
375
|
1 |
|
case 'resource': |
|
376
|
1 |
|
case 'NULL': |
|
377
|
|
|
} |
|
378
|
|
|
|
|
379
|
18 |
|
return $return; |
|
380
|
|
|
} |
|
381
|
|
|
|
|
382
|
|
|
/** |
|
383
|
|
|
* Todo. |
|
384
|
|
|
* |
|
385
|
|
|
* @param mixed $value |
|
386
|
|
|
* Todo. |
|
387
|
|
|
* |
|
388
|
|
|
* @return string |
|
389
|
|
|
* A string. |
|
390
|
|
|
*/ |
|
391
|
20 |
|
private function ensureString($value) |
|
392
|
|
|
{ |
|
393
|
20 |
|
$type = gettype($value); |
|
394
|
|
|
|
|
395
|
20 |
|
$return = ''; |
|
396
|
|
|
|
|
397
|
20 |
|
switch ($type) { |
|
398
|
20 |
|
case 'string': |
|
399
|
18 |
|
$return = $value; |
|
400
|
18 |
|
break; |
|
401
|
|
|
|
|
402
|
8 |
|
case 'array': |
|
403
|
5 |
|
$return = implode( |
|
404
|
5 |
|
' ', |
|
405
|
5 |
|
$this->ensureFlatArray($value) |
|
406
|
|
|
); |
|
407
|
5 |
|
break; |
|
408
|
|
|
|
|
409
|
7 |
|
case 'double': |
|
410
|
7 |
|
case 'integer': |
|
411
|
1 |
|
$return = (string) $value; |
|
412
|
1 |
|
break; |
|
413
|
6 |
|
case 'object': |
|
414
|
6 |
|
case 'boolean': |
|
415
|
6 |
|
case 'resource': |
|
416
|
6 |
|
case 'NULL': |
|
417
|
|
|
} |
|
418
|
|
|
|
|
419
|
20 |
|
return $return; |
|
420
|
|
|
} |
|
421
|
|
|
} |
|
422
|
|
|
|