Completed
Push — master ( 2347bb...dd8a62 )
by Pol
01:31
created

Attribute::isBoolean()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 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 41
    public function __construct($name, ...$values)
35
    {
36 41
        $this->name = $name;
37 41
        $this->values = array_filter($values);
38 41
    }
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 33
    public function getName()
85
    {
86 33
        return \trim($this->sanitize($this->name));
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92 33
    public function getValuesAsArray()
93
    {
94 33
        $name = $this->getName();
95
96 33
        return $this->preprocess($this->normalizeValue($this->values), $name);
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102 29
    public function render()
103
    {
104 29
        $values = $this->getValuesAsArray();
105
106 29
        return empty($values) ?
107 13
            $this->getName() :
108 29
            $this->getName() . '="' . $this->sanitize(\implode(' ', $values)) . '"';
109
    }
110
111
    /**
112
     * Sanitize a value.
113
     *
114
     * @param string|null $value
115
     *   The value to sanitize
116
     *
117
     * @return string
118
     *   The value sanitized.
119
     */
120 33
    protected function sanitize($value)
121
    {
122 33
        return htmlspecialchars((string) $value, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128 1
    public function __toString()
129
    {
130 1
        return $this->render();
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136 1
    public function isBoolean()
137
    {
138 1
        return [] === $this->getValuesAsArray();
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144 28
    public function append(...$value)
145
    {
146 28
        $this->values[] = $value;
147
148 28
        return $this;
149
    }
150
151
    /**
152
     * {@inheritdoc}
153
     */
154 5
    public function remove(...$value)
155
    {
156 5
        return $this->set(
157 5
            \array_diff(
158 5
                $this->normalizeValue($this->values),
159 5
                $this->normalizeValue($value)
160
            )
161
        );
162
    }
163
164
    /**
165
     * {@inheritdoc}
166
     */
167 2
    public function replace($original, ...$replacement)
168
    {
169 2
        $count_start = \count($this->normalizeValue($this->values));
170 2
        $this->remove($original);
171 2
        $count_end = \count($this->normalizeValue($this->values));
172
173 2
        if ($count_end !== $count_start) {
174 2
            $this->append($replacement);
175
        }
176
177 2
        return $this;
178
    }
179
180
    /**
181
     * {@inheritdoc}
182
     */
183 5
    public function contains(...$substring)
184
    {
185 5
        $values = $this->normalizeValue($this->values);
186
187 5
        return !in_array(
188 5
            false,
189 5
            \array_map(
190
                function ($substring_item) use ($values) {
191 5
                    return \in_array($substring_item, $values, true);
192 5
                },
193 5
                $this->normalizeValue($substring)
194
            ),
195 5
            true
196
        );
197
    }
198
199
    /**
200
     * {@inheritdoc}
201
     */
202 2
    public function setBoolean($boolean = true)
203
    {
204 2
        return true === $boolean ?
205 2
            $this->set(null) :
206 2
            $this;
207
    }
208
209
    /**
210
     * {@inheritdoc}
211
     */
212 1
    public function delete()
213
    {
214 1
        $this->name = null;
215 1
        $this->values = null;
216
217 1
        return $this;
218
    }
219
220
    /**
221
     * {@inheritdoc}
222
     */
223 1
    public function alter(callable ...$closures)
224
    {
225 1
        $name = $this->getName();
226
227 1
        foreach ($closures as $closure) {
228 1
            $this->values = $closure(
229 1
                $this->normalizeValue($this->values),
230 1
                $name
231
            );
232
        }
233
234 1
        return $this;
235
    }
236
237
    /**
238
     * {@inheritdoc}
239
     */
240 1
    public function serialize()
241
    {
242 1
        return \serialize([
243 1
            'name' => $this->getName(),
244 1
            'values' => $this->getValuesAsArray(),
245
        ]);
246
    }
247
248
    /**
249
     * {@inheritdoc}
250
     */
251 1
    public function unserialize($serialized)
252
    {
253 1
        $unserialized = \unserialize($serialized);
254
255 1
        $this->name = $unserialized['name'];
256 1
        $this->values = $unserialized['values'];
257 1
    }
258
}
259