1 | <?php |
||
2 | |||
3 | namespace CybozuHttp\Middleware; |
||
4 | |||
5 | use GuzzleHttp\Psr7\StreamDecoratorTrait; |
||
6 | use Psr\Http\Message\StreamInterface; |
||
7 | |||
8 | /** |
||
9 | * @author ochi51 <[email protected]> |
||
10 | */ |
||
11 | class JsonStream implements StreamInterface, \JsonSerializable |
||
12 | { |
||
13 | use StreamDecoratorTrait; |
||
14 | |||
15 | private $stream = null; |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
16 | |||
17 | // TODO: Specify return type as mixed when PHP 7.x support is dropped |
||
18 | 61 | #[\ReturnTypeWillChange] |
|
19 | public function jsonSerialize() |
||
20 | { |
||
21 | 61 | $contents = (string) $this->getContents(); |
|
22 | 61 | if ($contents === '') { |
|
23 | 1 | return null; |
|
24 | } |
||
25 | 60 | $decodedContents = json_decode($contents, true); |
|
26 | 60 | if (json_last_error() !== JSON_ERROR_NONE) { |
|
27 | 1 | throw new \RuntimeException( |
|
28 | 1 | 'Error trying to decode response: ' . |
|
29 | 1 | json_last_error_msg() |
|
30 | 1 | ); |
|
31 | } |
||
32 | |||
33 | 59 | return $decodedContents; |
|
34 | } |
||
35 | } |