Completed
Push — master ( cf5a75...420187 )
by Pol
01:43
created

Attribute::getValueAsString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace drupol\htmltag\Attribute;
4
5
use drupol\htmltag\AbstractBaseHtmlTagObject;
6
7
/**
8
 * Class Attribute.
9
 */
10
class Attribute extends AbstractBaseHtmlTagObject implements AttributeInterface
11
{
12
    /**
13
     * Store the attribute name.
14
     *
15
     * @var string|null
16
     */
17
    private $name;
18
19
    /**
20
     * Store the attribute value.
21
     *
22
     * @var array|null
23
     */
24
    private $values;
25
26
    /**
27
     * Attribute constructor.
28
     *
29
     * @param string $name
30
     *   The attribute name.
31
     * @param string|string[]|mixed[] ...$value
32
     *   The attribute value.
33
     */
34 29
    public function __construct($name, ...$value)
35
    {
36 29
        $this->name = trim($name);
37 29
        $this->set($value);
38 29
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 29
    public function set(...$value)
44
    {
45 29
        $this->values = $value;
46
47 29
        return $this;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 21
    public function getName()
54
    {
55 21
        return $this->name;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 21
    public function getValueAsArray()
62
    {
63 21
        return array_map(
64
            function ($item) {
65 18
                return addcslashes($item, '\"');
66 21
            },
67 21
            $this->preprocess(
68 21
                $this->normalizeValue($this->values),
69 21
                $this->getName()
70
            )
71
        );
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 20
    public function render()
78
    {
79 20
        $output = (string) $this->name;
80
81 20
        if (!$this->isBoolean()) {
82 17
            $output .= '="' . implode(' ', $this->getValueAsArray()) . '"';
83
        }
84
85 20
        return $output;
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91 13
    public function __toString()
92
    {
93 13
        return $this->render();
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99 20
    public function isBoolean()
100
    {
101 20
        if ([] === $this->getValueAsArray()) {
102 13
            $this->values = null;
103
        }
104
105 20
        return null === $this->values;
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111 22
    public function append(...$value)
112
    {
113 22
        $this->values[] = $value;
114
115 22
        return $this;
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121 4
    public function remove(...$value)
122
    {
123 4
        $this->values = array_diff(
124 4
            $this->normalizeValue($this->values),
125 4
            $this->normalizeValue($value)
126
        );
127
128 4
        return $this;
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134 2
    public function replace($original, ...$replacement)
135
    {
136 2
        $count_start = count($this->normalizeValue($this->values));
137 2
        $this->remove($original);
138 2
        $count_end = count($this->normalizeValue($this->values));
139
140 2
        if ($count_end !== $count_start) {
141 2
            $this->append($replacement);
142
        }
143
144 2
        return $this;
145
    }
146
147
    /**
148
     * {@inheritdoc}
149
     */
150 4
    public function contains(...$substring)
151
    {
152 4
        $values = $this->normalizeValue($this->values);
153
154 4
        $array = array_map(
155
            function ($substring_item) use ($values) {
156 4
                return in_array($substring_item, $values, true);
157 4
            },
158 4
            $this->normalizeValue($substring)
159
        );
160
161 4
        return $array &&
0 ignored issues
show
Bug Best Practice introduced by
The expression $array of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
162 4
            array_reduce(
163 4
                $array,
164
                function ($left, $right) {
165 4
                    return $left && $right;
166 4
                },
167 4
                true
168
            );
169
    }
170
171
    /**
172
     * {@inheritdoc}
173
     */
174 2
    public function setBoolean($boolean = true)
175
    {
176 2
        if (true === $boolean) {
177 2
            $this->values = null;
178
        }
179
180 2
        return $this;
181
    }
182
183
    /**
184
     * {@inheritdoc}
185
     */
186 1
    public function delete()
187
    {
188 1
        $this->name = null;
189 1
        $this->values = null;
190
191 1
        return $this;
192
    }
193
194
    /**
195
     * {@inheritdoc}
196
     */
197 1
    public function alter(callable ...$callable)
198
    {
199 1
        $name = $this->getName();
200
201 1
        foreach ($callable as $closure) {
202 1
            $values = $this->normalizeValue($this->values);
203 1
            $this->values = call_user_func_array($closure, [&$values, $name]);
204
        }
205
206 1
        return $this;
207
    }
208
209
    /**
210
     * {@inheritdoc}
211
     */
212 21
    protected function preprocess(array $values, $name = null)
213
    {
214 21
        return $values;
215
    }
216
}
217