Completed
Push — master ( 39f77b...267ff0 )
by Chris
02:13
created

Output::generator_text()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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