1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Cakasim\Payone\Sdk\Api\Message; |
||
6 | |||
7 | /** |
||
8 | * @author Fabian Böttcher <[email protected]> |
||
9 | * @since 0.1.0 |
||
10 | */ |
||
11 | class JsonResponse implements JsonResponseInterface |
||
12 | { |
||
13 | /** |
||
14 | * @var array The JSON response data. |
||
15 | */ |
||
16 | protected $json = []; |
||
17 | |||
18 | /** |
||
19 | * @inheritDoc |
||
20 | */ |
||
21 | public function serialize(): string |
||
22 | { |
||
23 | $encoded = json_encode($this->json); |
||
24 | |||
25 | if (!is_string($encoded)) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
26 | throw new \RuntimeException("Cannot serialize JSON response message, failed encoding of JSON data."); |
||
27 | } |
||
28 | |||
29 | return $encoded; |
||
30 | } |
||
31 | |||
32 | /** |
||
33 | * @inheritDoc |
||
34 | */ |
||
35 | public function unserialize($serialized): void |
||
36 | { |
||
37 | $this->json = json_decode($serialized, true); |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * @inheritDoc |
||
42 | */ |
||
43 | public function parseResponseData($data): void |
||
44 | { |
||
45 | if (!is_string($data)) { |
||
46 | throw new \RuntimeException(sprintf("Cannot parse response data of type '%s', expected a string.", gettype($data))); |
||
47 | } |
||
48 | |||
49 | $json = json_decode($data, true); |
||
50 | |||
51 | if (!is_array($json)) { |
||
52 | throw new \RuntimeException(sprintf("Failed decoding of JSON response data: %s", json_last_error_msg())); |
||
53 | } |
||
54 | |||
55 | $this->json = $json; |
||
56 | } |
||
57 | } |
||
58 |