Completed
Push — master ( cf5a75...420187 )
by Pol
01:43
created

Attributes   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 266
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 98.77%

Importance

Changes 0
Metric Value
wmc 35
lcom 1
cbo 3
dl 0
loc 266
ccs 80
cts 81
cp 0.9877
rs 9.6
c 0
b 0
f 0

22 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A import() 0 8 2
A set() 0 6 1
A offsetGet() 0 8 2
A offsetSet() 0 4 1
A offsetUnset() 0 4 1
A offsetExists() 0 4 1
A append() 0 10 1
A remove() 0 8 2
A delete() 0 8 2
A without() 0 6 1
A replace() 0 14 3
A merge() 0 8 2
A exists() 0 12 3
A contains() 0 8 2
A __toString() 0 4 1
A render() 0 6 2
A getStorage() 0 4 1
A getIterator() 0 4 1
A count() 0 4 1
A toArray() 0 10 2
A preprocess() 0 12 2
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->getInstance($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->getInstance($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->preprocess($this->toArray()));
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 18
    public function toArray()
251
    {
252 18
        $result = [];
253
254 18
        foreach ($this->preprocess($this->storage) as $attribute) {
255 14
            $result[$attribute->getName()] = $attribute->getValueAsArray();
256
        }
257
258 18
        return $result;
259
    }
260
261
    /**
262
     * {@inheritdoc}
263
     */
264 18
    protected function preprocess(array $values, $name = null)
265
    {
266
        /** @var \drupol\htmltag\Attribute\AttributeInterface[] $attributes */
267 18
        $attributes = $this->storage;
268
269
        // If empty, just return an empty array.
270 18
        if ([] === $attributes) {
271 8
            return [];
272
        }
273
274 14
        return $attributes;
275
    }
276
}
277