Completed
Push — 1.x ( af4fd8...f9e31b )
by Pol
01:26
created

Attribute::withName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace drupol\htmltag;
4
5
/**
6
 * Class Attribute.
7
 */
8
class Attribute
9
{
10
    /**
11
     * Store the attribute name.
12
     *
13
     * @var string
14
     */
15
    private $name;
16
17
    /**
18
     * Store the attribute value.
19
     *
20
     * @var array|null
21
     */
22
    private $values;
23
24
    /**
25
     * Attribute constructor.
26
     *
27
     * @param string $name
28
     *   The attribute name.
29
     * @param string $value
30
     *   The attribute value.
31
     */
32 10
    public function __construct($name, $value = null)
33
    {
34 10
        $this->name = trim($name);
35 10
        $this->set($value);
36 10
    }
37
38
    /**
39
     * Create a copy of the current attribute with specific value.
40
     *
41
     * @param string $value
42
     *   The attribute value.
43
     *
44
     * @return \drupol\htmltag\Attribute
45
     *   The attribute.
46
     */
47 3
    public function withValue($value)
48
    {
49 3
        $clone = clone $this;
50
51 3
        $clone->values = explode(' ', trim($value));
52
53 3
        return $clone;
54
    }
55
56
    /**
57
     * Get the attribute name.
58
     *
59
     * @return string
60
     *   The attribute name.
61
     */
62 5
    public function getName()
63
    {
64 5
        return $this->name;
65
    }
66
67
    /**
68
     * Get the attribute value as a string.
69
     *
70
     * @return string
71
     *   The attribute value as a string.
72
     */
73 8
    public function getValueAsString()
74
    {
75 8
        return implode(
76 8
            ' ',
77 8
            $this->getValueAsArray()
78
        );
79
    }
80
81
    /**
82
     * Get the attribute value as an array.
83
     *
84
     * @return array
85
     *   The attribute value as an array.
86
     */
87 9
    public function getValueAsArray()
88
    {
89 9
        return array_values(
90 9
            array_filter(
91 9
                array_unique(
92 9
                    (array) $this->values
93
                )
94
            )
95
        );
96
    }
97
98
    /**
99
     * Convert the object in a string.
100
     *
101
     * @return string
102
     *   The attribute as a string.
103
     */
104 9
    public function __toString()
105
    {
106 9
        $output = $this->name;
107
108 9
        if (!$this->isLoner()) {
109 8
            $output .= '="' . trim($this->getValueAsString()) . '"';
110
        }
111
112 9
        return $output;
113
    }
114
115
    /**
116
     * Check if the attribute is a loner attribute.
117
     *
118
     * @return bool
119
     *   True or False.
120
     */
121 9
    public function isLoner()
122
    {
123 9
        if ([] === $this->getValueAsArray()) {
124 3
            $this->values = null;
125
        }
126
127 9
        return null === $this->values;
128
    }
129
130
    /**
131
     * Set the value.
132
     *
133
     * @param string|null $value
134
     *   The value.
135
     *
136
     * @return \drupol\htmltag\Attribute
137
     */
138 10
    public function set($value = null)
139
    {
140 10
        if (null !== $value) {
141 7
            $this->values = explode(' ', trim($value));
142
        }
143
144 10
        return $this;
145
    }
146
147
    /**
148
     * Append a value to the attribute.
149
     *
150
     * @param string $value
151
     *   The value to append.
152
     *
153
     * @return $this
154
     *   The attribute.
155
     */
156 5
    public function append($value)
157
    {
158 5
        $this->values = array_merge(
159 5
            (array) $this->values,
160 5
            explode(
161 5
                ' ',
162 5
                trim($value)
163
            )
164
        );
165
166 5
        return $this;
167
    }
168
169
    /**
170
     * Merge data into the attribute value.
171
     *
172
     * @param array $values
173
     *   The values to merge.
174
     *
175
     * @return $this
176
     *   The attribute.
177
     */
178
    public function merge(array $values)
179
    {
180
        $this->values = array_merge(
181
            (array) $this->values,
182
            $values
183
        );
184
185
        return $this;
186
    }
187
188
    /**
189
     * Remove a value from the attribute.
190
     *
191
     * @param string $value
192
     *   The value to remove.
193
     *
194
     * @return $this
195
     *   The attribute.
196
     */
197 2
    public function remove($value)
198
    {
199 2
        $this->values = array_filter(
200 2
            (array) $this->values,
201
            function ($value_item) use ($value) {
202 2
                return $value_item !== $value;
203 2
            }
204
        );
205
206 2
        return $this;
207
    }
208
209
    /**
210
     * Replace a value of the attribute.
211
     *
212
     * @param string $original
213
     *   The original value.
214
     * @param string $replacement
215
     *   The replacement value.
216
     *
217
     * @return $this
218
     *   The attribute.
219
     */
220 1
    public function replace($original, $replacement)
221
    {
222 1
        $this->values = array_map(
223
            function (&$value_item) use ($original, $replacement) {
224 1
                if ($value_item === $original) {
225 1
                    $value_item = $replacement;
226
                }
227
228 1
                return $value_item;
229 1
            },
230 1
            (array) $this->values
231
        );
232
233 1
        return $this;
234
    }
235
236
    /**
237
     * Check if the attribute contains a string or a substring.
238
     *
239
     * @param string $substring
240
     *   The string to check.
241
     *
242
     * @return bool
243
     *   True or False.
244
     */
245
    public function contains($substring)
246
    {
247
        foreach ((array) $this->values as $value_item) {
248
            if (false !== strpos($value_item, $substring)) {
249
                return true;
250
            }
251
        }
252
253
        return false;
254
    }
255
256
    /**
257
     * Set the attribute as a loner attribute.
258
     *
259
     * @param bool $loner
260
     *   True or False.
261
     *
262
     * @return $this
263
     *   The attribute.
264
     */
265 1
    public function setLoner($loner = true)
266
    {
267 1
        if (true === $loner) {
268 1
            $this->values = null;
269
        }
270
271 1
        return $this;
272
    }
273
274
    /**
275
     * Delete the current attribute.
276
     *
277
     * @return $this
278
     *   The attribute.
279
     */
280
    public function delete()
281
    {
282
        $this->name = '';
283
        $this->values = null;
284
285
        return $this;
286
    }
287
}
288