forked from skolodyazhnyy/json-stream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWriter.php
290 lines (256 loc) · 6.83 KB
/
Writer.php
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<?php
/**
*
* This file is part of the JSON Stream Project.
*
* @author Sergey Kolodyazhnyy <[email protected]>
*
*/
namespace Bcn\Component\Json;
use Bcn\Component\Json\Exception\WritingError;
/**
* Class Writer
* @package Bcn\Component\Json
*/
class Writer
{
const TYPE_OBJECT = 1;
const TYPE_ARRAY = 2;
const TYPE_NULL = 3;
const TYPE_BOOL = 4;
const TYPE_SCALAR = 5;
const TYPE_URI = 6;
const CONTEXT_NONE = 0;
const CONTEXT_ARRAY = 1;
const CONTEXT_ARRAY_START = 2;
const CONTEXT_OBJECT = 3;
const CONTEXT_OBJECT_START = 4;
protected $stream;
protected $context;
protected $parents = array();
/**
* @param resource $stream A stream resource.
* @throws \InvalidArgumentException If $stream is not a stream resource.
*/
public function __construct($stream)
{
if (!is_resource($stream) || get_resource_type($stream) != 'stream') {
throw new \InvalidArgumentException("Resource is not a stream");
}
$this->stream = $stream;
$this->context = self::CONTEXT_NONE;
}
/**
* @param $key
* @param $value
* @param null $type
* @return $this
* @throws Exception\WritingError
*/
public function write($key, $value, $type = null)
{
if ($value instanceof \JsonSerializable) {
$value = $value->jsonSerialize();
}
switch ($type ? : $this->getType($value)) {
case self::TYPE_NULL:
$this->prefix($key);
$this->streamWrite('null');
break;
case self::TYPE_BOOL:
$this->prefix($key);
$this->streamWrite($value ? 'true' : 'false');
break;
case self::TYPE_SCALAR:
$this->prefix($key);
$this->scalar($value);
break;
case self::TYPE_ARRAY:
$this->encodeArray($key, $value);
break;
case self::TYPE_OBJECT:
$this->encodeObject($key, $value);
break;
case self::TYPE_URI:
$this->prefix($key);
$this->uri($value);
break;
default:
throw new WritingError("Unrecognized type");
}
$this->streamFlush();
return $this;
}
/**
* @param string $key
* @param int $type
* @return $this
*/
public function enter($key = null, $type = null)
{
if ($type === null) {
if (in_array($key, array(self::TYPE_OBJECT, self::TYPE_ARRAY))) {
$type = $key;
$key = null;
} else {
$type = self::TYPE_ARRAY;
}
}
$this->prefix($key);
array_push($this->parents, $this->context);
$this->context = $type == self::TYPE_OBJECT ? self::CONTEXT_OBJECT_START : self::CONTEXT_ARRAY_START;
return $this;
}
/**
* @return $this
*/
public function leave()
{
switch ($this->context) {
case self::CONTEXT_OBJECT:
$this->streamWrite("}");
break;
case self::CONTEXT_OBJECT_START:
$this->streamWrite("{}");
break;
case self::CONTEXT_ARRAY:
$this->streamWrite("]");
break;
case self::CONTEXT_ARRAY_START:
$this->streamWrite("[]");
break;
}
$this->context = array_pop($this->parents);
return $this;
}
/**
* @param $value
* @return string
*/
protected function getType($value)
{
if (is_bool($value)) {
return self::TYPE_BOOL;
}
if (is_scalar($value)) {
return self::TYPE_SCALAR;
}
if (is_array($value) && empty($value)) {
return self::TYPE_ARRAY;
}
if (is_array($value) && !$this->isAssociative($value)) {
return self::TYPE_ARRAY;
}
if (is_object($value) && $value instanceof \Traversable) {
return self::TYPE_ARRAY;
}
if (is_object($value) || is_array($value)) {
return self::TYPE_OBJECT;
}
return self::TYPE_NULL;
}
/**
* @param $value
* @return $this
*/
public function scalar($value)
{
$this->streamWrite(json_encode($value, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT));
return $this;
}
/**
* @param $value
* @return $this
*/
public function uri($value)
{
$this->streamWrite(json_encode($value, JSON_UNESCAPED_SLASHES));
return $this;
}
/**
* @param $value
* @return bool
*/
protected function isAssociative($value)
{
if (is_object($value) && $value instanceof \Traversable && !($value instanceof \ArrayAccess)) {
return false;
}
if (!is_array($value)) {
return true;
}
$keys = array_keys($value);
sort($keys);
return end($keys) !== count($keys) - 1;
}
/**
* @param $key
* @param $array
*/
protected function encodeArray($key, $array)
{
$this->enter($key, self::TYPE_ARRAY);
foreach ($array as $value) {
$this->write(null, $value);
}
$this->leave();
}
/**
* @param $key
* @param $object
*/
protected function encodeObject($key, $object)
{
$this->enter($key, self::TYPE_OBJECT);
foreach ($object as $key => $value) {
$this->write((string) $key, $value);
}
$this->leave();
}
/**
* @param $value
*/
protected function streamWrite($value)
{
fwrite($this->stream, $value);
}
/**
*
*/
protected function streamFlush()
{
fflush($this->stream);
}
/**
* @param $key
*/
protected function key($key)
{
$this->scalar((string) $key);
$this->streamWrite(":");
}
/**
* @param $key
*/
protected function prefix($key)
{
switch ($this->context) {
case self::CONTEXT_OBJECT_START:
$this->streamWrite("{");
$this->key($key);
$this->context = self::CONTEXT_OBJECT;
break;
case self::CONTEXT_ARRAY_START:
$this->streamWrite("[");
$this->context = self::CONTEXT_ARRAY;
break;
case self::CONTEXT_OBJECT:
$this->streamWrite(',');
$this->key($key);
break;
case self::CONTEXT_ARRAY:
$this->streamWrite(',');
break;
}
}
}