|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Violet\StreamingJsonEncoder; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Encodes value into JSON and directly echoes it or passes it to a stream. |
|
7
|
|
|
* |
|
8
|
|
|
* @author Riikka Kalliomäki <[email protected]> |
|
9
|
|
|
* @copyright Copyright (c) 2016-2020 Riikka Kalliomäki |
|
10
|
|
|
* @license http://opensource.org/licenses/mit-license.php MIT License |
|
11
|
|
|
*/ |
|
12
|
|
|
class StreamJsonEncoder extends AbstractJsonEncoder |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var callable|null The stream callable */ |
|
15
|
|
|
private $stream; |
|
16
|
|
|
|
|
17
|
|
|
/** @var int Number of bytes written in the current step */ |
|
18
|
|
|
private $bytes; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* StreamJsonEncoder constructor. |
|
22
|
|
|
* |
|
23
|
|
|
* If a callable is given as the second argument, the callable will be |
|
24
|
|
|
* called with two arguments. The first argument is the JSON string to |
|
25
|
|
|
* output and the second argument is the type of the token being outputted. |
|
26
|
|
|
* |
|
27
|
|
|
* If no second parameter is passed to the constructor, then the encoder |
|
28
|
|
|
* will simply output the json using an echo statement. |
|
29
|
|
|
* |
|
30
|
|
|
* @param mixed $value The value to encode as JSON |
|
31
|
|
|
* @param callable|null $stream An optional stream to pass the output or null to echo it |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct($value, callable $stream = null) |
|
34
|
|
|
{ |
|
35
|
|
|
parent::__construct($value); |
|
36
|
|
|
|
|
37
|
|
|
$this->stream = $stream; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Encodes the entire value into JSON and returns the number bytes. |
|
42
|
|
|
* @return int Returned the number of bytes outputted |
|
43
|
|
|
*/ |
|
44
|
|
|
public function encode() |
|
45
|
|
|
{ |
|
46
|
|
|
$total = 0; |
|
47
|
|
|
|
|
48
|
|
|
foreach ($this as $bytes) { |
|
49
|
|
|
$total += $bytes; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return $total; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** {@inheritdoc} */ |
|
56
|
|
|
public function rewind() |
|
57
|
|
|
{ |
|
58
|
|
|
$this->bytes = 0; |
|
59
|
|
|
|
|
60
|
|
|
parent::rewind(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** {@inheritdoc} */ |
|
64
|
|
|
public function next() |
|
65
|
|
|
{ |
|
66
|
|
|
$this->bytes = 0; |
|
67
|
|
|
|
|
68
|
|
|
parent::next(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Returns the bytes written in the last step or null if the encoder is not in valid state. |
|
73
|
|
|
* @return int|null The number of bytes written or null when invalid |
|
74
|
|
|
*/ |
|
75
|
|
|
public function current() |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->valid() ? $this->bytes : null; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Echoes to given string or passes it to the stream callback. |
|
82
|
|
|
* @param string $string The string to output |
|
83
|
|
|
* @param int $token The type of the string |
|
84
|
|
|
*/ |
|
85
|
|
|
protected function write($string, $token) |
|
86
|
|
|
{ |
|
87
|
|
|
if ($this->stream === null) { |
|
88
|
|
|
echo $string; |
|
89
|
|
|
} else { |
|
90
|
|
|
$callback = $this->stream; |
|
91
|
|
|
$callback($string, $token); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$this->bytes += strlen($string); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|