Completed
Push — master ( 420187...cff10f )
by Pol
14:06
created

Attribute::contains()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 9
cts 9
cp 1
rs 9.6
c 0
b 0
f 0
cc 3
nc 2
nop 1
crap 3
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 15
    public function getName()
54
    {
55 15
        return $this->name;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 17
    public function getValuesAsArray()
62
    {
63 17
        return array_map(
64 17
            function ($item) {
65 17
                return addcslashes($item, '\"');
66
            },
67
            $this->preprocess(
68
                $this->normalizeValue($this->values),
69
                $this->getName()
70
            )
71
        );
72 21
    }
73
74 21
    /**
75 21
     * {@inheritdoc}
76
     */
77
    public function render()
78
    {
79
        $output = (string) $this->name;
80
81
        if (!$this->isBoolean()) {
82 20
            $output .= '="' . implode(' ', $this->getValuesAsArray()) . '"';
83
        }
84 20
85
        return $output;
86 20
    }
87 17
88
    /**
89
     * {@inheritdoc}
90 20
     */
91
    public function __toString()
92
    {
93
        return $this->render();
94
    }
95
96 13
    /**
97
     * {@inheritdoc}
98 13
     */
99
    public function isBoolean()
100
    {
101
        if ([] === $this->getValuesAsArray()) {
102
            $this->values = null;
103
        }
104 20
105
        return null === $this->values;
106 20
    }
107 12
108
    /**
109
     * {@inheritdoc}
110 20
     */
111
    public function append(...$value)
112
    {
113
        $this->values[] = $value;
114
115
        return $this;
116 22
    }
117
118 22
    /**
119
     * {@inheritdoc}
120 22
     */
121
    public function remove(...$value)
122
    {
123
        $this->values = array_diff(
124
            $this->normalizeValue($this->values),
125
            $this->normalizeValue($value)
126 4
        );
127
128 4
        return $this;
129 4
    }
130 4
131
    /**
132
     * {@inheritdoc}
133 4
     */
134
    public function replace($original, ...$replacement)
135
    {
136
        $count_start = count($this->normalizeValue($this->values));
137
        $this->remove($original);
138
        $count_end = count($this->normalizeValue($this->values));
139 2
140
        if ($count_end !== $count_start) {
141 2
            $this->append($replacement);
142 2
        }
143 2
144
        return $this;
145 2
    }
146 2
147
    /**
148
     * {@inheritdoc}
149 2
     */
150
    public function contains(...$substring)
151
    {
152
        $values = $this->normalizeValue($this->values);
153
154
        $array = array_map(
155 4
            function ($substring_item) use ($values) {
156
                return in_array($substring_item, $values, true);
157 4
            },
158
            $this->normalizeValue($substring)
159 4
        );
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
                    return $left && $right;
166 4
                },
167 4
                true
168 4
            );
169
    }
170 4
171 4
    /**
172 4
     * {@inheritdoc}
173
     */
174
    public function setBoolean($boolean = true)
175
    {
176
        if (true === $boolean) {
177
            $this->values = null;
178
        }
179 2
180
        return $this;
181 2
    }
182 2
183
    /**
184
     * {@inheritdoc}
185 2
     */
186
    public function delete()
187
    {
188
        $this->name = null;
189
        $this->values = null;
190
191 1
        return $this;
192
    }
193 1
194 1
    /**
195
     * {@inheritdoc}
196 1
     */
197
    public function alter(callable ...$callable)
198
    {
199
        $name = $this->getName();
200
201
        foreach ($callable as $closure) {
202 1
            $values = $this->normalizeValue($this->values);
203
            $this->values = call_user_func_array($closure, [&$values, $name]);
204 1
        }
205
206 1
        return $this;
207
    }
208
209
    /**
210
     * {@inheritdoc}
211
     */
212
    protected function preprocess(array $values, $name = null)
213
    {
214
        return $values;
215
    }
216
}
217