1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace drupol\htmltag; |
6
|
|
|
|
7
|
|
|
use function gettype; |
8
|
|
|
use function is_array; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class AbstractBaseHtmlTagObject. |
12
|
|
|
* |
13
|
|
|
* This class is the base class of other HTMLTag objects. |
14
|
|
|
* It contains simple methods that are needed everywhere. |
15
|
|
|
* We could have used a Trait instead, but I don't like working with traits. |
16
|
|
|
*/ |
17
|
|
|
abstract class AbstractBaseHtmlTagObject |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Transform a multidimensional array into a flat array. |
21
|
|
|
* |
22
|
|
|
* This method will flatten an array containing arrays. |
23
|
|
|
* Keys will be lost during the flattening. |
24
|
|
|
* We could use a iterator_to_array() with a custom RecursiveArrayIterator. |
25
|
|
|
* But it seems to be even slower. |
26
|
|
|
* |
27
|
|
|
* @see http://php.net/manual/en/class.recursivearrayiterator.php#106519 |
28
|
|
|
* |
29
|
|
|
* @param mixed[] $data |
30
|
48 |
|
* The input array |
31
|
|
|
* |
32
|
48 |
|
* @return mixed[] |
33
|
|
|
* A simple array |
34
|
48 |
|
*/ |
35
|
44 |
|
protected function ensureFlatArray(array $data): array |
36
|
|
|
{ |
37
|
44 |
|
$flat = []; |
38
|
35 |
|
|
39
|
|
|
while (!empty($data)) { |
40
|
35 |
|
$value = array_shift($data); |
41
|
|
|
|
42
|
|
|
if (is_array($value)) { |
43
|
44 |
|
$data = array_merge($value, $data); |
44
|
|
|
|
45
|
|
|
continue; |
46
|
48 |
|
} |
47
|
|
|
|
48
|
|
|
$flat[] = $value; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $flat; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Convert a value into a string when it's possible. |
56
|
|
|
* |
57
|
|
|
* Ensure that the value gets converted into a string when it is possible. |
58
|
|
|
* When it's not possible, the "null" value is returned instead. |
59
|
|
|
* |
60
|
|
|
* @param mixed $data |
61
|
41 |
|
* The input value |
62
|
|
|
* |
63
|
41 |
|
* @return string|null |
64
|
|
|
* The value converted as a string or null |
65
|
41 |
|
*/ |
66
|
41 |
|
protected function ensureString($data): ?string |
67
|
40 |
|
{ |
68
|
|
|
$return = null; |
69
|
40 |
|
|
70
|
|
|
switch (gettype($data)) { |
71
|
21 |
|
case 'string': |
72
|
20 |
|
$return = $data; |
73
|
3 |
|
|
74
|
|
|
break; |
75
|
3 |
|
case 'integer': |
76
|
|
|
case 'double': |
77
|
20 |
|
$return = (string) $data; |
78
|
5 |
|
|
79
|
5 |
|
break; |
80
|
|
|
case 'object': |
81
|
|
|
if (method_exists($data, '__toString')) { |
82
|
5 |
|
$return = (string) $data; |
83
|
|
|
} |
84
|
17 |
|
|
85
|
17 |
|
break; |
86
|
|
|
case 'boolean': |
87
|
17 |
|
case 'array': |
88
|
|
|
default: |
89
|
17 |
|
$return = null; |
90
|
|
|
|
91
|
|
|
break; |
92
|
41 |
|
} |
93
|
|
|
|
94
|
|
|
return $return; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Make sure that the value parameters is converted into an |
99
|
|
|
* array of strings. |
100
|
|
|
* |
101
|
|
|
* Only simple arrays (no nested arrays) are allowed here. |
102
|
|
|
* Values that cannot be converted to strings will be removed from the |
103
|
|
|
* resulting array. |
104
|
|
|
* |
105
|
|
|
* @param mixed[] $values |
106
|
|
|
* The input values |
107
|
|
|
* |
108
|
|
|
* @return null[]|string[] |
109
|
37 |
|
* The output values, should be an array of strings |
110
|
|
|
*/ |
111
|
37 |
|
protected function ensureStrings(array $values): array |
112
|
37 |
|
{ |
113
|
37 |
|
return array_values( |
114
|
37 |
|
array_filter( |
115
|
37 |
|
array_map( |
116
|
|
|
[$this, 'ensureString'], |
117
|
37 |
|
$values |
118
|
|
|
), |
119
|
|
|
'\is_string' |
120
|
|
|
) |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|