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 = []; |
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 = []) |
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 |
|
$this->storage[$key]->remove($value); |
120
|
|
|
} |
121
|
|
|
|
122
|
2 |
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* {@inheritdoc} |
127
|
|
|
*/ |
128
|
2 |
|
public function delete(...$name) |
129
|
|
|
{ |
130
|
2 |
|
foreach ($this->normalizeValue($name) as $attribute_name) { |
131
|
2 |
|
unset($this->storage[$attribute_name]); |
132
|
|
|
} |
133
|
|
|
|
134
|
2 |
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* {@inheritdoc} |
139
|
|
|
*/ |
140
|
1 |
|
public function without(...$key) |
141
|
|
|
{ |
142
|
1 |
|
$attributes = clone $this; |
143
|
|
|
|
144
|
1 |
|
return $attributes->delete($key); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* {@inheritdoc} |
149
|
|
|
*/ |
150
|
1 |
|
public function replace($key, $value, ...$replacement) |
151
|
|
|
{ |
152
|
1 |
|
if (!isset($this->storage[$key])) { |
153
|
1 |
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
1 |
|
if (!$this->contains($key, $value)) { |
157
|
1 |
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
1 |
|
$this->storage[$key]->replace($value, $replacement); |
161
|
|
|
|
162
|
1 |
|
return $this; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* {@inheritdoc} |
167
|
|
|
*/ |
168
|
1 |
|
public function merge(array $data = []) |
169
|
|
|
{ |
170
|
1 |
|
foreach ($data as $key => $value) { |
171
|
1 |
|
$this->append($key, $value); |
172
|
|
|
} |
173
|
|
|
|
174
|
1 |
|
return $this; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* {@inheritdoc} |
179
|
|
|
*/ |
180
|
1 |
|
public function exists($key, $value = null) |
181
|
|
|
{ |
182
|
1 |
|
if (!isset($this->storage[$key])) { |
183
|
1 |
|
return false; |
184
|
|
|
} |
185
|
|
|
|
186
|
1 |
|
if (null !== $value) { |
187
|
1 |
|
return $this->contains($key, $value); |
188
|
|
|
} |
189
|
|
|
|
190
|
1 |
|
return true; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* {@inheritdoc} |
195
|
|
|
*/ |
196
|
3 |
|
public function contains($key, ...$value) |
197
|
|
|
{ |
198
|
3 |
|
if (!isset($this->storage[$key])) { |
199
|
1 |
|
return false; |
200
|
|
|
} |
201
|
|
|
|
202
|
3 |
|
return $this->storage[$key]->contains($value); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* {@inheritdoc} |
207
|
|
|
*/ |
208
|
5 |
|
public function __toString() |
209
|
|
|
{ |
210
|
5 |
|
return $this->render(); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* {@inheritdoc} |
215
|
|
|
*/ |
216
|
16 |
|
public function render() |
217
|
|
|
{ |
218
|
16 |
|
$attributes = implode(' ', $this->prepareValues()); |
219
|
|
|
|
220
|
16 |
|
return $attributes ? ' ' . $attributes : ''; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* {@inheritdoc} |
225
|
|
|
*/ |
226
|
1 |
|
public function getStorage() |
227
|
|
|
{ |
228
|
1 |
|
return $this->storage; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* {@inheritdoc} |
233
|
|
|
*/ |
234
|
1 |
|
public function getIterator() |
235
|
|
|
{ |
236
|
1 |
|
return new \ArrayIterator($this->toArray()); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* {@inheritdoc} |
241
|
|
|
*/ |
242
|
1 |
|
public function count() |
243
|
|
|
{ |
244
|
1 |
|
return count($this->storage); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* {@inheritdoc} |
249
|
|
|
*/ |
250
|
2 |
|
public function toArray() |
251
|
|
|
{ |
252
|
2 |
|
$result = []; |
253
|
|
|
|
254
|
2 |
|
foreach ($this->prepareValues() as $attribute) { |
255
|
1 |
|
$result[$attribute->getName()] = $attribute->getValueAsArray(); |
256
|
|
|
} |
257
|
|
|
|
258
|
2 |
|
return $result; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Returns all storage elements as an array. |
263
|
|
|
* |
264
|
|
|
* @return \drupol\htmltag\Attribute\AttributeInterface[] |
265
|
|
|
* An associative array of attributes. |
266
|
|
|
*/ |
267
|
18 |
|
private function prepareValues() |
268
|
|
|
{ |
269
|
|
|
/** @var \drupol\htmltag\Attribute\AttributeInterface[] $attributes */ |
270
|
18 |
|
$attributes = $this->storage; |
271
|
|
|
|
272
|
|
|
// If empty, just return an empty array. |
273
|
18 |
|
if ([] === $attributes) { |
274
|
8 |
|
return []; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
// Sort the attributes. |
278
|
14 |
|
ksort($attributes); |
279
|
|
|
|
280
|
14 |
|
return array_map( |
281
|
|
|
function ($attribute) { |
282
|
14 |
|
switch ($attribute->getName()) { |
283
|
14 |
|
case 'class': |
284
|
12 |
|
$classes = $attribute->getValueAsArray(); |
285
|
12 |
|
asort($classes); |
286
|
12 |
|
$attribute->set($classes); |
287
|
12 |
|
break; |
288
|
|
|
} |
289
|
|
|
|
290
|
14 |
|
return $attribute; |
291
|
14 |
|
}, |
292
|
14 |
|
$attributes |
293
|
|
|
); |
294
|
|
|
} |
295
|
|
|
} |
296
|
|
|
|