forked from skolodyazhnyy/json-stream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Reader.php
150 lines (124 loc) · 3.68 KB
/
Reader.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
<?php
/**
*
* This file is part of the JSON Stream Project.
*
* @author Sergey Kolodyazhnyy <[email protected]>
*
*/
namespace Bcn\Component\Json;
use Bcn\Component\Json\Exception\ReadingError;
use Bcn\Component\Json\Reader\Tokenizer;
class Reader
{
const TYPE_OBJECT = 1;
const TYPE_ARRAY = 2;
/** @var \Bcn\Component\Json\Reader\Tokenizer */
protected $tokenizer;
/** @var array */
protected $token;
/**
* @param resource $resource
*/
public function __construct($resource)
{
$this->tokenizer = new Tokenizer($resource);
$this->token = $this->tokenizer->next();
}
/**
* @param null|string $key
* @param null|string $type
* @return bool
*/
public function enter($key = null, $type = null)
{
if ($type === null && in_array($key, array(self::TYPE_OBJECT, self::TYPE_ARRAY))) {
$type = $key;
$key = null;
}
switch ($type) {
case self::TYPE_ARRAY:
$tokens = array(Tokenizer::TOKEN_ARRAY_START);
break;
case self::TYPE_OBJECT:
$tokens = array(Tokenizer::TOKEN_OBJECT_START);
break;
default:
$tokens = array(Tokenizer::TOKEN_ARRAY_START, Tokenizer::TOKEN_OBJECT_START);
}
if ($this->token['key'] != $key || !in_array($this->token['token'], $tokens)) {
return false;
}
$this->next();
return true;
}
/**
* @return bool
*/
public function leave()
{
if (!($context = $this->tokenizer->context())) {
return false;
}
$level = 0;
do {
switch ($this->token['token']) {
case Tokenizer::TOKEN_ARRAY_START:
case Tokenizer::TOKEN_OBJECT_START:
$level++;
break;
case Tokenizer::TOKEN_ARRAY_END:
case Tokenizer::TOKEN_OBJECT_END:
$level--;
break;
}
} while ($this->next() && $level >= 0 && $this->tokenizer->context());
return true;
}
/**
* @param string|null $key
* @return mixed
*/
public function read($key = null)
{
if ($this->token['key'] != $key) {
return false;
}
switch ($this->token['token']) {
case Tokenizer::TOKEN_SCALAR:
$value = $this->token['content'];
$this->next();
return $value;
case Tokenizer::TOKEN_ARRAY_START:
$items = array();
$this->enter($this->token['key']);
while ($this->token['token'] != Tokenizer::TOKEN_ARRAY_END) {
$items[] = $this->read();
if (!$this->token) {
throw new ReadingError("Unexpected object ending");
}
}
$this->leave();
return $items;
case Tokenizer::TOKEN_OBJECT_START:
$items = array();
$this->enter($this->token['key']);
while ($this->token['token'] != Tokenizer::TOKEN_OBJECT_END) {
$items[$this->token['key']] = $this->read($this->token['key']);
if (!$this->token) {
throw new ReadingError("Unexpected object ending");
}
}
$this->leave();
return $items;
}
return null;
}
/**
*
*/
protected function next()
{
return $this->token = $this->tokenizer->next();
}
}