1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace drupol\htmltag; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class AbstractBaseHtmlTagObject |
7
|
|
|
*/ |
8
|
|
|
abstract class AbstractBaseHtmlTagObject |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Escape values. |
12
|
|
|
* |
13
|
|
|
* @param array $values |
14
|
|
|
* The values to escape. |
15
|
|
|
* |
16
|
|
|
* @return array |
17
|
|
|
* The values escaped. |
18
|
|
|
*/ |
19
|
42 |
|
protected function escapeValues(array $values) |
20
|
|
|
{ |
21
|
42 |
|
return \array_map( |
22
|
42 |
|
[$this, 'escape'], |
23
|
42 |
|
$values |
24
|
|
|
); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Normalize the values. |
29
|
|
|
* |
30
|
|
|
* @param mixed $values |
31
|
|
|
* The value to normalize. |
32
|
|
|
* |
33
|
|
|
* @return array |
34
|
|
|
* The value normalized. |
35
|
|
|
*/ |
36
|
42 |
|
protected function normalizeValues($values) |
37
|
|
|
{ |
38
|
42 |
|
return \array_filter( |
39
|
42 |
|
$this->escapeValues($values), |
40
|
42 |
|
'\is_string' |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Transform a multidimensional array into a flat array. |
46
|
|
|
* |
47
|
|
|
* We could use a iterator_to_array() with a custom RecursiveArrayIterator. |
48
|
|
|
* But it seems to be even slower. |
49
|
|
|
* |
50
|
|
|
* @see http://php.net/manual/en/class.recursivearrayiterator.php#106519 |
51
|
|
|
* |
52
|
|
|
* @param mixed[] $array |
53
|
|
|
* The input array. |
54
|
|
|
* |
55
|
|
|
* @return mixed[] |
56
|
|
|
* The array with only one dimension. |
57
|
|
|
*/ |
58
|
42 |
|
protected function ensureFlatArray(array $array) |
59
|
|
|
{ |
60
|
42 |
|
return \array_reduce( |
61
|
42 |
|
$array, |
62
|
|
|
function ($carry, $item) { |
63
|
38 |
|
if (\is_array($item)) { |
64
|
33 |
|
return \array_merge( |
65
|
33 |
|
$carry, |
66
|
33 |
|
$this->ensureFlatArray($item) |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
37 |
|
$carry[] = $item; |
71
|
|
|
|
72
|
37 |
|
return $carry; |
73
|
42 |
|
}, |
74
|
42 |
|
[] |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Make sure the value is an array. |
80
|
|
|
* |
81
|
|
|
* @param mixed $data |
82
|
|
|
* The input value. |
83
|
|
|
* |
84
|
|
|
* @return string|null |
85
|
|
|
* The input value in an array. |
86
|
|
|
*/ |
87
|
44 |
|
protected function ensureString($data) |
88
|
|
|
{ |
89
|
44 |
|
$return = null; |
90
|
|
|
|
91
|
44 |
|
switch (\gettype($data)) { |
92
|
44 |
|
case 'string': |
93
|
44 |
|
$return = $data; |
94
|
44 |
|
break; |
95
|
4 |
|
case 'integer': |
96
|
3 |
|
case 'double': |
97
|
2 |
|
$return = (string) $data; |
98
|
2 |
|
break; |
99
|
3 |
|
case 'object': |
100
|
1 |
|
if (\method_exists($data, '__toString')) { |
101
|
1 |
|
$return = $data->__toString(); |
102
|
|
|
} |
103
|
1 |
|
break; |
104
|
3 |
|
case 'boolean': |
105
|
3 |
|
case 'array': |
106
|
|
|
default: |
107
|
3 |
|
$return = null; |
108
|
3 |
|
break; |
109
|
|
|
} |
110
|
|
|
|
111
|
44 |
|
return $return; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Preprocess values before they are returned to user. |
116
|
|
|
* |
117
|
|
|
* @param array $values |
118
|
|
|
* The raw values. |
119
|
|
|
* @param string $name |
120
|
|
|
* The name of the object. |
121
|
|
|
* |
122
|
|
|
* @return array|\drupol\htmltag\Attribute\AttributeInterface[] |
123
|
|
|
* The values. |
124
|
|
|
*/ |
125
|
41 |
|
public function preprocess(array $values, $name = null) |
|
|
|
|
126
|
|
|
{ |
127
|
41 |
|
return $values; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Sanitize a value. |
132
|
|
|
* |
133
|
|
|
* @param string|mixed $value |
134
|
|
|
* The value to sanitize |
135
|
|
|
* |
136
|
|
|
* @return string|mixed |
137
|
|
|
* The value sanitized. |
138
|
|
|
*/ |
139
|
|
|
abstract protected function escape($value); |
140
|
|
|
} |
141
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.