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