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