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