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