1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_templates\HtmlElement; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Trait TAttributes |
8
|
|
|
* @package kalanis\kw_templates\Template |
9
|
|
|
* Trait for work with attributes |
10
|
|
|
* It's not necessary to have attributes directly in HtmlElement |
11
|
|
|
* @author Adam Dornak original |
12
|
|
|
* @author Petr Plsek refactored |
13
|
|
|
*/ |
14
|
|
|
trait TAttributes |
15
|
|
|
{ |
16
|
|
|
/** @var array<string, string> */ |
17
|
|
|
protected array $attributes = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Returns serialized attributes |
21
|
|
|
* Use $attributes param if is set |
22
|
|
|
* @param string|array<string, string|int|array<string>>|null $attributes |
23
|
|
|
* @return string |
24
|
|
|
*/ |
25
|
2 |
|
protected function renderAttributes($attributes = null): string |
26
|
|
|
{ |
27
|
2 |
|
$attributes = is_null($attributes) ? $this->attributes : $this->attributesParse($attributes); |
28
|
|
|
|
29
|
2 |
|
$return = ''; |
30
|
2 |
|
foreach ($attributes as $name => $value) { |
31
|
2 |
|
$return .= ' ' . $name . '="' . $value . '"'; |
32
|
|
|
} |
33
|
|
|
|
34
|
2 |
|
return $return; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Add array of attributes into current object attributes |
39
|
|
|
* @param string|array<string, string|int|array<string>>|null $attributes |
40
|
|
|
*/ |
41
|
8 |
|
public function addAttributes($attributes): void |
42
|
|
|
{ |
43
|
8 |
|
$this->attributes = array_merge($this->attributes, $this->attributesParse($attributes)); |
44
|
8 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Change attributes in variable to 2-dimensional array |
48
|
|
|
* Expects array, discard rest |
49
|
|
|
* @param string|array<string, string|int|array<string>>|null $attributes |
50
|
|
|
* @return array<string, string> |
51
|
|
|
*/ |
52
|
8 |
|
public function attributesParse($attributes): array |
53
|
|
|
{ |
54
|
8 |
|
$attributes = is_array($attributes) ? $attributes : $this->attributesParseString(strval($attributes)); |
55
|
8 |
|
return $this->attributesParseArray($attributes); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Change attributes in variable to 2-dimensional array |
60
|
|
|
* Expects array, discard rest |
61
|
|
|
* @param array<string|int, string|int|array<string>> $attributes |
62
|
|
|
* @return array<string, string> |
63
|
|
|
*/ |
64
|
8 |
|
public function attributesParseArray(array $attributes): array |
65
|
|
|
{ |
66
|
8 |
|
$array = []; |
67
|
8 |
|
foreach ($attributes as $key => $val) { |
68
|
8 |
|
if (is_string($key)) { |
69
|
8 |
|
$key = strtolower($key); |
70
|
8 |
|
if (is_string($val) || is_numeric($val)) { |
71
|
8 |
|
$val = strtolower(strval($val)); |
72
|
8 |
|
$array[$key] = $val; |
73
|
1 |
|
} else if (is_array($val)) { |
74
|
1 |
|
foreach ($val as &$_val) { |
75
|
1 |
|
$_val = strtolower(strval($_val)); |
76
|
|
|
} |
77
|
1 |
|
$val = implode(';', $val); |
78
|
1 |
|
$array[$key] = $val; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
8 |
|
return $array; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Change attributes to 2-dimensional array |
87
|
|
|
* Expects string like: width="100px" height='150px' style="color:red" |
88
|
|
|
* Discard rest |
89
|
|
|
* @param string $attributes |
90
|
|
|
* @return array<string|int, string|int> |
91
|
|
|
*/ |
92
|
3 |
|
public function attributesParseString(string $attributes): array |
93
|
|
|
{ |
94
|
3 |
|
$array = []; |
95
|
3 |
|
$string = trim($attributes); |
96
|
3 |
|
if (preg_match_all('/([a-z]+)\=("|\')?(.+?)(?(2)\2)(\s|$)/', $string, $matches)) { |
97
|
3 |
|
$limit = count($matches[1]); |
98
|
3 |
|
for ($i = 0; $i < $limit; $i++) { |
99
|
3 |
|
$array[$matches[1][$i]] = trim($matches[3][$i]); |
100
|
|
|
} |
101
|
|
|
} |
102
|
3 |
|
return $array; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Set attributes, leave nothing from previous ones |
107
|
|
|
* @param string|array<string, string|int|array<string>>|null $attributes |
108
|
|
|
*/ |
109
|
4 |
|
public function setAttributes($attributes): void |
110
|
|
|
{ |
111
|
4 |
|
$this->attributes = []; |
112
|
4 |
|
$this->addAttributes($attributes); |
113
|
4 |
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get all available attributes |
117
|
|
|
* @return array<string, string> |
118
|
|
|
*/ |
119
|
11 |
|
public function getAttributes(): array |
120
|
|
|
{ |
121
|
11 |
|
return $this->attributes; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get attribute value |
126
|
|
|
* @param string $name |
127
|
|
|
* @return string|null |
128
|
|
|
*/ |
129
|
10 |
|
public function getAttribute(string $name): ?string |
130
|
|
|
{ |
131
|
10 |
|
return isset($this->attributes[$name]) ? $this->attributes[$name] : null ; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Set attribute value |
136
|
|
|
* @param string $name |
137
|
|
|
* @param string $value |
138
|
|
|
*/ |
139
|
7 |
|
public function setAttribute(string $name, string $value): void |
140
|
|
|
{ |
141
|
7 |
|
$this->attributes[$name] = $value; |
142
|
7 |
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Remove attribute |
146
|
|
|
* @param string $name |
147
|
|
|
*/ |
148
|
2 |
|
public function removeAttribute(string $name): void |
149
|
|
|
{ |
150
|
2 |
|
if (isset($this->attributes[$name])) { |
151
|
2 |
|
unset($this->attributes[$name]); |
152
|
|
|
} |
153
|
2 |
|
} |
154
|
|
|
} |
155
|
|
|
|