1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Vlaswinkel\Lua; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Serializer |
7
|
|
|
* |
8
|
|
|
* @see https://github.com/Sorroko/cclite/blob/62677542ed63bd4db212f83da1357cb953e82ce3/src/lua/rom/apis/textutils |
9
|
|
|
* |
10
|
|
|
* @author Koen Vlaswinkel <[email protected]> |
11
|
|
|
* @package Vlaswinkel\Lua |
12
|
|
|
*/ |
13
|
|
|
class Serializer { |
14
|
|
|
public static function encode($data, $indent = '') { |
15
|
|
|
if (is_null($data)) { |
16
|
|
|
return self::encodeNull(); |
17
|
|
|
} else { |
18
|
|
|
if (is_array($data)) { |
19
|
|
|
return self::encodeArray($data, $indent); |
20
|
|
|
} else { |
21
|
|
|
if (is_object($data)) { |
22
|
|
|
return self::encodeArray((array)$data, $indent); |
23
|
|
|
} else { |
24
|
|
|
if (is_string($data)) { |
25
|
|
|
return self::encodeString($data); |
26
|
|
|
} else { |
27
|
|
|
if (is_numeric($data)) { |
28
|
|
|
return self::encodeNumber($data); |
29
|
|
|
} else { |
30
|
|
|
if (is_bool($data)) { |
31
|
|
|
return self::encodeBoolean($data); |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
throw new \InvalidArgumentException("Cannot encode type " . get_class($data) . ": " . var_export($data, true)); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
private static function encodeArray(array $data, $indent) { |
43
|
|
|
if (count($data) === 0) { |
44
|
|
|
return '{}'; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$result = "{\n"; |
48
|
|
|
$subIndent = $indent . ' '; |
49
|
|
|
$seen = []; |
50
|
|
|
foreach ($data as $key => $value) { |
51
|
|
|
if (is_int($key)) { |
52
|
|
|
$seen[$key] = true; |
53
|
|
|
$result .= $subIndent . self::encode($value, $subIndent) . ",\n"; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
foreach ($data as $key => $value) { |
58
|
|
|
if (!array_key_exists($key, $seen)) { |
59
|
|
|
if (is_string($key) |
60
|
|
|
&& !in_array($key, Lua::$luaKeywords) |
61
|
|
|
&& preg_match('/^[a-zA-Z_][a-zA-Z0-9_]*$/', $key) |
62
|
|
|
) { |
63
|
|
|
$entry = $key . ' = ' . self::encode($value, $subIndent) . ",\n"; |
64
|
|
|
} else { |
65
|
|
|
$entry = '[ ' . self::encode($key, $subIndent) . ' ] = ' . self::encode($value, $subIndent) . ",\n"; |
66
|
|
|
} |
67
|
|
|
$result = $result . $subIndent . $entry; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
$result = $result . $indent . '}'; |
71
|
|
|
return $result; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param $data |
76
|
|
|
* |
77
|
|
|
* @see http://luaj.cvs.sourceforge.net/viewvc/luaj/luaj-vm/src/core/org/luaj/vm2/lib/StringLib.java?view=markup |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
|
private static function encodeString($data) { |
81
|
|
|
$data = str_replace(["\n\r", "\r\n"], "\n", $data); |
82
|
|
|
$result = '"'; |
83
|
|
|
for ($i = 0, $n = strlen($data); $i < $n; $i++) { |
84
|
|
|
$char = $data[$i]; |
85
|
|
|
switch ($char) { |
86
|
|
|
case '"': |
87
|
|
|
case "\\": |
88
|
|
|
case "\n": |
89
|
|
|
$result .= "\\" . $char; |
90
|
|
|
break; |
91
|
|
|
default: |
92
|
|
|
if (($char <= chr(0x1F) || $char == chr(0x7F)) && $char != chr(9)) { |
93
|
|
|
$result .= "\\"; |
94
|
|
|
if ($i + 1 == $n || $data[$i + 1] < '0' || $data[$i + 1] > '9') { |
95
|
|
|
$result .= $char; |
96
|
|
|
} else { |
97
|
|
|
$result .= '0'; |
98
|
|
|
$result .= chr(ord('0') + $char / 10); |
99
|
|
|
$result .= chr(ord('0') + $char % 10); |
100
|
|
|
} |
101
|
|
|
} else { |
102
|
|
|
$result .= $char; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
$result .= '"'; |
107
|
|
|
return $result; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
private static function encodeNumber($data) { |
111
|
|
|
return $data; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
private static function encodeBoolean($data) { |
115
|
|
|
return $data ? 'true' : 'false'; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
private static function encodeNull() { |
119
|
|
|
return 'nil'; |
120
|
|
|
} |
121
|
|
|
} |