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