Completed
Pull Request — master (#214)
by
unknown
06:58 queued 03:47
created

Requests_Response::json()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
cc 5
eloc 16
c 2
b 1
f 1
nc 5
nop 1
dl 0
loc 25
rs 8.439
1
<?php
2
/**
3
 * HTTP response class
4
 *
5
 * Contains a response from Requests::request()
6
 * @package Requests
7
 */
8
9
/**
10
 * HTTP response class
11
 *
12
 * Contains a response from Requests::request()
13
 * @package Requests
14
 */
15
class Requests_Response {
16
	/**
17
	 * Constructor
18
	 */
19
	public function __construct() {
20
		$this->headers = new Requests_Response_Headers();
21
		$this->cookies = new Requests_Cookie_Jar();
22
	}
23
24
	/**
25
	 * Response body
26
	 *
27
	 * @var string
28
	 */
29
	public $body = '';
30
31
	/**
32
	 * Raw HTTP data from the transport
33
	 *
34
	 * @var string
35
	 */
36
	public $raw = '';
37
38
	/**
39
	 * Headers, as an associative array
40
	 *
41
	 * @var Requests_Response_Headers Array-like object representing headers
42
	 */
43
	public $headers = array();
44
45
	/**
46
	 * Status code, false if non-blocking
47
	 *
48
	 * @var integer|boolean
49
	 */
50
	public $status_code = false;
51
52
	/**
53
	 * Protocol version, false if non-blocking
54
	 * @var float|boolean
55
	 */
56
	public $protocol_version = false;
57
58
	/**
59
	 * Whether the request succeeded or not
60
	 *
61
	 * @var boolean
62
	 */
63
	public $success = false;
64
65
	/**
66
	 * Number of redirects the request used
67
	 *
68
	 * @var integer
69
	 */
70
	public $redirects = 0;
71
72
	/**
73
	 * URL requested
74
	 *
75
	 * @var string
76
	 */
77
	public $url = '';
78
79
	/**
80
	 * Previous requests (from redirects)
81
	 *
82
	 * @var array Array of Requests_Response objects
83
	 */
84
	public $history = array();
85
86
	/**
87
	 * Cookies from the request
88
	 *
89
	 * @var Requests_Cookie_Jar Array-like object representing a cookie jar
90
	 */
91
	public $cookies = array();
92
93
	/**
94
	 * Is the response a redirect?
95
	 *
96
	 * @return boolean True if redirect (3xx status), false if not.
97
	 */
98
	public function is_redirect() {
99
		$code = $this->status_code;
100
		return in_array($code, array(300, 301, 302, 303, 307)) || $code > 307 && $code < 400;
101
	}
102
103
	/**
104
	 * Throws an exception if the request was not successful
105
	 *
106
	 * @throws Requests_Exception If `$allow_redirects` is false, and code is 3xx (`response.no_redirects`)
107
	 * @throws Requests_Exception_HTTP On non-successful status code. Exception class corresponds to code (e.g. {@see Requests_Exception_HTTP_404})
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 144 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
108
	 * @param boolean $allow_redirects Set to false to throw on a 3xx as well
109
	 */
110
	public function throw_for_status($allow_redirects = true) {
111
		if ($this->is_redirect()) {
112
			if (!$allow_redirects) {
113
				throw new Requests_Exception('Redirection not allowed', 'response.no_redirects', $this);
114
			}
115
		}
116
		elseif (!$this->success) {
117
			$exception = Requests_Exception_HTTP::get_class($this->status_code);
118
			throw new $exception(null, $this);
119
		}
120
	}
121
122
	/**
123
	 * Returns json decoded response
124
	 *
125
	 * @throws Requests_Exception If `$this->body` is not a valid json
126
	 * @return array
127
	 */
128
	public function json($assoc = true) {
129
		static $json_errors = array(
130
			JSON_ERROR_DEPTH => 'JSON_ERROR_DEPTH - Maximum stack depth exceeded',
131
			JSON_ERROR_STATE_MISMATCH => 'JSON_ERROR_STATE_MISMATCH - Underflow or the modes mismatch',
132
			JSON_ERROR_CTRL_CHAR => 'JSON_ERROR_CTRL_CHAR - Unexpected control character found',
133
			JSON_ERROR_SYNTAX => 'JSON_ERROR_SYNTAX - Syntax error, malformed JSON',
134
			JSON_ERROR_UTF8 => 'JSON_ERROR_UTF8 - Malformed UTF-8 characters, possibly incorrectly encoded'
135
		);
136
137
		$data = json_decode($this->body, $assoc);
138
139
		if (function_exists('json_last_error')) {
140
			if (JSON_ERROR_NONE !== json_last_error()) {
141
				$last_error = json_last_error();
142
				$error = isset($json_errors[$last_error]) ? $json_errors[$last_error] : 'Unknown error';
143
				throw new Requests_Exception('Unable to parse JSON data: ' . $error, 'response.invalid', $this);
144
			}
145
		}
146
		elseif ($data === null) {
147
			throw new Requests_Exception('Unable to parse JSON data.', 'response.invalid', $this);
148
		}
149
150
151
		return $data;
152
	}
153
}
154