Overview

Namespaces

  • Httpful
    • Exception
    • Handlers
    • Response
  • PHP

Classes

  • Closure
  • XMLWriter

Interfaces

  • ArrayAccess
  • Countable
  • Exception
  • Overview
  • Namespace
  • Class
 1: <?php
 2: /**
 3:  * Mime Type: application/json
 4:  * @author Nathan Good <me@nategood.com>
 5:  */
 6: 
 7: namespace Httpful\Handlers;
 8: 
 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:      * @throws \Exception
22:      */
23:     public function parse($body)
24:     {
25:         $body = $this->stripBom($body);
26:         if (empty($body))
27:             return null;
28:         $parsed = json_decode($body, $this->decode_as_array);
29:         if (is_null($parsed) && 'null' !== strtolower($body))
30:             throw new \Exception("Unable to parse response as JSON");
31:         return $parsed;
32:     }
33: 
34:     /**
35:      * @param mixed $payload
36:      * @return string
37:      */
38:     public function serialize($payload)
39:     {
40:         return json_encode($payload);
41:     }
42: }
43: 
API documentation generated by ApiGen