[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/externals/httpful/src/Httpful/Handlers/ -> JsonHandler.php (source)

   1  <?php
   2  /**
   3   * Mime Type: application/json
   4   * @author Nathan Good <[email protected]>
   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       */
  22      public function parse($body)
  23      {
  24          if (empty($body))
  25              return null;
  26          $parsed = json_decode($body, $this->decode_as_array);
  27          if (is_null($parsed))
  28              throw new \Exception("Unable to parse response as JSON");
  29          return $parsed;
  30      }
  31  
  32      /**
  33       * @param mixed $payload
  34       * @return string
  35       */
  36      public function serialize($payload)
  37      {
  38          return json_encode($payload);
  39      }
  40  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1