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