1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ApiClients\Middleware\Json; |
4
|
|
|
|
5
|
|
|
use Psr\Http\Message\StreamInterface; |
6
|
|
|
use RingCentral\Psr7\BufferStream; |
7
|
|
|
|
8
|
|
|
class JsonStream implements StreamInterface |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var array |
12
|
|
|
*/ |
13
|
|
|
private $json = []; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var StreamInterface |
17
|
|
|
*/ |
18
|
|
|
private $bufferStream; |
19
|
|
|
|
20
|
3 |
|
public function __construct(array $json) |
21
|
|
|
{ |
22
|
3 |
|
$this->json = $json; |
23
|
3 |
|
$jsonString = json_encode($json); |
24
|
3 |
|
$this->bufferStream = new BufferStream(strlen($jsonString)); |
25
|
3 |
|
$this->bufferStream->write($jsonString); |
26
|
3 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return array |
30
|
|
|
*/ |
31
|
3 |
|
public function getJson() |
32
|
|
|
{ |
33
|
3 |
|
return $this->json; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function __toString() |
37
|
|
|
{ |
38
|
|
|
return $this->getContents(); |
39
|
|
|
} |
40
|
|
|
|
41
|
1 |
|
public function getContents() |
42
|
|
|
{ |
43
|
1 |
|
return $this->bufferStream->getContents(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function close() |
47
|
|
|
{ |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function detach() |
51
|
|
|
{ |
52
|
|
|
} |
53
|
|
|
|
54
|
1 |
|
public function getSize() |
55
|
|
|
{ |
56
|
1 |
|
return $this->bufferStream->getSize(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function isReadable() |
60
|
|
|
{ |
61
|
|
|
return $this->bufferStream->isReadable(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function isWritable() |
65
|
|
|
{ |
66
|
|
|
return $this->bufferStream->isWritable(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function isSeekable() |
70
|
|
|
{ |
71
|
|
|
return $this->bufferStream->isSeekable(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function rewind() |
75
|
|
|
{ |
76
|
|
|
return $this->bufferStream->rewind(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function seek($offset, $whence = SEEK_SET) |
80
|
|
|
{ |
81
|
|
|
return $this->bufferStream->seek($offset, $whence); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function eof() |
85
|
|
|
{ |
86
|
|
|
return $this->bufferStream->eof(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function tell() |
90
|
|
|
{ |
91
|
|
|
return $this->bufferStream->tell(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Reads data from the buffer. |
96
|
|
|
*/ |
97
|
|
|
public function read($length) |
98
|
|
|
{ |
99
|
|
|
return $this->bufferStream->read($length); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Writes data to the buffer. |
104
|
|
|
*/ |
105
|
|
|
public function write($string) |
106
|
|
|
{ |
107
|
|
|
return $this->bufferStream->write($string); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function getMetadata($key = null) |
111
|
|
|
{ |
112
|
|
|
return $this->bufferStream->getMetadata($key); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|