|  | 
|  | 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 | +    public function __construct($source, string $key = null) | 
|  | 40 | +    { | 
|  | 41 | +        $this->setStreamFromSource($source); | 
|  | 42 | + | 
|  | 43 | +        $this->key = $key; | 
|  | 44 | +    } | 
|  | 45 | + | 
|  | 46 | +    /** | 
|  | 47 | +     * Set the JSON stream from the given source | 
|  | 48 | +     * | 
|  | 49 | +     * @param mixed $source | 
|  | 50 | +     * @return void | 
|  | 51 | +     * | 
|  | 52 | +     * @throws JsonObjectsException | 
|  | 53 | +     */ | 
|  | 54 | +    protected function setStreamFromSource($source) : void | 
|  | 55 | +    { | 
|  | 56 | +        if (is_resource($source)) { | 
|  | 57 | +            $this->stream = $source; | 
|  | 58 | +            return; | 
|  | 59 | +        } | 
|  | 60 | + | 
|  | 61 | +        if (!is_string($source)) { | 
|  | 62 | +            throw new JsonObjectsException('Unable to create a stream from the given source.'); | 
|  | 63 | +        } | 
|  | 64 | + | 
|  | 65 | +        $this->stream = extension_loaded('zlib') ? @gzopen($source, 'rb') : @fopen($source, 'rb'); | 
|  | 66 | + | 
|  | 67 | +        if ($this->stream === false) { | 
|  | 68 | +            throw new JsonObjectsException("Failed to open stream from: {$source}"); | 
|  | 69 | +        } | 
|  | 70 | +    } | 
|  | 71 | + | 
|  | 72 | +    /** | 
|  | 73 | +     * Create a new instance while easing method chaining | 
|  | 74 | +     * | 
|  | 75 | +     * @param resource|string $source | 
|  | 76 | +     * @param string|null $key | 
|  | 77 | +     * @return self | 
|  | 78 | +     * | 
|  | 79 | +     * @throws JsonObjectsException | 
|  | 80 | +     */ | 
|  | 81 | +    public static function from($source, string $key = null) : self | 
|  | 82 | +    { | 
|  | 83 | +        return new static($source, $key); | 
|  | 84 | +    } | 
|  | 85 | + | 
|  | 86 | +    /** | 
|  | 87 | +     * Process each JSON object separately | 
|  | 88 | +     * | 
|  | 89 | +     * @param callable $callback | 
|  | 90 | +     * @return void | 
|  | 91 | +     * | 
|  | 92 | +     * @throws JsonObjectsException | 
|  | 93 | +     */ | 
|  | 94 | +    public function each(callable $callback) : void | 
|  | 95 | +    { | 
|  | 96 | +        $this->parseStreamWithListener(new ObjectListener($callback)); | 
|  | 97 | +    } | 
|  | 98 | + | 
|  | 99 | +    /** | 
|  | 100 | +     * Parse the JSON stream with the given listener | 
|  | 101 | +     * | 
|  | 102 | +     * @param AbstractListener $listener | 
|  | 103 | +     * @return void | 
|  | 104 | +     * | 
|  | 105 | +     * @throws JsonObjectsException | 
|  | 106 | +     */ | 
|  | 107 | +    protected function parseStreamWithListener(AbstractListener $listener) : void | 
|  | 108 | +    { | 
|  | 109 | +        if ($this->key !== null) { | 
|  | 110 | +            $listener->setTargetFromKey($this->key); | 
|  | 111 | +        } | 
|  | 112 | + | 
|  | 113 | +        try { | 
|  | 114 | +            (new Parser($this->stream, $listener))->parse(); | 
|  | 115 | +        } catch (Exception $e) { | 
|  | 116 | +            throw new JsonObjectsException($e->getMessage()); | 
|  | 117 | +        } finally { | 
|  | 118 | +            extension_loaded('zlib') ? gzclose($this->stream) : fclose($this->stream); | 
|  | 119 | +        } | 
|  | 120 | +    } | 
|  | 121 | + | 
|  | 122 | +    /** | 
|  | 123 | +     * Process JSON objects in chunks | 
|  | 124 | +     * | 
|  | 125 | +     * @param int $size | 
|  | 126 | +     * @param callable $callback | 
|  | 127 | +     * @return void | 
|  | 128 | +     * | 
|  | 129 | +     * @throws JsonObjectsException | 
|  | 130 | +     */ | 
|  | 131 | +    public function chunk(int $size, callable $callback) : void | 
|  | 132 | +    { | 
|  | 133 | +        $this->parseStreamWithListener(new ChunkListener($size, $callback)); | 
|  | 134 | +    } | 
|  | 135 | +} | 
0 commit comments