Completed
Push — master ( fe70b5...51d4a3 )
by Pol
02:06
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 implements AttributeInterface
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 29
    public function __construct($name, $value = null)
33
    {
34 29
        $this->name = trim($name);
35 29
        $this->set($value);
36 29
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 15
    public function getName()
42
    {
43 15
        return $this->name;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 17
    public function getValueAsString()
50
    {
51 17
        return implode(
52 17
            ' ',
53 17
            $this->getValueAsArray()
54
        );
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 21
    public function getValueAsArray()
61
    {
62 21
        return array_unique(
63 21
            $this->normalizeValue($this->values)
64
        );
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 20
    public function render()
71
    {
72 20
        $output = $this->name;
73
74 20
        if (!$this->isBoolean()) {
75 17
            $output .= '="' . trim($this->getValueAsString()) . '"';
76
        }
77
78 20
        return $output;
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84 13
    public function __toString()
85
    {
86 13
        return $this->render();
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92 20
    public function isBoolean()
93
    {
94 20
        if ([] === $this->getValueAsArray()) {
95 12
            $this->values = null;
96
        }
97
98 20
        return null === $this->values;
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 29
    public function set($value = null)
105
    {
106 29
        $this->values = $this->normalizeValue($value);
107
108 29
        return $this;
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114 21
    public function append($value)
115
    {
116 21
        $this->values[] = $value;
117
118 21
        return $this;
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124 2
    public function merge(array $values)
125
    {
126 2
        $this->values[] = $values;
127
128 2
        return $this;
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134 3
    public function remove($value)
135
    {
136 3
        $this->values = array_diff(
137 3
            $this->normalizeValue($this->values),
138 3
            $this->normalizeValue($value)
139
        );
140
141 3
        return $this;
142
    }
143
144
    /**
145
     * {@inheritdoc}
146
     */
147 1
    public function replace($original, $replacement)
148
    {
149 1
        $this->values = array_map(
150
            function (&$value_item) use ($original, $replacement) {
151 1
                if ($value_item === $original) {
152 1
                    $value_item = $replacement;
153
                }
154
155 1
                return $value_item;
156 1
            },
157 1
            $this->normalizeValue($this->values)
158
        );
159
160 1
        return $this;
161
    }
162
163
    /**
164
     * {@inheritdoc}
165
     */
166 4
    public function contains($substring)
167
    {
168 4
        $this->values = $this->normalizeValue($this->values);
169
170 4
        foreach ($this->values as $value_item) {
171 4
            if (false !== strpos($value_item, $substring)) {
172 4
                return true;
173
            }
174
        }
175
176 3
        return false;
177
    }
178
179
    /**
180
     * {@inheritdoc}
181
     */
182 1
    public function setBoolean($boolean = true)
183
    {
184 1
        if (true === $boolean) {
185 1
            $this->values = null;
186
        }
187
188 1
        return $this;
189
    }
190
191
    /**
192
     * {@inheritdoc}
193
     */
194 1
    public function delete()
195
    {
196 1
        $this->name = '';
197 1
        $this->values = null;
198
199 1
        return $this;
200
    }
201
202
    /**
203
     * Normalize a value.
204
     *
205
     * @param mixed $values
206
     *  The value to normalize.
207
     *
208
     * @return array
209
     *   The value normalized.
210
     */
211 29
    private function normalizeValue($values)
212
    {
213 29
        $values = array_map(
214 29
            array($this, 'ensureArray'),
215 29
            $this->ensureFlatArray((array) $values)
216
        );
217
218 29
        return array_reduce(
219 29
            $values,
220
            function ($carry, $item) {
221 24
                return array_merge($carry, $item);
222 29
            },
223 29
            []
224
        );
225
    }
226
227
    /**
228
     * Todo.
229
     *
230
     * @param array $arr
231
     *   Todo.
232
     *
233
     * @return mixed[]
234
     *   Todo.
235
     */
236 29
    private function ensureFlatArray(array $arr)
237
    {
238 29
        return array_reduce(
239 29
            $arr,
240
            function ($c, $a) {
241 24
                return is_array($a) ?
242 3
                    array_merge($c, $this->ensureFlatArray($a)) :
243 24
                    array_merge($c, [$a]);
244 29
            },
245 29
            []
246
        );
247
    }
248
249
    /**
250
     * Todo.
251
     *
252
     * @param mixed $value
253
     *   Todo.
254
     *
255
     * @return array
256
     *   Todo.
257
     */
258 24
    private function ensureArray($value)
259
    {
260 24
        switch (gettype($value)) {
261 24
            case 'string':
262 23
                $return = explode(' ', $value);
263 23
                break;
264
265 8
            case 'array':
266 8
            case 'double':
267 8
            case 'integer':
268 2
                $return = array((string) $value);
269 2
                break;
270 8 View Code Duplication
            case 'object':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
271 1
                $return = array();
272 1
                if (method_exists($value, '__toString')) {
273
                    $return = array((string) $value);
274
                }
275 1
                break;
276 8
            case 'boolean':
277 8
            case 'resource':
278 8
            case 'NULL':
279
            default:
280 8
                $return = array();
281 8
                break;
282
        }
283
284 24
        return $return;
285
    }
286
}
287