-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathEncoder.php
More file actions
179 lines (157 loc) · 3.41 KB
/
Encoder.php
File metadata and controls
179 lines (157 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
namespace JsonStream;
class Encoder
{
private $_stream;
private $_echo;
/**
* @param resource $stream A stream resource.
* @throws \InvalidArgumentException If $stream is not a stream resource.
*/
public function __construct($stream,$echo=false)
{
$this->_echo=$echo;
if (!$this->_echo && (!is_resource($stream) || get_resource_type($stream) != 'stream')) {
throw new \InvalidArgumentException("Resource is not a stream");
}
if(!$this->_echo)
$this->_stream = $stream;
}
/**
* Encodes a value and writes it to the stream.
*
* @param mixed $value
*/
public function encode($value)
{
// null, bool and scalar values
if(is_null($value)) {
$this->_writeValue('null');
return;
}
elseif ($value === false) {
$this->_writeValue('false');
return;
}
elseif ($value === true) {
$this->_writeValue('true');
return;
}
elseif (is_scalar($value)) {
$this->_encodeScalar($value);
return;
}
// array of values
if ($this->_isList($value)) {
$this->_encodeList($value);
return;
}
// objects and associative arrays
else {
$this->_encodeObject($value);
return;
}
}
/**
* Writes a value to the stream.
*
* @param string $value
*/
private function _writeValue($value)
{
if(!$this->_echo){
fwrite($this->_stream, $value);
} else {
echo $value;
}
}
/**
* Encodes a scalar value.
*
* @param mixed $value
*/
private function _encodeScalar($value)
{
if (is_float($value)) {
// Always use "." for floats.
$encodedValue = floatval(str_replace(",", ".", strval($value)));
}
elseif (is_string($value)) {
$encodedValue = $this->_encodeString($value);
}
else {
// otherwise this must be an int
$encodedValue = $value;
}
$this->_writeValue($encodedValue);
}
/**
* Encodes a string.
*
* @param $string
* @return string
*/
private function _encodeString($string)
{
static $jsonReplaces = array(array("\\", "/", "\n", "\t", "\r", "\b", "\f", '"', "\0"), array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"', '\u0000'));
return '"' . str_replace($jsonReplaces[0], $jsonReplaces[1], $string) . '"';
}
/**
* Checks if a value is a flat list of values (simple array) or a map (assoc. array or object).
*
* @param mixed $value
* @return bool
*/
private function _isList($value)
{
// objects that are not explicitly traversable could never have integer keys, therefore they are not a list
if (is_object($value) && !($value instanceof \Traversable)) {
return false;
}
// check if the array/object has only integer keys.
$i = 0;
foreach ($value as $key => $element) {
if ($key !== $i) {
return false;
}
$i++;
}
return true;
}
/**
* Encodes a list of values.
*
* @param array $list
*/
private function _encodeList($list)
{
$this->_writeValue('[');
foreach ($list as $x => $value) {
$this->encode($value);
if ($x < count($list) - 1) {
$this->_writeValue(',');
}
}
$this->_writeValue(']');
}
/**
* Encodes an object or associative array.
*
* @param array|object $object
*/
private function _encodeObject($object)
{
$this->_writeValue('{');
$firstIteration = true;
foreach ($object as $key => $value) {
if (!$firstIteration) {
$this->_writeValue(',');
}
$firstIteration = false;
$this->_encodeScalar((string)$key);
$this->_writeValue(':');
$this->encode($value);
}
$this->_writeValue('}');
}
}