Completed
Push — 1.x ( fa3a77...584456 )
by Pol
15:01
created

Attribute::render()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

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