| Total Complexity | 7 |
| Total Lines | 31 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 9 | class JsonHandler extends MimeHandlerAdapter |
||
| 10 | { |
||
| 11 | private $decode_as_array = false; |
||
| 12 | |||
| 13 | public function init(array $args) |
||
| 14 | { |
||
| 15 | $this->decode_as_array = !!(array_key_exists('decode_as_array', $args) ? $args['decode_as_array'] : false); |
||
| 16 | } |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @param string $body |
||
| 20 | * @return mixed |
||
| 21 | */ |
||
| 22 | public function parse($body) |
||
| 23 | { |
||
| 24 | $body = $this->stripBom($body); |
||
| 25 | if (empty($body)) |
||
| 26 | return null; |
||
| 27 | $parsed = json_decode($body, $this->decode_as_array); |
||
| 28 | if (is_null($parsed) && 'null' !== strtolower($body)) |
||
| 29 | throw new \Exception("Unable to parse response as JSON"); |
||
| 30 | return $parsed; |
||
| 31 | } |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @param mixed $payload |
||
| 35 | * @return string |
||
| 36 | */ |
||
| 37 | public function serialize($payload) |
||
| 38 | { |
||
| 39 | return json_encode($payload); |
||
| 40 | } |
||
| 42 |