Completed
Push — master ( 97bc30...8f50f7 )
by Pol
09:42 queued 06:23
created

Attribute::contains()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 12
cts 12
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 28
    public function __construct($name, ...$value)
35
    {
36 28
        $this->name = trim($name);
37 28
        $this->set($value);
38 28
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 28
    public function set(...$value)
44
    {
45 28
        $this->values = $value;
46
47 28
        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 16
    public function getValueAsString()
62
    {
63 16
        return implode(
64 16
            ' ',
65 16
            $this->getValueAsArray()
66
        );
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 20
    public function getValueAsArray()
73
    {
74 20
        return array_unique(
75 20
            $this->normalizeValue($this->values)
76
        );
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82 19
    public function render()
83
    {
84 19
        $output = (string) $this->name;
85
86 19
        if (!$this->isBoolean()) {
87 16
            $output .= '="' . trim($this->getValueAsString()) . '"';
88
        }
89
90 19
        return $output;
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96 13
    public function __toString()
97
    {
98 13
        return $this->render();
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 19
    public function isBoolean()
105
    {
106 19
        if ([] === $this->getValueAsArray()) {
107 12
            $this->values = null;
108
        }
109
110 19
        return null === $this->values;
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116 21
    public function append(...$value)
117
    {
118 21
        $this->values[] = $value;
119
120 21
        return $this;
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126 4
    public function remove(...$value)
127
    {
128 4
        $this->values = array_diff(
129 4
            $this->normalizeValue($this->values),
130 4
            $this->normalizeValue($value)
131
        );
132
133 4
        return $this;
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139 2
    public function replace($original, ...$replacement)
140
    {
141 2
        $count_start = count($this->normalizeValue($this->values));
142 2
        $this->remove($original);
143 2
        $count_end = count($this->normalizeValue($this->values));
144
145 2
        if ($count_end !== $count_start) {
146 2
            $this->append($replacement);
147
        }
148
149 2
        return $this;
150
    }
151
152
    /**
153
     * {@inheritdoc}
154
     */
155 4
    public function contains(...$substring)
156
    {
157 4
        $values = $this->normalizeValue($this->values);
158
159 4
        $array = array_map(
160
            function ($substring_item) use ($values) {
161 4
                return in_array($substring_item, $values, true);
162 4
            },
163 4
            $this->normalizeValue($substring)
164
        );
165
166 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...
167 4
            array_reduce(
168 4
                $array,
169
                function ($left, $right) {
170 4
                    return $left && $right;
171 4
                },
172 4
                true
173
            );
174
    }
175
176
    /**
177
     * {@inheritdoc}
178
     */
179 2
    public function setBoolean($boolean = true)
180
    {
181 2
        if (true === $boolean) {
182 2
            $this->values = null;
183
        }
184
185 2
        return $this;
186
    }
187
188
    /**
189
     * {@inheritdoc}
190
     */
191 1
    public function delete()
192
    {
193 1
        $this->name = null;
194 1
        $this->values = null;
195
196 1
        return $this;
197
    }
198
}
199