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