Completed
Push — master ( 56b2a2...4da2f1 )
by Pol
02:28
created

AbstractAttributes::offsetGet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1.0156

Importance

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