Completed
Push — master ( 56b2a2...4da2f1 )
by Pol
02:28
created

AbstractAttribute::contains()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 9
cts 9
cp 1
rs 9.7666
c 0
b 0
f 0
cc 1
nc 1
nop 1
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 47
    public function __construct($name, ...$values)
35
    {
36 47
        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 47
        $this->name = $name;
42 47
        $this->values = $values;
43 47
    }
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 31
    public function append(...$value)
72
    {
73 31
        $this->values[] = $value;
74
75 31
        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 5
    public function getName()
112
    {
113 5
        return $this->name;
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119 38
    public function getValuesAsArray()
120
    {
121 38
        return $this->ensureStrings(
122 38
            $this->preprocess(
123 38
                $this->ensureFlatArray($this->values),
124 38
                $this->name
125
            )
126
        );
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132 34
    public function getValuesAsString()
133
    {
134 34
        return ($values = $this->getValuesAsArray()) === [] ?
135 13
            null :
136 34
            $this->escape(\implode(' ', \array_filter($values, '\strlen')));
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142 1
    public function isBoolean()
143
    {
144 1
        return $this->getValuesAsArray() === [];
145
    }
146
147
    /**
148
     * {@inheritdoc}
149
     */
150 1
    public function offsetExists($offset)
151
    {
152 1
        return $this->contains($offset);
153
    }
154
155
    /**
156
     * {@inheritdoc}
157
     */
158 1
    public function offsetGet($offset)
159
    {
160 1
        throw new \BadMethodCallException('Unsupported method.');
161
    }
162
163
    /**
164
     * {@inheritdoc}
165
     */
166 1
    public function offsetSet($offset, $value)
167
    {
168 1
        $this->append($value);
169 1
    }
170
171
    /**
172
     * {@inheritdoc}
173
     */
174 1
    public function offsetUnset($offset)
175
    {
176 1
        $this->remove($offset);
177 1
    }
178
179
    /**
180
     * {@inheritdoc}
181
     */
182 5
    public function remove(...$value)
183
    {
184 5
        return $this->set(
185 5
            \array_diff(
186 5
                $this->ensureFlatArray($this->values),
187 5
                $this->ensureFlatArray($value)
188
            )
189
        );
190
    }
191
192
    /**
193
     * {@inheritdoc}
194
     */
195 33
    public function render()
196
    {
197 33
        return null === ($values = $this->getValuesAsString()) ?
198 13
            $this->name :
199 33
            $this->name . '="' . $values . '"';
200
    }
201
202
    /**
203
     * {@inheritdoc}
204
     */
205 2
    public function replace($original, ...$replacement)
206
    {
207 2
        $count_start = \count($this->ensureFlatArray($this->values));
208 2
        $this->remove($original);
209
210 2
        if (\count($this->ensureFlatArray($this->values)) !== $count_start) {
211 2
            $this->append($replacement);
212
        }
213
214 2
        return $this;
215
    }
216
217
    /**
218
     * {@inheritdoc}
219
     */
220 1
    public function serialize()
221
    {
222 1
        return \serialize([
223 1
            'name' => $this->name,
224 1
            'values' => $this->getValuesAsArray(),
225
        ]);
226
    }
227
228
    /**
229
     * {@inheritdoc}
230
     */
231 9
    public function set(...$value)
232
    {
233 9
        $this->values = $value;
234
235 9
        return $this;
236
    }
237
238
    /**
239
     * {@inheritdoc}
240
     */
241 2
    public function setBoolean($boolean = true)
242
    {
243 2
        return true === $boolean ?
244 2
            $this->set() :
245 2
            $this->append('');
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
    /**
260
     * Escape a value.
261
     *
262
     * @param null|string $value
263
     *   The value to sanitize
264
     *
265
     * @return mixed|string
266
     *   The value sanitized
267
     */
268 31
    protected function escape($value)
269
    {
270 31
        return null === $value ?
271
                $value :
272 31
                \htmlspecialchars($value, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
273
    }
274
}
275