|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Cerbero\JsonObjects; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use JsonStreamingParser\Parser; |
|
7
|
|
|
use Cerbero\JsonObjects\Listeners\AbstractListener; |
|
8
|
|
|
use Cerbero\JsonObjects\Listeners\ChunkListener; |
|
9
|
|
|
use Cerbero\JsonObjects\Listeners\ObjectListener; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* The JSON objects main class. |
|
13
|
|
|
* |
|
14
|
|
|
*/ |
|
15
|
|
|
class JsonObjects |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* The JSON stream. |
|
19
|
|
|
* |
|
20
|
|
|
* @var resource |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $stream; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The key containing the JSON objects. |
|
26
|
|
|
* |
|
27
|
|
|
* @var string|null |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $key; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Set the dependencies. |
|
33
|
|
|
* |
|
34
|
|
|
* @param resource|string $source |
|
35
|
|
|
* @param string|null $key |
|
36
|
|
|
* |
|
37
|
|
|
* @throws JsonObjectsException |
|
38
|
|
|
*/ |
|
39
|
10 |
|
public function __construct($source, string $key = null) |
|
40
|
|
|
{ |
|
41
|
10 |
|
$this->stream = (new DataStreaming())->streamData($source); |
|
42
|
9 |
|
$this->key = $key; |
|
43
|
9 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Create a new instance while easing method chaining |
|
47
|
|
|
* |
|
48
|
|
|
* @param resource|string $source |
|
49
|
|
|
* @param string|null $key |
|
50
|
|
|
* @return self |
|
51
|
|
|
* |
|
52
|
|
|
* @throws JsonObjectsException |
|
53
|
|
|
*/ |
|
54
|
9 |
|
public static function from($source, string $key = null): self |
|
55
|
|
|
{ |
|
56
|
9 |
|
return new static($source, $key); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Process each JSON object separately |
|
61
|
|
|
* |
|
62
|
|
|
* @param callable $callback |
|
63
|
|
|
* @return void |
|
64
|
|
|
* |
|
65
|
|
|
* @throws JsonObjectsException |
|
66
|
|
|
*/ |
|
67
|
4 |
|
public function each(callable $callback): void |
|
68
|
|
|
{ |
|
69
|
4 |
|
$this->parseStreamWithListener(new ObjectListener($callback)); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Parse the JSON stream with the given listener |
|
74
|
|
|
* |
|
75
|
|
|
* @param AbstractListener $listener |
|
76
|
|
|
* @return void |
|
77
|
|
|
* |
|
78
|
|
|
* @throws JsonObjectsException |
|
79
|
|
|
*/ |
|
80
|
8 |
|
protected function parseStreamWithListener(AbstractListener $listener): void |
|
81
|
|
|
{ |
|
82
|
8 |
|
if ($this->key !== null) { |
|
83
|
5 |
|
$listener->setTargetFromKey($this->key); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
try { |
|
87
|
8 |
|
(new Parser($this->stream, $listener))->parse(); |
|
88
|
7 |
|
} catch (Exception $e) { |
|
89
|
7 |
|
throw new JsonObjectsException($e->getMessage()); |
|
90
|
1 |
|
} finally { |
|
91
|
8 |
|
extension_loaded('zlib') ? gzclose($this->stream) : fclose($this->stream); |
|
92
|
|
|
} |
|
93
|
1 |
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Process JSON objects in chunks |
|
97
|
|
|
* |
|
98
|
|
|
* @param int $size |
|
99
|
|
|
* @param callable $callback |
|
100
|
|
|
* @return void |
|
101
|
|
|
* |
|
102
|
|
|
* @throws JsonObjectsException |
|
103
|
|
|
*/ |
|
104
|
4 |
|
public function chunk(int $size, callable $callback): void |
|
105
|
|
|
{ |
|
106
|
4 |
|
$this->parseStreamWithListener(new ChunkListener($size, $callback)); |
|
107
|
1 |
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|