Completed
Push — master ( 51d4a3...e0bbf9 )
by Pol
01:46
created

Attribute::ensureFlatArray()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 9.8666
c 0
b 0
f 0
cc 2
nc 1
nop 1
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
            ArrayUtils::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 = ArrayUtils::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
            ArrayUtils::normalizeValue($this->values),
138 3
            ArrayUtils::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
            ArrayUtils::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 = ArrayUtils::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