Output::variable()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 2
b 0
f 0
nc 2
nop 2
dl 0
loc 7
rs 10
1
<?php
2
3
    namespace rAPId\Debug;
4
5
    class Output
6
    {
7
        /**
8
         * @param mixed  $data
9
         * @param string $identifier
10
         */
11
        public static function variable($data, $identifier = '') {
12
            if (!empty($identifier)) {
13
                $output = self::get_variable_text_with_title($data, $identifier);
14
            } else {
15
                $output = self::get_variable_text($data);
16
            }
17
            echo "\n$output\n";
18
        }
19
20
        private static function get_variable_text_with_title($data, $title) {
21
22
            $result = str_repeat('_', strlen($title)) . "_\n";
23
            $result .= $title . " |\n";
24
            $result .= "============================================================================\n";
25
            $result .= self::get_variable_text($data);
26
            $result .= "\n============================================================================";
27
28
            return $result;
29
        }
30
31
        private static function get_variable_text($var) {
32
            $type = gettype($var);
33
            if (method_exists(self::class, $type . '_text')) {
34
                return self::{$type . '_text'}($var);
35
            }
36
37
            return $type . ': ' . print_r($var, true);
38
        }
39
40
        private static function string_text($string) {
41
            return 'string: "' . $string . '"';
42
        }
43
44
        private static function integer_text($int) {
45
            return 'integer: ' . $int;
46
        }
47
48
        private static function double_text($double) {
49
            return 'float: ' . $double;
50
        }
51
52
        private static function boolean_text($bool) {
53
            $txt_bool = $bool ? 'true' : 'false';
54
55
            return 'boolean: ' . $txt_bool;
56
        }
57
58
        private static function NULL_text($null) {
59
            return 'null';
60
        }
61
62
        private static function array_text($array) {
63
            return print_r($array, true);
64
        }
65
66
        private static function object_text($object) {
67
            if (is_a($object, \Generator::class)) {
68
                return self::generator_text($object);
69
            }
70
71
            return print_r($object, true);
72
        }
73
74
        private static function generator_text($generator) {
75
            $output = print_r(iterator_to_array($generator), true);
76
            $output = implode('<< Generator >>', explode('Array', $output, 2));
77
78
            return $output;
79
        }
80
    }