Completed
Push — master ( 5149ce...fe70b5 )
by Pol
01:34
created

Attribute::isLoner()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 10
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 $value
206
     *  The value to normalize.
207
     *
208
     * @return array
209
     *   The value normalized.
210
     */
211 29
    private function normalizeValue($value)
212
    {
213 29
        return $this->ensureFlatArray($value);
214
    }
215
216
    /**
217
     * Todo.
218
     *
219
     * @param mixed $value
220
     *   Todo.
221
     *
222
     * @return array
223
     *   The array, flattened.
224
     */
225 29 View Code Duplication
    private function ensureFlatArray($value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
226
    {
227 29
        switch (gettype($value)) {
228 29
            case 'string':
229 23
                $return = explode(
230 23
                    ' ',
231 23
                    $value
232
                );
233 23
                break;
234
235 28
            case 'array':
236 23
                $flat_array = iterator_to_array(
237 23
                    new \RecursiveIteratorIterator(
238 23
                        new \RecursiveArrayIterator(
239 23
                            $value
240
                        )
241
                    ),
242 23
                    false
243
                );
244
245 23
                $return = [];
246 23
                foreach ($flat_array as $item) {
247 23
                    $return = array_merge(
248 23
                        $return,
249 23
                        $this->normalizeValue($item)
250
                    );
251
                }
252 23
                break;
253
254 25
            case 'double':
255 25
            case 'integer':
256 2
                $return = array($value);
257 2
                break;
258 25
            case 'object':
259 25
            case 'boolean':
260 25
            case 'resource':
261 25
            case 'NULL':
262
            default:
263 25
                $return = array();
264 25
                break;
265
        }
266
267 29
        return $return;
268
    }
269
}
270