1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AbterPhp\Framework\Html\Helper; |
6
|
|
|
|
7
|
|
|
use AbterPhp\Framework\Helper\Debug; |
8
|
|
|
use AbterPhp\Framework\Html\Attribute; |
9
|
|
|
|
10
|
|
|
class Attributes |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @param array<string,string|string[]>|null $values |
14
|
|
|
* |
15
|
|
|
* @return array |
16
|
|
|
*/ |
17
|
|
|
public static function fromArray(?array $values): array |
18
|
|
|
{ |
19
|
|
|
if ($values === null) { |
20
|
|
|
return []; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
$attributes = []; |
24
|
|
|
|
25
|
|
|
foreach ($values as $key => $value) { |
26
|
|
|
if (null === $value) { |
27
|
|
|
$values = []; |
28
|
|
|
} elseif (!is_array($value)) { |
29
|
|
|
$values = [$value]; |
30
|
|
|
} else { |
31
|
|
|
$values = $value; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
assert(Collection::allStrings($values), Debug::prettyPrint('not all strings: %s', $values)); |
35
|
|
|
|
36
|
|
|
$attributes[$key] = new Attribute($key, ...$values); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return $attributes; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param array<string,Attribute> $attributes |
44
|
|
|
* @param string $newKey |
45
|
|
|
* @param ...$newValues |
46
|
|
|
* |
47
|
|
|
* @return array |
48
|
|
|
*/ |
49
|
|
|
public static function addItem(array $attributes, string $newKey, ...$newValues): array |
50
|
|
|
{ |
51
|
|
|
$attributes[$newKey] = new Attribute($newKey, ...$newValues); |
52
|
|
|
|
53
|
|
|
return $attributes; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param array<string,Attribute>|null $attribA |
58
|
|
|
* @param array<string,Attribute>|null $attribB |
59
|
|
|
* |
60
|
|
|
* @return array<string,Attribute> |
61
|
|
|
*/ |
62
|
|
|
public static function merge(?array $attribA, ?array $attribB): array |
63
|
|
|
{ |
64
|
|
|
$attribA ??= []; |
65
|
|
|
$attribB ??= []; |
66
|
|
|
|
67
|
|
|
if (count($attribB) === 0) { |
68
|
|
|
return $attribA; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
foreach ($attribB as $key => $attrib2) { |
72
|
|
|
assert($attrib2 instanceof Attribute, Debug::prettyPrint('not an Attribute: "%s"', $attrib2)); |
73
|
|
|
assert($key === $attrib2->getKey(), sprintf('key: %s <=> %s', $key, $attrib2->getKey())); |
74
|
|
|
|
75
|
|
|
$key = $attrib2->getKey(); |
76
|
|
|
if (!array_key_exists($key, $attribA)) { |
77
|
|
|
$attribA[$key] = clone $attrib2; |
78
|
|
|
continue; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if (null !== $attrib2->getValues()) { |
82
|
|
|
$attribA[$key] = $attribA[$key]->append(...$attrib2->getValues()); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $attribA; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param array<string,Attribute>|null $attribA |
91
|
|
|
* @param Attribute ...$attribB |
92
|
|
|
* |
93
|
|
|
* @return array<string,Attribute> |
94
|
|
|
*/ |
95
|
|
|
public static function mergeItem(?array $attribA, Attribute ...$attribB): array |
96
|
|
|
{ |
97
|
|
|
$attribC = []; |
98
|
|
|
foreach ($attribB as $attribute) { |
99
|
|
|
$attribC[$attribute->getKey()] = $attribute; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return static::merge($attribA, $attribC); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param array<string,Attribute>|null $attribA |
107
|
|
|
* @param array<string,Attribute>|null $attribB |
108
|
|
|
* |
109
|
|
|
* @return array<string,Attribute> |
110
|
|
|
*/ |
111
|
|
|
public static function replace(?array $attribA, ?array $attribB): array |
112
|
|
|
{ |
113
|
|
|
$attribA ??= []; |
114
|
|
|
$attribB ??= []; |
115
|
|
|
|
116
|
|
|
if (count($attribB) === 0) { |
117
|
|
|
return $attribA; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
foreach ($attribB as $key => $attrib2) { |
121
|
|
|
assert($attrib2 instanceof Attribute, Debug::prettyPrint('not an Attribute: "%s"', $attrib2)); |
122
|
|
|
assert($key === $attrib2->getKey(), sprintf('key: %s <=> %s', $key, $attrib2->getKey())); |
123
|
|
|
|
124
|
|
|
$key = $attrib2->getKey(); |
125
|
|
|
$attribA[$key] = clone $attrib2; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $attribA; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param array<string,Attribute>|null $attribA |
133
|
|
|
* @param Attribute ...$attribB |
134
|
|
|
* |
135
|
|
|
* @return array<string,Attribute> |
136
|
|
|
*/ |
137
|
|
|
public static function replaceItem(?array $attribA, Attribute ...$attribB): array |
138
|
|
|
{ |
139
|
|
|
$attribC = []; |
140
|
|
|
foreach ($attribB as $attribute) { |
141
|
|
|
$attribC[$attribute->getKey()] = $attribute; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return static::replace($attribA, $attribC); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param array<string,Attribute>|null $attributes |
149
|
|
|
* @param array<string,Attribute>|null $attributes2 |
150
|
|
|
* |
151
|
|
|
* @return bool |
152
|
|
|
*/ |
153
|
|
|
public static function isEqual(?array $attributes, ?array $attributes2): bool |
154
|
|
|
{ |
155
|
|
|
$attributes ??= []; |
156
|
|
|
$attributes2 ??= []; |
157
|
|
|
|
158
|
|
|
if (count($attributes) != count($attributes2)) { |
159
|
|
|
return false; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
foreach ($attributes as $key => $attribute) { |
163
|
|
|
if (!array_key_exists($key, $attributes2)) { |
164
|
|
|
return false; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
if (!$attribute->isEqual($attributes2[$key])) { |
168
|
|
|
return false; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return true; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param array<string,Attribute> $attributes |
177
|
|
|
* |
178
|
|
|
* @return string |
179
|
|
|
*/ |
180
|
|
|
public static function toString(array $attributes): string |
181
|
|
|
{ |
182
|
|
|
$attr = []; |
183
|
|
|
foreach ($attributes as $attribute) { |
184
|
|
|
$attr[] = " " . $attribute; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
return join('', $attr); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|