1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace drupol\htmltag; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class AbstractBaseHtmlTagObject |
7
|
|
|
*/ |
8
|
|
|
abstract class AbstractBaseHtmlTagObject |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Escape values are converted to string. |
12
|
|
|
* |
13
|
|
|
* @param mixed[] $values |
14
|
|
|
* The values to convert. |
15
|
|
|
* |
16
|
|
|
* @return string[]|null[] |
17
|
|
|
* The values. |
18
|
|
|
*/ |
19
|
37 |
|
protected function ensureStrings(array $values) |
20
|
|
|
{ |
21
|
37 |
|
return \array_values( |
22
|
37 |
|
\array_filter( |
23
|
37 |
|
\array_map( |
24
|
37 |
|
[$this, 'ensureString'], |
25
|
37 |
|
$values |
26
|
|
|
), |
27
|
37 |
|
'\is_string' |
28
|
|
|
) |
29
|
|
|
); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Transform a multidimensional array into a flat array. |
34
|
|
|
* |
35
|
|
|
* We could use a iterator_to_array() with a custom RecursiveArrayIterator. |
36
|
|
|
* But it seems to be even slower. |
37
|
|
|
* |
38
|
|
|
* @see http://php.net/manual/en/class.recursivearrayiterator.php#106519 |
39
|
|
|
* |
40
|
|
|
* @param mixed[] $array |
41
|
|
|
* The input array. |
42
|
|
|
* |
43
|
|
|
* @return mixed[] |
44
|
|
|
* The array with only one dimension. |
45
|
|
|
*/ |
46
|
48 |
|
protected function ensureFlatArray(array $array) |
47
|
|
|
{ |
48
|
48 |
|
$flat = array(); |
49
|
|
|
|
50
|
48 |
|
while ($array) { |
|
|
|
|
51
|
44 |
|
$value = \array_shift($array); |
52
|
|
|
|
53
|
44 |
|
if (\is_array($value)) { |
54
|
36 |
|
$array = \array_merge($value, $array); |
55
|
36 |
|
continue; |
56
|
|
|
} |
57
|
|
|
|
58
|
44 |
|
$flat[] = $value; |
59
|
|
|
} |
60
|
|
|
|
61
|
48 |
|
return $flat; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Make sure the value is an array. |
66
|
|
|
* |
67
|
|
|
* @param mixed $data |
68
|
|
|
* The input value. |
69
|
|
|
* |
70
|
|
|
* @return string|null |
71
|
|
|
* The input value in an array. |
72
|
|
|
*/ |
73
|
41 |
|
protected function ensureString($data) |
74
|
|
|
{ |
75
|
41 |
|
$return = null; |
76
|
|
|
|
77
|
41 |
|
switch (\gettype($data)) { |
78
|
41 |
|
case 'string': |
79
|
40 |
|
$return = $data; |
80
|
40 |
|
break; |
81
|
23 |
|
case 'integer': |
82
|
22 |
|
case 'double': |
83
|
3 |
|
$return = (string) $data; |
84
|
3 |
|
break; |
85
|
22 |
|
case 'object': |
86
|
7 |
|
if (\method_exists($data, '__toString')) { |
87
|
7 |
|
$return = $data->__toString(); |
88
|
|
|
} |
89
|
7 |
|
break; |
90
|
18 |
|
case 'boolean': |
91
|
18 |
|
case 'array': |
92
|
|
|
default: |
93
|
18 |
|
$return = null; |
94
|
18 |
|
break; |
95
|
|
|
} |
96
|
|
|
|
97
|
41 |
|
return $return; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Preprocess values before they are returned to user. |
102
|
|
|
* |
103
|
|
|
* @param array $values |
104
|
|
|
* The raw values. |
105
|
|
|
* @param string $name |
106
|
|
|
* The name of the object. |
107
|
|
|
* |
108
|
|
|
* @return array|\drupol\htmltag\Attribute\AttributeInterface[] |
109
|
|
|
* The values. |
110
|
|
|
*/ |
111
|
47 |
|
protected function preprocess(array $values, $name = null) |
|
|
|
|
112
|
|
|
{ |
113
|
47 |
|
return $values; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Sanitize a value. |
118
|
|
|
* |
119
|
|
|
* @param string|mixed $value |
120
|
|
|
* The value to sanitize |
121
|
|
|
* |
122
|
|
|
* @return string|mixed |
123
|
|
|
* The value sanitized. |
124
|
|
|
*/ |
125
|
|
|
abstract protected function escape($value); |
126
|
|
|
} |
127
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.