Completed
Push — master ( 187e82...97d744 )
by Виктор
04:35
created

VarExport::exportElement()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 4
dl 0
loc 7
ccs 6
cts 6
cp 1
crap 1
rs 9.4285
1
<?php
2
3
namespace SakhCom\VarExport;
4
5
/**
6
 * Class VarExport
7
 *
8
 * @package SakhCom\VarExport
9
 */
10
class VarExport
11
{
12
13
    private $variable;
14
15
    /**
16
     * VarExport constructor.
17
     *
18
     * @param mixed $variable
19
     */
20 44
    public function __construct($variable)
21
    {
22 44
        $this->variable = $variable;
23 44
    }
24
25
    /**
26
     * Export the variable
27
     *
28
     * @param int $maxDepth maximum depth
29
     * @return string
30
     */
31 44
    public function export($maxDepth = -1)
32
    {
33 44
        return $this->exportRef($this->variable, $maxDepth === -1 ? -1 : $maxDepth + 1);
34
    }
35
36
    /**
37
     * @param mixed $ref
38
     * @param int $maxDepth maximum depth
39
     * @param int $indent
40
     * @return string
41
     */
42 44
    private function exportRef($ref, $maxDepth, $indent = 0)
43
    {
44 44
        if ($maxDepth !== -1) {
45 6
            $maxDepth--;
46
        }
47
48 44
        switch (gettype($ref)) {
49 44
            case 'boolean':
50 4
                return $ref ? 'true' : 'false';
51 40
            case 'integer':
52 34
            case 'double':
53 24
                return $ref;
54 32
            case 'string':
55 20
                return "'" . $ref . "'";
56 28
            case 'resource':
57 26
            case 'NULL':
58 2
                return 'NULL';
59 26
            case 'array':
60 16
                $output = '';
61 16
                $baseIndentString = str_repeat(' ', $indent * 2);
62 16
                $indentString = str_repeat(' ', ($indent + 1) * 2);
63 16
                if ($indent > 0) {
64 8
                    $output .= PHP_EOL . $baseIndentString;
65
                }
66 16
                $output .= 'array (';
67 16
                if ($maxDepth === -1 || $maxDepth > 0) {
68 16
                    $output .= PHP_EOL;
69 16
                    foreach ($ref as $key => $value) {
70 16
                        $output .= $indentString . $this->exportElement($key, $value, $maxDepth, $indent);
71
                    }
72 16
                    $output .= $baseIndentString . ')';
73
                } else {
74 4
                    $output .= '...)';
75
                }
76 16
                return $output;
77 20
            case 'object':
78 20
                $output = '';
79 20
                $baseIndentString = str_repeat(' ', $indent * 2);
80 20
                $indentString = str_repeat(' ', ($indent + 1) * 2 + 1);
81 20
                if ($indent > 0) {
82 8
                    $output .= PHP_EOL . $baseIndentString;
83
                }
84 20
                $output .= get_class($ref) . '::__set_state(array(';
85 20
                if ($maxDepth === -1 || $maxDepth > 0) {
86 20
                    $output .= PHP_EOL;
87 20
                    $reflection = new \ReflectionClass($ref);
88 20
                    foreach (get_object_vars($ref) as $key => $value) {
89 14
                        $output .= $indentString . $this->exportElement($key, $value, $maxDepth, $indent);
90
                    }
91
92 20
                    foreach ($reflection->getProperties(\ReflectionProperty::IS_PRIVATE | \ReflectionProperty::IS_PROTECTED) as $property) {
93 6
                        $key = $property->getName();
94 6
                        $property->setAccessible(true);
95 6
                        $value = $property->getValue($ref);
96 6
                        $output .= $indentString . $this->exportElement($key, $value, $maxDepth, $indent);
97 6
                        $property->setAccessible(false);
98
                    }
99
100 20
                    $output .= $baseIndentString . '))';
101
                } else {
102 2
                    $output .= '...))';
103
                }
104 20
                return $output;
105
            default:
106
                return '{unknown}';
107
        }
108
    }
109
110
    /**
111
     * @param $key
112
     * @param $value
113
     * @param $maxDepth
114
     * @param $indent
115
     * @return string
116
     */
117 24
    private function exportElement($key, $value, $maxDepth, $indent)
118
    {
119 24
        return $this->exportRef($key, $maxDepth, $indent + 1) .
120 24
            ' => ' .
121 24
            $this->exportRef($value, $maxDepth, $indent + 1) .
122 24
            ',' .
123 24
            PHP_EOL;
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     *
129
     * @return string
130
     */
131 30
    public function __toString()
132
    {
133 30
        return (string)$this->export();
134
    }
135
}
136