Completed
Push — master ( 7a4805...2347bb )
by Pol
01:45
created

Attribute::offsetSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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((string) $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() . '="' . \addcslashes(\implode(' ', $values), '\"') . '"';
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114 1
    public function __toString()
115
    {
116 1
        return $this->render();
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122 1
    public function isBoolean()
123
    {
124 1
        return [] === $this->getValuesAsArray();
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130 28
    public function append(...$value)
131
    {
132 28
        $this->values[] = $value;
133
134 28
        return $this;
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140 5
    public function remove(...$value)
141
    {
142 5
        return $this->set(
143 5
            \array_diff(
144 5
                $this->normalizeValue($this->values),
145 5
                $this->normalizeValue($value)
146
            )
147
        );
148
    }
149
150
    /**
151
     * {@inheritdoc}
152
     */
153 2
    public function replace($original, ...$replacement)
154
    {
155 2
        $count_start = \count($this->normalizeValue($this->values));
156 2
        $this->remove($original);
157 2
        $count_end = \count($this->normalizeValue($this->values));
158
159 2
        if ($count_end !== $count_start) {
160 2
            $this->append($replacement);
161
        }
162
163 2
        return $this;
164
    }
165
166
    /**
167
     * {@inheritdoc}
168
     */
169 5
    public function contains(...$substring)
170
    {
171 5
        $values = $this->normalizeValue($this->values);
172
173 5
        return !in_array(
174 5
            false,
175 5
            \array_map(
176
                function ($substring_item) use ($values) {
177 5
                    return \in_array($substring_item, $values, true);
178 5
                },
179 5
                $this->normalizeValue($substring)
180
            ),
181 5
            true
182
        );
183
    }
184
185
    /**
186
     * {@inheritdoc}
187
     */
188 2
    public function setBoolean($boolean = true)
189
    {
190 2
        return true === $boolean ?
191 2
            $this->set(null) :
192 2
            $this;
193
    }
194
195
    /**
196
     * {@inheritdoc}
197
     */
198 1
    public function delete()
199
    {
200 1
        $this->name = null;
201 1
        $this->values = null;
202
203 1
        return $this;
204
    }
205
206
    /**
207
     * {@inheritdoc}
208
     */
209 1
    public function alter(callable ...$closures)
210
    {
211 1
        $name = $this->getName();
212
213 1
        foreach ($closures as $closure) {
214 1
            $this->values = $closure(
215 1
                $this->normalizeValue($this->values),
216 1
                $name
217
            );
218
        }
219
220 1
        return $this;
221
    }
222
223
    /**
224
     * {@inheritdoc}
225
     */
226 1
    public function serialize()
227
    {
228 1
        return \serialize([
229 1
            'name' => $this->getName(),
230 1
            'values' => $this->getValuesAsArray(),
231
        ]);
232
    }
233
234
    /**
235
     * {@inheritdoc}
236
     */
237 1
    public function unserialize($serialized)
238
    {
239 1
        $unserialized = \unserialize($serialized);
240
241 1
        $this->name = $unserialized['name'];
242 1
        $this->values = $unserialized['values'];
243 1
    }
244
}
245