| Total Complexity | 6 |
| Total Lines | 36 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 7 | class JsonSerializer implements SerializerInterface |
||
| 8 | { |
||
| 9 | protected int $flags; |
||
| 10 | protected int $depth; |
||
| 11 | |||
| 12 | public function __construct(int $flags = 0, int $depth = 512) |
||
| 13 | { |
||
| 14 | $this->flags = $flags; |
||
| 15 | $this->depth = $depth; |
||
| 16 | } |
||
| 17 | |||
| 18 | public function serialize(array $data): string |
||
| 19 | { |
||
| 20 | $data = json_encode($data, $this->flags, $this->depth); |
||
| 21 | |||
| 22 | if (json_last_error() != JSON_ERROR_NONE) { |
||
| 23 | throw new ParseException("Serialization data is not JSON parseable."); |
||
| 24 | } |
||
| 25 | |||
| 26 | return $data; |
||
| 27 | } |
||
| 28 | |||
| 29 | public function unserialize(string $data): array |
||
| 30 | { |
||
| 31 | $data = json_decode($data, true, $this->depth, $this->flags); |
||
| 32 | |||
| 33 | if (json_last_error() != JSON_ERROR_NONE) { |
||
| 34 | throw new ParseException("Received data is not JSON parseable: $data."); |
||
| 35 | } |
||
| 36 | |||
| 37 | return $data; |
||
| 38 | } |
||
| 39 | |||
| 40 | public function getContentType(): string |
||
| 43 | } |
||
| 44 | } |
||
| 45 |