Overview

Namespaces

  • OpenCloud
    • Autoscale
      • Resource
    • CloudMonitoring
      • Exception
      • Resource
    • Common
      • Collection
      • Constants
      • Exceptions
      • Http
        • Message
      • Identity
      • Log
      • Service
    • Compute
      • Constants
      • Exception
      • Resource
    • Database
      • Resource
    • DNS
      • Resource
    • LoadBalancer
      • Resource
    • ObjectStore
      • Constants
      • Exception
      • Resource
      • Upload
    • Orchestration
    • Queues
      • Exception
      • Resource
    • Volume
      • Resource
  • PHP

Classes

  • Formatter
  • RequestSubscriber
  • Overview
  • Namespace
  • Class
  • Tree
  • Download
 1: <?php
 2: /**
 3:  * PHP OpenCloud library.
 4:  * 
 5:  * @copyright 2013 Rackspace Hosting, Inc. See LICENSE for information.
 6:  * @license   https://www.apache.org/licenses/LICENSE-2.0
 7:  * @author    Jamie Hannaford <jamie.hannaford@rackspace.com>
 8:  */
 9: 
10: namespace OpenCloud\Common\Http\Message;
11: 
12: use Guzzle\Http\Message\Response;
13: use OpenCloud\Common\Constants\Header;
14: use OpenCloud\Common\Constants\Mime;
15: use OpenCloud\Common\Exceptions\JsonError;
16: 
17: class Formatter
18: {
19: 
20:     public static function decode(Response $response)
21:     {
22:         if (strpos($response->getHeader(Header::CONTENT_TYPE), Mime::JSON) !== false) {
23:             $string   = (string) $response->getBody();
24:             $response = json_decode($string);
25:             self::checkJsonError($string);
26:             return $response;
27:         }
28:     }
29: 
30:     public static function encode($body)
31:     {
32:         return json_encode($body);
33:     }
34: 
35:     public static function checkJsonError($string = null)
36:     {
37:         if (json_last_error()) {
38:             $error   = sprintf('%s', json_last_error_msg());
39:             $message = ($string) ? sprintf('%s trying to decode: %s', $error, $string) : $error;
40:             throw new JsonError($message);
41:         }
42:     }
43: 
44: }
PHP OpenCloud API API documentation generated by ApiGen 2.8.0