|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Sandro Keil (https://sandro-keil.de) |
|
4
|
|
|
* |
|
5
|
|
|
* @link http://github.com/sandrokeil/arangodb-php-client for the canonical source repository |
|
6
|
|
|
* @copyright Copyright (c) 2018-2019 Sandro Keil |
|
7
|
|
|
* @license http://github.com/sandrokeil/arangodb-php-client/blob/master/LICENSE.md New BSD License |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace ArangoDb\Http; |
|
11
|
|
|
|
|
12
|
|
|
use ArangoDb\Exception\RuntimeException; |
|
13
|
|
|
use ArangoDb\Util\Json; |
|
14
|
|
|
use Psr\Http\Message\StreamInterface; |
|
15
|
|
|
|
|
16
|
|
|
final class JsonStream implements StreamInterface |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Original data |
|
20
|
|
|
* |
|
21
|
|
|
* @var string|array |
|
22
|
|
|
*/ |
|
23
|
|
|
private $data; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Buffered json data if needed |
|
27
|
|
|
* |
|
28
|
|
|
* @var string|null |
|
29
|
|
|
*/ |
|
30
|
|
|
private $buffer; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var int |
|
34
|
|
|
*/ |
|
35
|
|
|
private $size; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param string|array $data |
|
39
|
|
|
*/ |
|
40
|
42 |
|
public function __construct($data) |
|
41
|
|
|
{ |
|
42
|
42 |
|
$this->data = $data; |
|
43
|
42 |
|
} |
|
44
|
|
|
|
|
45
|
25 |
|
public function toJson(): string |
|
46
|
|
|
{ |
|
47
|
25 |
|
if (! is_string($this->data)) { |
|
48
|
1 |
|
return Json::encode($this->data); |
|
49
|
|
|
} |
|
50
|
24 |
|
return $this->data; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
16 |
|
public function toArray(): array |
|
54
|
|
|
{ |
|
55
|
16 |
|
if (is_string($this->data)) { |
|
56
|
7 |
|
return Json::decode($this->data); |
|
57
|
|
|
} |
|
58
|
9 |
|
return $this->data; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
2 |
|
public function __toString() |
|
62
|
|
|
{ |
|
63
|
2 |
|
return $this->getContents(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function close(): void |
|
67
|
|
|
{ |
|
68
|
|
|
$this->data = ''; |
|
69
|
|
|
$this->buffer = null; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function detach() |
|
73
|
|
|
{ |
|
74
|
|
|
$this->close(); |
|
75
|
|
|
return null; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
2 |
|
public function getSize(): int |
|
79
|
|
|
{ |
|
80
|
2 |
|
if ($this->size === null) { |
|
81
|
2 |
|
$this->size = strlen($this->getContents()); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
2 |
|
return $this->size; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function tell(): int |
|
88
|
|
|
{ |
|
89
|
|
|
throw new RuntimeException('Cannot determine the position of a JsonStream'); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function eof(): bool |
|
93
|
|
|
{ |
|
94
|
|
|
if ($this->buffer === null) { |
|
95
|
|
|
return false; |
|
96
|
|
|
} |
|
97
|
|
|
return $this->buffer === ''; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function isSeekable(): bool |
|
101
|
|
|
{ |
|
102
|
|
|
return false; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param int $offset |
|
107
|
|
|
* @param int $whence |
|
108
|
|
|
*/ |
|
109
|
|
|
public function seek($offset, $whence = SEEK_SET): void |
|
110
|
|
|
{ |
|
111
|
|
|
throw new RuntimeException('Cannot seek a JsonStream'); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
2 |
|
public function rewind(): void |
|
115
|
|
|
{ |
|
116
|
2 |
|
$this->buffer = null; |
|
117
|
2 |
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function isWritable(): bool |
|
120
|
|
|
{ |
|
121
|
|
|
return false; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
public function write($string): int |
|
125
|
|
|
{ |
|
126
|
|
|
throw new RuntimeException('Cannot write a JsonStream'); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
public function isReadable(): bool |
|
130
|
|
|
{ |
|
131
|
|
|
return true; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @param int $length |
|
136
|
|
|
* @return string |
|
137
|
|
|
*/ |
|
138
|
2 |
|
public function read($length): string |
|
139
|
|
|
{ |
|
140
|
2 |
|
if ($this->buffer === null) { |
|
141
|
2 |
|
$this->buffer = $this->getContents(); |
|
142
|
|
|
} |
|
143
|
2 |
|
$currentLength = strlen($this->buffer); |
|
144
|
|
|
|
|
145
|
2 |
|
if ($length >= $currentLength) { |
|
146
|
|
|
// No need to slice the data because we don't have enough data. |
|
147
|
2 |
|
$result = $this->buffer; |
|
148
|
2 |
|
$this->buffer = ''; |
|
149
|
|
|
} else { |
|
150
|
|
|
// Slice up the result to provide a subset of the data. |
|
151
|
2 |
|
$result = substr($this->buffer, 0, $length); |
|
152
|
2 |
|
$this->buffer = substr($this->buffer, $length); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
2 |
|
return $result; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
33 |
|
public function getContents(): string |
|
159
|
|
|
{ |
|
160
|
33 |
|
if (is_string($this->data)) { |
|
161
|
15 |
|
return $this->data; |
|
162
|
|
|
} |
|
163
|
31 |
|
return Json::encode($this->data); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @param string|null $key |
|
168
|
|
|
* @return array|mixed|null |
|
169
|
|
|
*/ |
|
170
|
|
|
public function getMetadata($key = null) |
|
171
|
|
|
{ |
|
172
|
|
|
$metadata = [ |
|
173
|
|
|
'wrapper_data' => ['string'], |
|
174
|
|
|
'wrapper_type' => 'string', |
|
175
|
|
|
'stream_type' => 'string', |
|
176
|
|
|
'mode' => 'r', |
|
177
|
|
|
'unread_bytes' => $this->getSize() - strlen($this->buffer ?? ''), |
|
178
|
|
|
'seekable' => $this->isSeekable(), |
|
179
|
|
|
'timeout' => false, |
|
180
|
|
|
'blocked' => false, |
|
181
|
|
|
'eof' => $this->eof() |
|
182
|
|
|
]; |
|
183
|
|
|
|
|
184
|
|
|
if ($key === null) { |
|
185
|
|
|
return $metadata; |
|
186
|
|
|
} |
|
187
|
|
|
return $metadata[$key] ?? null; |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
|