Completed
Branch feature/psr-7-support (86b985)
by Andrea Marco
01:24
created

JsonObjects::parseStreamWithListener()   A

Complexity

Conditions 4
Paths 12

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 12
nop 1
dl 0
loc 12
ccs 8
cts 8
cp 1
crap 4
rs 10
c 0
b 0
f 0
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 30
    public function __construct($source, string $key = null)
40
    {
41 30
        $this->stream = (new DataStreaming)->streamData($source);
42 27
        $this->key = $key;
43 27
    }
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 27
    public static function from($source, string $key = null) : self
55
    {
56 27
        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 12
    public function each(callable $callback) : void
68
    {
69 12
        $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 24
    protected function parseStreamWithListener(AbstractListener $listener) : void
81
    {
82 24
        if ($this->key !== null) {
83 15
            $listener->setTargetFromKey($this->key);
84
        }
85
86
        try {
87 24
            (new Parser($this->stream, $listener))->parse();
88 21
        } catch (Exception $e) {
89 21
            throw new JsonObjectsException($e->getMessage());
90 3
        } finally {
91 24
            extension_loaded('zlib') ? gzclose($this->stream) : fclose($this->stream);
92
        }
93 3
    }
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 12
    public function chunk(int $size, callable $callback) : void
105
    {
106 12
        $this->parseStreamWithListener(new ChunkListener($size, $callback));
107 3
    }
108
}
109