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>|null $attribA |
44
|
|
|
* @param array<string,Attribute>|null $attribB |
45
|
|
|
* |
46
|
|
|
* @return array<string,Attribute> |
47
|
|
|
*/ |
48
|
|
|
public static function merge(?array $attribA, ?array $attribB): array |
49
|
|
|
{ |
50
|
|
|
$attribA ??= []; |
51
|
|
|
$attribB ??= []; |
52
|
|
|
|
53
|
|
|
if (count($attribB) === 0) { |
54
|
|
|
return $attribA; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
foreach ($attribB as $key => $attrib2) { |
58
|
|
|
assert($attrib2 instanceof Attribute, Debug::prettyPrint('not an Attribute: "%s"', $attrib2)); |
59
|
|
|
assert($key === $attrib2->getKey(), sprintf('key: %s <=> %s', $key, $attrib2->getKey())); |
60
|
|
|
|
61
|
|
|
$key = $attrib2->getKey(); |
62
|
|
|
if (!array_key_exists($key, $attribA)) { |
63
|
|
|
$attribA[$key] = clone $attrib2; |
64
|
|
|
continue; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if (null !== $attrib2->getValues()) { |
68
|
|
|
$attribA[$key] = $attribA[$key]->append(...$attrib2->getValues()); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $attribA; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param array<string,Attribute>|null $attribA |
77
|
|
|
* @param Attribute ...$attribB |
78
|
|
|
* |
79
|
|
|
* @return array<string,Attribute> |
80
|
|
|
*/ |
81
|
|
|
public static function mergeItem(?array $attribA, Attribute ...$attribB): array |
82
|
|
|
{ |
83
|
|
|
$attribC = []; |
84
|
|
|
foreach ($attribB as $attribute) { |
85
|
|
|
$attribC[$attribute->getKey()] = $attribute; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return static::merge($attribA, $attribC); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param array<string,Attribute>|null $attribA |
93
|
|
|
* @param array<string,Attribute>|null $attribB |
94
|
|
|
* |
95
|
|
|
* @return array<string,Attribute> |
96
|
|
|
*/ |
97
|
|
|
public static function replace(?array $attribA, ?array $attribB): array |
98
|
|
|
{ |
99
|
|
|
$attribA ??= []; |
100
|
|
|
$attribB ??= []; |
101
|
|
|
|
102
|
|
|
if (count($attribB) === 0) { |
103
|
|
|
return $attribA; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
foreach ($attribB as $key => $attrib2) { |
107
|
|
|
assert($attrib2 instanceof Attribute, Debug::prettyPrint('not an Attribute: "%s"', $attrib2)); |
108
|
|
|
assert($key === $attrib2->getKey(), sprintf('key: %s <=> %s', $key, $attrib2->getKey())); |
109
|
|
|
|
110
|
|
|
$key = $attrib2->getKey(); |
111
|
|
|
$attribA[$key] = clone $attrib2; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $attribA; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param array<string,Attribute>|null $attribA |
119
|
|
|
* @param Attribute ...$attribB |
120
|
|
|
* |
121
|
|
|
* @return array<string,Attribute> |
122
|
|
|
*/ |
123
|
|
|
public static function replaceItem(?array $attribA, Attribute ...$attribB): array |
124
|
|
|
{ |
125
|
|
|
$attribC = []; |
126
|
|
|
foreach ($attribB as $attribute) { |
127
|
|
|
$attribC[$attribute->getKey()] = $attribute; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return static::replace($attribA, $attribC); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param array<string,Attribute>|null $attributes |
135
|
|
|
* @param array<string,Attribute>|null $attributes2 |
136
|
|
|
* |
137
|
|
|
* @return bool |
138
|
|
|
*/ |
139
|
|
|
public static function isEqual(?array $attributes, ?array $attributes2): bool |
140
|
|
|
{ |
141
|
|
|
$attributes ??= []; |
142
|
|
|
$attributes2 ??= []; |
143
|
|
|
|
144
|
|
|
if (count($attributes) != count($attributes2)) { |
145
|
|
|
return false; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
foreach ($attributes as $key => $attribute) { |
149
|
|
|
if (!array_key_exists($key, $attributes2)) { |
150
|
|
|
return false; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
if (!$attribute->isEqual($attributes2[$key])) { |
154
|
|
|
return false; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return true; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param array<string,Attribute> $attributes |
163
|
|
|
* |
164
|
|
|
* @return string |
165
|
|
|
*/ |
166
|
|
|
public static function toString(array $attributes): string |
167
|
|
|
{ |
168
|
|
|
$attr = []; |
169
|
|
|
foreach ($attributes as $attribute) { |
170
|
|
|
$attr[] = " " . $attribute; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return join('', $attr); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|