1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JsonCollectionParser; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use JsonCollectionParser\Stream\DataStream; |
7
|
|
|
use JsonStreamingParser\Listener\ListenerInterface; |
8
|
|
|
use JsonStreamingParser\Parser as BaseParser; |
9
|
|
|
|
10
|
|
|
class Parser |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
protected $options = [ |
16
|
|
|
'line_ending' => "\n", |
17
|
|
|
'emit_whitespace' => false, |
18
|
|
|
]; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var BaseParser |
22
|
|
|
*/ |
23
|
|
|
protected $parser; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var bool |
27
|
|
|
*/ |
28
|
|
|
protected $gzipSupported; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var resource |
32
|
|
|
*/ |
33
|
|
|
protected $stream; |
34
|
|
|
|
35
|
|
|
public function __construct() |
36
|
|
|
{ |
37
|
|
|
$this->gzipSupported = extension_loaded('zlib'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string|resource $input File path or resource |
42
|
|
|
* @param callback|callable $itemCallback Callback |
43
|
|
|
* @param bool $assoc Parse as associative arrays |
44
|
|
|
* |
45
|
|
|
* @throws Exception |
46
|
|
|
*/ |
47
|
|
|
public function parse($input, $itemCallback, bool $assoc = true): void |
48
|
|
|
{ |
49
|
|
|
$this->checkCallback($itemCallback); |
50
|
|
|
|
51
|
|
|
$this->parseStream($input, new Listener($itemCallback, $assoc)); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param int $size Size of the chunk to collect before processing |
56
|
|
|
* @param string|resource $input File path or resource |
57
|
|
|
* @param callback|callable $itemCallback Callback |
58
|
|
|
* @param bool $assoc Parse as associative arrays |
59
|
|
|
* |
60
|
|
|
* @throws Exception |
61
|
|
|
*/ |
62
|
|
|
public function chunk($input, $itemCallback, int $size, bool $assoc = true): void |
63
|
|
|
{ |
64
|
|
|
$this->checkCallback($itemCallback); |
65
|
|
|
|
66
|
|
|
$this->parseStream($input, new ChunkListener($size, $itemCallback, $assoc)); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string|resource $input File path or resource |
71
|
|
|
* @param callback|callable $itemCallback Callback |
72
|
|
|
* |
73
|
|
|
* @throws Exception |
74
|
|
|
*/ |
75
|
|
|
public function parseAsObjects($input, $itemCallback): void |
76
|
|
|
{ |
77
|
|
|
$this->parse($input, $itemCallback, false); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string|resource $input File path or resource |
82
|
|
|
* @param callback|callable $itemCallback Callback |
83
|
|
|
* @param int $size Size of the chunk to collect before processing |
84
|
|
|
* |
85
|
|
|
* @throws Exception |
86
|
|
|
*/ |
87
|
|
|
public function chunkAsObjects($input, $itemCallback, int $size): void |
88
|
|
|
{ |
89
|
|
|
$this->chunk($input, $itemCallback, $size, false); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function stop(): void |
93
|
|
|
{ |
94
|
|
|
$this->parser->stop(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string $name |
99
|
|
|
* @param mixed $value |
100
|
|
|
*/ |
101
|
|
|
public function setOption(string $name, $value): void |
102
|
|
|
{ |
103
|
|
|
$this->options[$name] = $value; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param string $name |
108
|
|
|
* |
109
|
|
|
* @return mixed |
110
|
|
|
*/ |
111
|
|
|
public function getOption(string $name) |
112
|
|
|
{ |
113
|
|
|
if (isset($this->options[$name])) { |
114
|
|
|
return $this->options[$name]; |
115
|
|
|
} else { |
116
|
|
|
return null; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param string|resource $input File path or resource |
122
|
|
|
* @param ListenerInterface $listener |
123
|
|
|
* |
124
|
|
|
* @throws Exception |
125
|
|
|
*/ |
126
|
|
|
protected function parseStream($input, ListenerInterface $listener) |
127
|
|
|
{ |
128
|
|
|
$stream = DataStream::get($input); |
129
|
|
|
|
130
|
|
|
try { |
131
|
|
|
$this->parser = new BaseParser( |
132
|
|
|
$stream, |
133
|
|
|
$listener, |
134
|
|
|
$this->getOption('line_ending'), |
135
|
|
|
$this->getOption('emit_whitespace') |
136
|
|
|
); |
137
|
|
|
$this->parser->parse(); |
138
|
|
|
} catch (Exception $e) { |
139
|
|
|
throw $e; |
140
|
|
|
} finally { |
141
|
|
|
DataStream::close($stream); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param callback|callable $callback |
147
|
|
|
* |
148
|
|
|
* @throws Exception |
149
|
|
|
*/ |
150
|
|
|
protected function checkCallback($callback) |
151
|
|
|
{ |
152
|
|
|
if (!is_callable($callback)) { |
153
|
|
|
throw new Exception("Callback should be callable"); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|