|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace drupol\htmltag; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class Attribute. |
|
7
|
|
|
*/ |
|
8
|
|
|
class Attribute extends AbstractBaseHtmlTagObject 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|string[]|mixed[] ...$value |
|
30
|
|
|
* The attribute value. |
|
31
|
|
|
*/ |
|
32
|
28 |
|
public function __construct($name, ...$value) |
|
33
|
|
|
{ |
|
34
|
28 |
|
$this->name = trim($name); |
|
35
|
28 |
|
$this->set($value); |
|
36
|
28 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* {@inheritdoc} |
|
40
|
|
|
*/ |
|
41
|
28 |
|
public function set(...$value) |
|
42
|
|
|
{ |
|
43
|
28 |
|
$this->values = $value; |
|
44
|
|
|
|
|
45
|
28 |
|
return $this; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* {@inheritdoc} |
|
50
|
|
|
*/ |
|
51
|
15 |
|
public function getName() |
|
52
|
|
|
{ |
|
53
|
15 |
|
return $this->name; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* {@inheritdoc} |
|
58
|
|
|
*/ |
|
59
|
16 |
|
public function getValueAsString() |
|
60
|
|
|
{ |
|
61
|
16 |
|
return implode( |
|
62
|
16 |
|
' ', |
|
63
|
16 |
|
$this->getValueAsArray() |
|
64
|
|
|
); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* {@inheritdoc} |
|
69
|
|
|
*/ |
|
70
|
20 |
|
public function getValueAsArray() |
|
71
|
|
|
{ |
|
72
|
20 |
|
return array_unique( |
|
73
|
20 |
|
$this->normalizeValue($this->values) |
|
74
|
|
|
); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* {@inheritdoc} |
|
79
|
|
|
*/ |
|
80
|
19 |
|
public function render() |
|
81
|
|
|
{ |
|
82
|
19 |
|
$output = $this->name; |
|
83
|
|
|
|
|
84
|
19 |
|
if (!$this->isBoolean()) { |
|
85
|
16 |
|
$output .= '="' . trim($this->getValueAsString()) . '"'; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
19 |
|
return $output; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* {@inheritdoc} |
|
93
|
|
|
*/ |
|
94
|
13 |
|
public function __toString() |
|
95
|
|
|
{ |
|
96
|
13 |
|
return $this->render(); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* {@inheritdoc} |
|
101
|
|
|
*/ |
|
102
|
19 |
|
public function isBoolean() |
|
103
|
|
|
{ |
|
104
|
19 |
|
if ([] === $this->getValueAsArray()) { |
|
105
|
12 |
|
$this->values = null; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
19 |
|
return null === $this->values; |
|
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
|
4 |
|
public function remove(...$value) |
|
125
|
|
|
{ |
|
126
|
4 |
|
$this->values = array_diff( |
|
127
|
4 |
|
$this->normalizeValue($this->values), |
|
128
|
4 |
|
$this->normalizeValue($value) |
|
129
|
|
|
); |
|
130
|
|
|
|
|
131
|
4 |
|
return $this; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* {@inheritdoc} |
|
136
|
|
|
*/ |
|
137
|
2 |
|
public function replace($original, ...$replacement) |
|
138
|
|
|
{ |
|
139
|
2 |
|
$count_start = count($this->normalizeValue($this->values)); |
|
140
|
2 |
|
$this->remove($original); |
|
141
|
2 |
|
$count_end = count($this->normalizeValue($this->values)); |
|
142
|
|
|
|
|
143
|
2 |
|
if ($count_end !== $count_start) { |
|
144
|
2 |
|
$this->append($replacement); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
2 |
|
return $this; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* {@inheritdoc} |
|
152
|
|
|
*/ |
|
153
|
4 |
|
public function contains(...$substring) |
|
154
|
|
|
{ |
|
155
|
4 |
|
$values = $this->normalizeValue($this->values); |
|
156
|
|
|
|
|
157
|
4 |
|
$array = array_map( |
|
158
|
|
|
function ($substring_item) use ($values) { |
|
159
|
4 |
|
return in_array($substring_item, $values, true); |
|
160
|
4 |
|
}, |
|
161
|
4 |
|
$this->normalizeValue($substring) |
|
162
|
|
|
); |
|
163
|
|
|
|
|
164
|
4 |
|
return $array && |
|
|
|
|
|
|
165
|
4 |
|
array_reduce( |
|
166
|
4 |
|
$array, |
|
167
|
|
|
function ($left, $right) { |
|
168
|
4 |
|
return $left && $right; |
|
169
|
4 |
|
}, |
|
170
|
4 |
|
true |
|
171
|
|
|
); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* {@inheritdoc} |
|
176
|
|
|
*/ |
|
177
|
2 |
|
public function setBoolean($boolean = true) |
|
178
|
|
|
{ |
|
179
|
2 |
|
if (true === $boolean) { |
|
180
|
2 |
|
$this->values = null; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
2 |
|
return $this; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* {@inheritdoc} |
|
188
|
|
|
*/ |
|
189
|
1 |
|
public function delete() |
|
190
|
|
|
{ |
|
191
|
1 |
|
$this->name = ''; |
|
192
|
1 |
|
$this->values = null; |
|
193
|
|
|
|
|
194
|
1 |
|
return $this; |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
|
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.