1: <?php
2: 3: 4: 5: 6: 7: 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: }