|
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
|
46 |
|
public function __construct($variable) |
|
21
|
|
|
{ |
|
22
|
46 |
|
$this->variable = $variable; |
|
23
|
46 |
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Export the variable |
|
27
|
|
|
* |
|
28
|
|
|
* @param int $maxDepth maximum depth |
|
29
|
|
|
* @return string |
|
30
|
|
|
*/ |
|
31
|
46 |
|
public function export($maxDepth = -1) |
|
32
|
|
|
{ |
|
33
|
46 |
|
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
|
46 |
|
private function exportRef($ref, $maxDepth, $indent = 0) |
|
43
|
|
|
{ |
|
44
|
46 |
|
if ($maxDepth !== -1) { |
|
45
|
6 |
|
$maxDepth--; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
46 |
|
switch (gettype($ref)) { |
|
49
|
46 |
|
case 'boolean': |
|
50
|
4 |
|
return $ref ? 'true' : 'false'; |
|
51
|
42 |
|
case 'integer': |
|
52
|
36 |
|
case 'double': |
|
53
|
24 |
|
return $ref; |
|
54
|
34 |
|
case 'string': |
|
55
|
22 |
|
return "'" . $this->escapeString($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
|
|
|
* @param string $string |
|
128
|
|
|
* @return string |
|
129
|
|
|
*/ |
|
130
|
22 |
|
private function escapeString($string) |
|
131
|
|
|
{ |
|
132
|
22 |
|
$string = str_replace('\\', '\\\\', $string); |
|
133
|
22 |
|
$string = preg_replace("#(?!=\\\)'#", '\\\'', $string); |
|
134
|
22 |
|
return $string; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* {@inheritdoc} |
|
139
|
|
|
* |
|
140
|
|
|
* @return string |
|
141
|
|
|
*/ |
|
142
|
32 |
|
public function __toString() |
|
143
|
|
|
{ |
|
144
|
32 |
|
return (string)$this->export(); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|