Completed
Push — tmp ( 8901a3 )
by Pol
13:02
created

AbstractBaseHtmlTagObject::ensureString()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.4444
c 0
b 0
f 0
cc 8
nc 8
nop 1
1
<?php
2
3
namespace drupol\htmltag;
4
5
/**
6
 * Class AbstractBaseHtmlTagObject
7
 */
8
abstract class AbstractBaseHtmlTagObject
9
{
10
    /**
11
     * Normalize the values.
12
     *
13
     * @param mixed $values
14
     *  The value to normalize.
15
     *
16
     * @return array
17
     *   The value normalized.
18
     */
19
    public function normalizeValue($values)
20
    {
21
        var_dump('BEFORE');
0 ignored issues
show
Security Debugging Code introduced by
var_dump('BEFORE'); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
22
        var_dump($values);
23
24
        $ttt = array_filter(
25
            array_map(
26
                function ($item) {
27
                    return is_object($item) ?
28
                        $this->ensureString($item):
29
                        $this->escape($this->ensureString($item));
30
                },
31
                $this->ensureFlatArray((array) $values))
32
        , '\is_string');
0 ignored issues
show
Coding Style introduced by
Space found before comma in function call
Loading history...
33
34
        var_dump('AFTER');
0 ignored issues
show
Security Debugging Code introduced by
var_dump('AFTER'); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
35
        var_dump($ttt);
36
37
        return $ttt;
38
    }
39
40
    /**
41
     * Transform a multidimensional array into a flat array.
42
     *
43
     * We could use a iterator_to_array() with a custom RecursiveArrayIterator.
44
     * But it seems to be even slower.
45
     *
46
     * @see http://php.net/manual/en/class.recursivearrayiterator.php#106519
47
     *
48
     * @param mixed[] $array
49
     *   The input array.
50
     *
51
     * @return mixed[]
52
     *   The array with only one dimension.
53
     */
54
    public function ensureFlatArray(array $array)
55
    {
56
        return \array_reduce(
57
            $array,
58
            function ($carry, $item) {
59
                if (\is_array($item)) {
60
                    return \array_merge(
61
                        $carry,
62
                        $this->ensureFlatArray($item)
63
                    );
64
                }
65
66
                $carry[] = $item;
67
68
                return $carry;
69
            },
70
            []
71
        );
72
    }
73
74
    /**
75
     * Make sure the value is an array.
76
     *
77
     * @param mixed $data
78
     *   The input value.
79
     *
80
     * @return string|NULL
81
     *   The input value in an array.
82
     */
83
    public function ensureString($data)
84
    {
85
        $return = null;
86
87
        switch (\gettype($data)) {
88
            case 'string':
89
                $return = $data;
90
                break;
91
            case 'integer':
92
            case 'double':
93
                $return = (string) $data;
94
                break;
95
            case 'object':
96
                if (\method_exists($data, '__toString')) {
97
                    $return = $data->__toString();
98
                }
99
                break;
100
            case 'boolean':
101
            case 'array':
102
            default:
103
                $return = NULL;
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
104
                break;
105
        }
106
107
        return $return;
108
    }
109
110
    /**
111
     * Preprocess values before they are returned to user.
112
     *
113
     * @param array $values
114
     *   The raw values.
115
     * @param string $name
116
     *   The name of the object.
117
     *
118
     * @return array|\drupol\htmltag\Attribute\AttributeInterface[]
119
     *   The values.
120
     */
121
    public function preprocess(array $values, $name = null)
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
122
    {
123
        return $values;
124
    }
125
126
    /**
127
     * Sanitize a value.
128
     *
129
     * @param string|null $value
130
     *   The value to sanitize
131
     *
132
     * @return string
133
     *   The value sanitized.
134
     */
135
    abstract protected function escape($value);
136
}
137