Completed
Branch master (254118)
by Alexander
01:40
created

Html::dump_array()   D

Complexity

Conditions 9
Paths 3

Size

Total Lines 30
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 24
nc 3
nop 2
dl 0
loc 30
rs 4.909
c 0
b 0
f 0
1
<?php
2
/**
3
 * Global debug methods
4
 */
5
namespace alkemann\h2l\internals\debug\adapters;
6
7
use alkemann\h2l\internals\debug\Debug;
8
9
class Html {
10
11
    public static function dump_array(array $array, Debug $debug) : string
12
    {
13
        $debug->current_depth++;
14
        $count = count($array);
15
        $ret = ' <span class="class">array</span>';
16
        $ret .= '[<span class="count">' . $count . '</span>]</li>';
17
        if ($count > 0) {
18
            $ret .= '<ul class="array">';
19
            if (in_array('array', $debug->options['avoid'])) {
20
                $ret .= '<li><span class="empty"> -- Array Type Avoided -- </span></li>';
21
            } else
22
                foreach ($array as $key => $value) {
23
                    $ret .= '<li>[ <span class="key">' . $key . '</span> ] => ';
24
                    if (is_string($key) && in_array($key, $debug->options['blacklist']['key'])) {
25
                        $ret .= '<span class="empty"> -- Blacklisted Key Avoided -- </span></li>';
26
                        continue;
27
                    }
28
                    if ((is_array($value) || is_object($value)) && $debug->current_depth >= $debug->options['depth']) {
29
                        $ret .= ' <span class="class">array</span> ';
30
                        $ret .= '[<span class="count">' . count($value) . '</span>]</li>';
31
                        $ret .= '<ul class="array"><li><span class="empty"> -- Debug Depth reached -- </span></li></ul>';
32
                        continue;
33
                    }
34
                    $ret .= $debug->dump_it($value);
35
                }
36
            $ret .= '</ul>';
37
        }
38
        $debug->current_depth--;
39
        return $ret;
40
    }
41
42
    public static function dump_object($obj, Debug $debug) : string
43
    {
44
        $debug->current_depth++;
45
        $hash = spl_object_hash($obj);
46
        $id = substr($hash, 9, 7);
47
        $class = get_class($obj);
48
        $ret = ' object[ <span class="class-id"> ' . $id . ' </span> ] ';
49
        $ret .= ' class[ <span class="class">' . $class . '</span> ] </li>';
50
        $ret .= '<ul class="properties">';
51
        if (in_array(get_class($obj), $debug->options['blacklist']['class'])) {
52
            $debug->current_depth--;
53
            return $ret . '<li><span class="empty"> -- Blacklisted Object Avoided -- </span></li></ul>';
54
        }
55
        if (isset($debug->object_references[$hash]))  {
56
            $debug->current_depth--;
57
            return $ret . '<li><span class="empty"> -- Object Recursion Avoided -- </span></li></ul>';
58
        }
59
        if (in_array('object', $debug->options['avoid']))  {
60
            $debug->current_depth--;
61
            return $ret . '<li><span class="empty"> -- Object Type Avoided -- </span></li></ul>';
62
        }
63
        if ($debug->current_depth > $debug->options['depth']) {
64
            $debug->current_depth--;
65
            return $ret . '<li><span class="empty"> -- Debug Depth reached -- </span></li></ul>';
66
        }
67
        $debug->object_references[$hash] = true;
68
        $reflection = new \ReflectionObject($obj);
69
        $props = '';
70
        foreach (array(
71
            'public' => \ReflectionProperty::IS_PUBLIC,
72
            'protected' => \ReflectionProperty::IS_PROTECTED,
73
            'private' => \ReflectionProperty::IS_PRIVATE
74
            ) as $type => $rule) {
75
                $props .= self::dump_properties($reflection, $obj, $type, $rule, $debug);
76
        }
77
        $debug->current_depth--;
78
        if ($props == '') return $ret .= '<li><span class="empty"> -- No properties -- </span></li></ul>';
79
        else  $ret .=  $props;
80
        $ret .= '</ul>';
81
        return  $ret;
82
    }
83
84
    public static function dump_properties(\ReflectionObject $reflection, $obj, string $type, string $rule, Debug $debug) : string
85
    {
86
        $vars = $reflection->getProperties($rule);
87
        $i = 0; $ret = '';
88
        foreach ($vars as $refProp) {
89
            $property = $refProp->getName();
90
            $i++;
91
            $refProp->setAccessible(true);
92
            $value = $refProp->getValue($obj);
93
            $ret .= '<li>';
94
            $ret .= '<span class="access">' . $type . '</span> <span class="property">' . $property . '</span> ';
95
            if (in_array($property, $debug->options['blacklist']['property']))
96
                $ret .= ' : <span class="empty"> -- Blacklisted Property Avoided -- </span>';
97
            else
98
                $ret .= ' : ' . $debug->dump_it($value);
99
            $ret .= '</li>';
100
        }
101
        return $i ? $ret : '';
102
    }
103
104
    public static function dump_other($var) : string
105
    {
106
        $type = gettype($var);
107
        switch ($type) {
108
            case 'boolean': $var = $var ? 'true' : 'false'; break;
109
            case 'string' : $length = strlen($var); $var = '\'' . htmlentities($var) . '\''; break;
110
            case 'NULL' : return '<span class="empty">NULL</span>'; break;
111
        }
112
		$ret = '<span class="value ' . $type .'">' . $var . '</span> ';
113
114
		if ($type == 'string') {
115
			$ret .= '<span class="type">string[' . $length . ']</span>';
116
		} else {
117
			$ret .= '<span class="type">' . $type . '</span>';
118
		}
119
        return $ret;
120
    }
121
122
    public static function locationString(array $location) : string
123
    {
124
        extract($location);
125
        $ret = "line: <span>$line</span> &nbsp;".
126
               "file: <span>$file</span> &nbsp;";
127
        $ret .= isset($class) ? "class: <span>$class</span> &nbsp;" :'';
128
        $ret .= isset($function) && $function != 'include' ? "function: <span>$function</span> &nbsp;" :'';
129
        return $ret;
130
    }
131
132
    public static function top(array $outputs, ?string $key = null) : void
133
    {
134
            if (empty($outputs)) return;
135
            echo '<div id="debugger">';
136
            if ($key !== null) {
137
                    if (!isset($outputs[$key])) {
138
                            throw new Exception('DEBUG: Not that many outputs in buffer');
139
                    }
140
                    echo $outputs[$key];
141
                    return;
142
            }
143
            foreach ($outputs as $out) {
144
                    echo $out;
145
            }
146
            echo '</div>';
147
    }
148
149
}
150