Completed
Push — master ( ed2cf8...f2ba0d )
by Pol
02:03
created

Attribute   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 267
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 26
lcom 1
cbo 1
dl 0
loc 267
ccs 93
cts 93
cp 1
rs 10
c 0
b 0
f 0

22 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A offsetExists() 0 4 1
A offsetGet() 0 4 1
A offsetSet() 0 4 1
A offsetUnset() 0 4 1
A set() 0 6 1
A getName() 0 4 1
A getValuesAsArray() 0 9 1
A getValuesAsString() 0 14 1
A render() 0 11 2
A __toString() 0 4 1
A isBoolean() 0 4 1
A append() 0 6 1
A remove() 0 9 1
A replace() 0 12 2
A contains() 0 15 1
A setBoolean() 0 6 2
A delete() 0 7 1
A alter() 0 13 2
A serialize() 0 7 1
A unserialize() 0 7 1
A escape() 0 4 1
1
<?php
2
3
namespace drupol\htmltag\Attribute;
4
5
use drupol\htmltag\AbstractBaseHtmlTagObject;
6
7
/**
8
 * Class Attribute.
9
 */
10
class Attribute extends AbstractBaseHtmlTagObject implements AttributeInterface
11
{
12
    /**
13
     * Store the attribute name.
14
     *
15
     * @var string|null
16
     */
17
    private $name;
18
19
    /**
20
     * Store the attribute value.
21
     *
22
     * @var array|null
23
     */
24
    private $values;
25
26
    /**
27
     * Attribute constructor.
28
     *
29
     * @param string $name
30
     *   The attribute name.
31
     * @param string|string[]|mixed[] ...$values
32
     *   The attribute values.
33
     */
34 42
    public function __construct($name, ...$values)
35
    {
36 42
        $this->name = \trim($this->escape($name));
37 42
        $this->values = array_filter($values);
38 42
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 1
    public function offsetExists($offset)
44
    {
45 1
        return $this->contains($offset);
46
    }
47
    /**
48
     * {@inheritdoc}
49
     */
50 1
    public function offsetGet($offset)
51
    {
52 1
        throw new \BadMethodCallException('Unsupported method.');
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 1
    public function offsetSet($offset, $value)
59
    {
60 1
        $this->append($value);
61 1
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 1
    public function offsetUnset($offset)
67
    {
68 1
        $this->remove($offset);
69 1
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74 8
    public function set(...$value)
75
    {
76 8
        $this->values = $value;
77
78 8
        return $this;
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84 34
    public function getName()
85
    {
86 34
        return (string) $this->name;
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92 10
    public function getValuesAsArray()
93
    {
94 10
        return array_values(
95 10
            $this->preprocess(
96 10
                $this->normalizeValue($this->values),
97 10
                $this->getName()
98
            )
99
        );
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105 1
    public function getValuesAsString()
106
    {
107 1
        return $this->escape(
108 1
            \implode(
109 1
                ' ',
110 1
                array_filter(
111 1
                    $this->preprocess(
112 1
                        $this->normalizeValue($this->values),
113 1
                        $this->getName()
114
                    )
115
                )
116
            )
117
        );
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123 29
    public function render()
124
    {
125 29
        $values = $this->preprocess(
126 29
            $this->normalizeValue($this->values),
127 29
            $this->getName()
128
        );
129
130 29
        return empty($values) ?
131 13
            $this->getName() :
132 29
            $this->getName() . '="' . $this->escape(\implode(' ', array_filter($values))) . '"';
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138 1
    public function __toString()
139
    {
140 1
        return $this->render();
141
    }
142
143
    /**
144
     * {@inheritdoc}
145
     */
146 1
    public function isBoolean()
147
    {
148 1
        return [] === $this->getValuesAsArray();
149
    }
150
151
    /**
152
     * {@inheritdoc}
153
     */
154 29
    public function append(...$value)
155
    {
156 29
        $this->values[] = $value;
157
158 29
        return $this;
159
    }
160
161
    /**
162
     * {@inheritdoc}
163
     */
164 5
    public function remove(...$value)
165
    {
166 5
        return $this->set(
167 5
            \array_diff(
168 5
                $this->normalizeValue($this->values),
169 5
                $this->normalizeValue($value)
170
            )
171
        );
172
    }
173
174
    /**
175
     * {@inheritdoc}
176
     */
177 2
    public function replace($original, ...$replacement)
178
    {
179 2
        $count_start = \count($this->normalizeValue($this->values));
180 2
        $this->remove($original);
181 2
        $count_end = \count($this->normalizeValue($this->values));
182
183 2
        if ($count_end !== $count_start) {
184 2
            $this->append($replacement);
185
        }
186
187 2
        return $this;
188
    }
189
190
    /**
191
     * {@inheritdoc}
192
     */
193 5
    public function contains(...$substring)
194
    {
195 5
        $values = $this->normalizeValue($this->values);
196
197 5
        return !in_array(
198 5
            false,
199 5
            \array_map(
200
                function ($substring_item) use ($values) {
201 5
                    return \in_array($substring_item, $values, true);
202 5
                },
203 5
                $this->normalizeValue($substring)
204
            ),
205 5
            true
206
        );
207
    }
208
209
    /**
210
     * {@inheritdoc}
211
     */
212 2
    public function setBoolean($boolean = true)
213
    {
214 2
        return true === $boolean ?
215 2
            $this->set(null) :
216 2
            $this;
217
    }
218
219
    /**
220
     * {@inheritdoc}
221
     */
222 1
    public function delete()
223
    {
224 1
        $this->name = null;
225 1
        $this->values = null;
226
227 1
        return $this;
228
    }
229
230
    /**
231
     * {@inheritdoc}
232
     */
233 1
    public function alter(callable ...$closures)
234
    {
235 1
        $name = $this->getName();
236
237 1
        foreach ($closures as $closure) {
238 1
            $this->values = $closure(
239 1
                $this->normalizeValue($this->values),
240 1
                $name
241
            );
242
        }
243
244 1
        return $this;
245
    }
246
247
    /**
248
     * {@inheritdoc}
249
     */
250 1
    public function serialize()
251
    {
252 1
        return \serialize([
253 1
            'name' => $this->getName(),
254 1
            'values' => $this->getValuesAsArray(),
255
        ]);
256
    }
257
258
    /**
259
     * {@inheritdoc}
260
     */
261 1
    public function unserialize($serialized)
262
    {
263 1
        $unserialized = \unserialize($serialized);
264
265 1
        $this->name = $unserialized['name'];
266 1
        $this->values = $unserialized['values'];
267 1
    }
268
269
    /**
270
     * {@inheritdoc}
271
     */
272 42
    protected function escape($value)
273
    {
274 42
        return htmlspecialchars((string) $value, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
275
    }
276
}
277