Skip to content

Commit ad528d6

Browse files
committed
Made JSONStream a bit more PSR-7 stream compliant
1 parent f5b6528 commit ad528d6

File tree

2 files changed

+23
-53
lines changed

2 files changed

+23
-53
lines changed

src/JsonStream.php

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace ApiClients\Middleware\Json;
44

55
use Psr\Http\Message\StreamInterface;
6-
use RuntimeException;
6+
use RingCentral\Psr7\BufferStream;
77

88
class JsonStream implements StreamInterface
99
{
@@ -12,9 +12,17 @@ class JsonStream implements StreamInterface
1212
*/
1313
private $json = [];
1414

15+
/**
16+
* @var StreamInterface
17+
*/
18+
private $bufferStream;
19+
1520
public function __construct(array $json)
1621
{
1722
$this->json = $json;
23+
$jsonString = json_encode($json);
24+
$this->bufferStream = new BufferStream(strlen($jsonString));
25+
$this->bufferStream->write($jsonString);
1826
}
1927

2028
/**
@@ -32,7 +40,7 @@ public function __toString()
3240

3341
public function getContents()
3442
{
35-
return '';
43+
return $this->bufferStream->getContents();
3644
}
3745

3846
public function close()
@@ -45,61 +53,62 @@ public function detach()
4553

4654
public function getSize()
4755
{
48-
return count($this->json);
56+
return $this->bufferStream->getSize();
4957
}
5058

5159
public function isReadable()
5260
{
53-
return true;
61+
return $this->bufferStream->isReadable();
5462
}
5563

5664
public function isWritable()
5765
{
58-
return false;
66+
return $this->bufferStream->isWritable();
5967
}
6068

6169
public function isSeekable()
6270
{
63-
return false;
71+
return $this->bufferStream->isSeekable();
6472
}
6573

6674
public function rewind()
6775
{
76+
return $this->bufferStream->rewind();
6877
}
6978

7079
public function seek($offset, $whence = SEEK_SET)
7180
{
72-
throw new RuntimeException('Cannot seek a BufferStream');
81+
return $this->bufferStream->seek($offset, $whence);
7382
}
7483

7584
public function eof()
7685
{
77-
return true;
86+
return $this->bufferStream->eof();
7887
}
7988

8089
public function tell()
8190
{
82-
throw new RuntimeException('Cannot determine the position of a BufferStream');
91+
return $this->bufferStream->tell();
8392
}
8493

8594
/**
8695
* Reads data from the buffer.
8796
*/
8897
public function read($length)
8998
{
90-
throw new RuntimeException('Cannot read from a JsonStream');
99+
return $this->bufferStream->read($length);
91100
}
92101

93102
/**
94103
* Writes data to the buffer.
95104
*/
96105
public function write($string)
97106
{
98-
throw new RuntimeException('Cannot write to a JsonStream');
107+
return $this->bufferStream->write($string);
99108
}
100109

101110
public function getMetadata($key = null)
102111
{
103-
return [];
112+
return $this->bufferStream->getMetadata($key);
104113
}
105114
}

tests/JsonStreamTest.php

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,53 +5,14 @@
55

66
use ApiClients\Middleware\Json\JsonStream;
77
use ApiClients\Tools\TestUtilities\TestCase;
8-
use RuntimeException;
98

109
class JsonStreamTest extends TestCase
1110
{
1211
public function testBasics()
1312
{
1413
$stream = new JsonStream([]);
1514
self::assertSame([], $stream->getJson());
16-
self::assertSame('', (string)$stream);
17-
self::assertSame('', $stream->getContents());
18-
self::assertSame(0, $stream->getSize());
19-
self::assertSame(true, $stream->isReadable());
20-
self::assertSame(false, $stream->isWritable());
21-
self::assertSame(false, $stream->isSeekable());
22-
self::assertSame(true, $stream->eof());
23-
self::assertSame([], $stream->getMetadata());
24-
25-
$stream->close();
26-
$stream->detach();
27-
$stream->rewind();
28-
}
29-
30-
public function testSeek()
31-
{
32-
$this->expectException(RuntimeException::class);
33-
$this->expectExceptionMessage('Cannot seek a BufferStream');
34-
(new JsonStream([]))->seek(0);
35-
}
36-
37-
public function testTell()
38-
{
39-
$this->expectException(RuntimeException::class);
40-
$this->expectExceptionMessage('Cannot determine the position of a BufferStream');
41-
(new JsonStream([]))->tell();
42-
}
43-
44-
public function testRead()
45-
{
46-
$this->expectException(RuntimeException::class);
47-
$this->expectExceptionMessage('Cannot read from a JsonStream');
48-
(new JsonStream([]))->read(0);
49-
}
50-
51-
public function testWrite()
52-
{
53-
$this->expectException(RuntimeException::class);
54-
$this->expectExceptionMessage('Cannot write to a JsonStream');
55-
(new JsonStream([]))->write('');
15+
self::assertSame(2, $stream->getSize());
16+
self::assertSame('[]', $stream->getContents());
5617
}
5718
}

0 commit comments

Comments
 (0)