1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AbterPhp\Framework\Html; |
6
|
|
|
|
7
|
|
|
class Attribute |
8
|
|
|
{ |
9
|
|
|
protected string $key; |
10
|
|
|
|
11
|
|
|
/** @var array<string,string>|null */ |
12
|
|
|
protected ?array $values = null; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Attribute constructor. |
16
|
|
|
* |
17
|
|
|
* @param string $key |
18
|
|
|
* @param string|float|int|bool ...$values |
19
|
|
|
*/ |
20
|
|
|
public function __construct(string $key, ...$values) |
21
|
|
|
{ |
22
|
|
|
$this->key = $key; |
23
|
|
|
$this->append(...$values); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param string|float|int|bool ...$values |
28
|
|
|
* |
29
|
|
|
* @return $this |
30
|
|
|
*/ |
31
|
|
|
public function set(...$values): self |
32
|
|
|
{ |
33
|
|
|
$this->values = []; |
34
|
|
|
|
35
|
|
|
return $this->unsafeSet(...$values); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string|float|int|bool ...$values |
40
|
|
|
* |
41
|
|
|
* @return $this |
42
|
|
|
*/ |
43
|
|
|
public function append(...$values): self |
44
|
|
|
{ |
45
|
|
|
if (!$values) { |
46
|
|
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if ($this->values === null) { |
50
|
|
|
$this->values = []; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $this->unsafeSet(...$values); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string|float|int|bool ...$values |
58
|
|
|
* |
59
|
|
|
* @return $this |
60
|
|
|
*/ |
61
|
|
|
private function unsafeSet(...$values): self |
62
|
|
|
{ |
63
|
|
|
foreach ($values as $part) { |
64
|
|
|
if (empty($part)) { |
65
|
|
|
continue; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$part = (string)$part; |
69
|
|
|
|
70
|
|
|
$this->values[$part] = $part; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return bool |
78
|
|
|
*/ |
79
|
|
|
public function isNull(): bool |
80
|
|
|
{ |
81
|
|
|
return is_null($this->values); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string ...$value |
86
|
|
|
* |
87
|
|
|
* @return int |
88
|
|
|
*/ |
89
|
|
|
public function remove(string ...$value): int |
90
|
|
|
{ |
91
|
|
|
$count = 0; |
92
|
|
|
foreach ($value as $key) { |
93
|
|
|
if (!array_key_exists($key, $this->values)) { |
94
|
|
|
continue; |
95
|
|
|
} |
96
|
|
|
unset($this->values[$key]); |
97
|
|
|
$count++; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $count; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return $this |
105
|
|
|
*/ |
106
|
|
|
public function reset(): self |
107
|
|
|
{ |
108
|
|
|
$this->values = null; |
109
|
|
|
|
110
|
|
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
|
|
public function getValue(): string |
117
|
|
|
{ |
118
|
|
|
if (null === $this->values) { |
119
|
|
|
return ''; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return implode(' ', $this->values); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return string[]|null |
127
|
|
|
*/ |
128
|
|
|
public function getValues(): ?array |
129
|
|
|
{ |
130
|
|
|
if (null === $this->values) { |
131
|
|
|
return null; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return array_values($this->values); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
|
|
public function getKey(): string |
141
|
|
|
{ |
142
|
|
|
return $this->key; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param Attribute $attribute |
147
|
|
|
* |
148
|
|
|
* @return bool |
149
|
|
|
*/ |
150
|
|
|
public function isEqual(Attribute $attribute): bool |
151
|
|
|
{ |
152
|
|
|
if ($this->key !== $attribute->getKey()) { |
153
|
|
|
return false; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
$v1 = $this->getValues(); |
157
|
|
|
$v2 = $attribute->getValues(); |
158
|
|
|
|
159
|
|
|
if ($v1 === null || $v2 === null) { |
160
|
|
|
return $v1 === $v2; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return join('$$$', $v1) == join('$$$', $v2); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @return string |
168
|
|
|
*/ |
169
|
|
|
public function __toString(): string |
170
|
|
|
{ |
171
|
|
|
if ($this->values === null) { |
172
|
|
|
return $this->key; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
return sprintf('%s="%s"', $this->key, implode(" ", $this->values)); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|