1
|
|
|
<?php |
2
|
|
|
namespace Redaxscript\Head; |
3
|
|
|
|
4
|
|
|
use Redaxscript\Singleton; |
5
|
|
|
use function array_key_exists; |
6
|
|
|
use function array_map; |
7
|
|
|
use function array_splice; |
8
|
|
|
use function array_unshift; |
9
|
|
|
use function is_array; |
10
|
|
|
use function strlen; |
11
|
|
|
use function trim; |
12
|
|
|
use function ucfirst; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* abstract class to create a head class |
16
|
|
|
* |
17
|
|
|
* @since 3.0.0 |
18
|
|
|
* |
19
|
|
|
* @package Redaxscript |
20
|
|
|
* @category Head |
21
|
|
|
* @author Henry Ruhs |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
abstract class HeadAbstract extends Singleton implements HeadInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* collection namespace |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
|
32
|
|
|
protected static $_namespace = 'Redaxscript\Head'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* collection of the head |
36
|
|
|
* |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
|
40
|
|
|
protected static $_collectionArray = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* stringify the collection |
44
|
|
|
* |
45
|
|
|
* @since 3.0.0 |
46
|
|
|
* |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
25 |
|
|
50
|
|
|
public function __toString() : string |
51
|
25 |
|
{ |
52
|
25 |
|
$render = $this->render(); |
53
|
|
|
if ($render) |
54
|
16 |
|
{ |
55
|
|
|
return $render; |
56
|
9 |
|
} |
57
|
|
|
return '<!-- ' . self::$_namespace . ' -->'; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* init the class |
62
|
|
|
* |
63
|
|
|
* @param string $namespace collection sub namespace |
64
|
|
|
* |
65
|
|
|
* @since 3.0.0 |
66
|
|
|
* |
67
|
|
|
* @return self |
68
|
|
|
*/ |
69
|
26 |
|
|
70
|
|
|
public function init(string $namespace = null) : self |
71
|
26 |
|
{ |
72
|
26 |
|
self::$_namespace = static::class; |
73
|
|
|
if ($namespace) |
|
|
|
|
74
|
26 |
|
{ |
75
|
|
|
self::$_namespace .= '\\' . ucfirst($namespace); |
76
|
26 |
|
} |
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* append to the collection |
82
|
|
|
* |
83
|
|
|
* @since 3.0.0 |
84
|
|
|
* |
85
|
|
|
* @param string|array $attribute key or array of attributes |
86
|
|
|
* @param string $value value of the attribute |
87
|
|
|
* |
88
|
|
|
* @return self |
89
|
|
|
*/ |
90
|
8 |
|
|
91
|
|
|
public function append($attribute = null, string $value = null) : self |
92
|
8 |
|
{ |
93
|
8 |
|
$collectionArray = $this->_getCollectionArray(); |
94
|
|
|
if (is_array($attribute)) |
95
|
8 |
|
{ |
96
|
|
|
$collectionArray[] = array_map('trim', $attribute); |
97
|
1 |
|
} |
98
|
|
|
else if (strlen($attribute) && strlen($value)) |
99
|
1 |
|
{ |
100
|
|
|
$collectionArray[] = |
101
|
1 |
|
[ |
102
|
|
|
trim($attribute) => trim($value) |
103
|
|
|
]; |
104
|
8 |
|
} |
105
|
8 |
|
$this->_setCollectionArray($collectionArray); |
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* prepend to the collection |
111
|
|
|
* |
112
|
|
|
* @since 3.0.0 |
113
|
|
|
* |
114
|
|
|
* @param string|array $attribute key or array of attributes |
115
|
|
|
* @param string $value value of the attribute |
116
|
|
|
* |
117
|
|
|
* @return self |
118
|
|
|
*/ |
119
|
4 |
|
|
120
|
|
|
public function prepend($attribute = null, string $value = null) : self |
121
|
4 |
|
{ |
122
|
4 |
|
$collectionArray = $this->_getCollectionArray(); |
123
|
|
|
if (is_array($attribute)) |
124
|
4 |
|
{ |
125
|
|
|
array_unshift($collectionArray, array_map('trim', $attribute)); |
126
|
1 |
|
} |
127
|
|
|
else if (strlen($attribute) && strlen($value)) |
128
|
1 |
|
{ |
129
|
|
|
array_unshift($collectionArray, |
130
|
1 |
|
[ |
131
|
|
|
trim($attribute) => trim($value) |
132
|
|
|
]); |
133
|
4 |
|
} |
134
|
4 |
|
$this->_setCollectionArray($collectionArray); |
135
|
|
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* remove from to the collection |
140
|
|
|
* |
141
|
|
|
* @since 3.0.0 |
142
|
|
|
* |
143
|
|
|
* @param string $attribute name of attribute |
144
|
|
|
* @param string $value value of the attribute |
145
|
|
|
* |
146
|
|
|
* @return self |
147
|
|
|
*/ |
148
|
2 |
|
|
149
|
|
|
public function remove(string $attribute = null, string $value = null) : self |
150
|
2 |
|
{ |
151
|
2 |
|
$collectionArray = $this->_getCollectionArray(); |
152
|
|
|
if (is_array($collectionArray)) |
153
|
2 |
|
{ |
154
|
|
|
foreach ($collectionArray as $collectionKey => $collectionValue) |
155
|
2 |
|
{ |
156
|
|
|
if ($collectionValue[$attribute] === $value) |
157
|
2 |
|
{ |
158
|
|
|
array_splice($collectionArray, $collectionKey, 1); |
159
|
|
|
} |
160
|
2 |
|
} |
161
|
|
|
$this->_setCollectionArray($collectionArray); |
162
|
2 |
|
} |
163
|
|
|
return $this; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* clear the collection |
168
|
|
|
* |
169
|
|
|
* @since 3.0.0 |
170
|
|
|
* |
171
|
|
|
* @return self |
172
|
|
|
*/ |
173
|
25 |
|
|
174
|
|
|
public function clear() : self |
175
|
25 |
|
{ |
176
|
25 |
|
$this->_setCollectionArray(); |
177
|
|
|
return $this; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* get the collection array |
182
|
|
|
* |
183
|
|
|
* @since 3.0.0 |
184
|
|
|
* |
185
|
|
|
* @return array |
186
|
|
|
*/ |
187
|
25 |
|
|
188
|
|
|
protected function _getCollectionArray() : array |
189
|
25 |
|
{ |
190
|
|
|
return array_key_exists(self::$_namespace, self::$_collectionArray) ? self::$_collectionArray[self::$_namespace] : []; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* set the collection array |
195
|
|
|
* |
196
|
|
|
* @since 3.0.0 |
197
|
|
|
* |
198
|
|
|
* @param array $collectionArray |
199
|
|
|
*/ |
200
|
25 |
|
|
201
|
|
|
protected function _setCollectionArray(array $collectionArray = []) : void |
202
|
25 |
|
{ |
203
|
25 |
|
self::$_collectionArray[self::$_namespace] = $collectionArray; |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: