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