Completed
Push — master ( ff3a40...ebaa84 )
by Pol
03:05
created

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