1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* php-guard/curl <https://github.com/php-guard/curl> |
4
|
|
|
* Copyright (C) ${YEAR} by Alexandre Le Borgne <[email protected]> |
5
|
|
|
* |
6
|
|
|
* This program is free software: you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU General Public License as published by |
8
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
* (at your option) any later version. |
10
|
|
|
* |
11
|
|
|
* This program is distributed in the hope that it will be useful, |
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
* GNU General Public License for more details. |
15
|
|
|
* |
16
|
|
|
* You should have received a copy of the GNU General Public License |
17
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace PhpGuard\Curl; |
21
|
|
|
|
22
|
|
|
class CurlResponse |
23
|
|
|
{ |
24
|
|
|
const JSON_PATTERN = '/^(?:application|text)\/(?:[a-z]+(?:[\.-][0-9a-z]+){0,}[\+\.]|x-)?json(?:-[a-z]+)?/i'; |
25
|
|
|
const XML_PATTERN = '~^(?:text/|application/(?:atom\+|rss\+)?)xml~i'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $rawResponse; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
private $headers; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var int |
39
|
|
|
*/ |
40
|
|
|
private $statusCode; |
41
|
|
|
/** |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
private $info; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* CurlResponse constructor. |
48
|
|
|
* |
49
|
|
|
* @param int $statusCode |
50
|
|
|
* @param string $rawResponse |
51
|
|
|
* @param array $headers |
52
|
|
|
* @param array $info |
53
|
|
|
*/ |
54
|
13 |
|
public function __construct(int $statusCode, string $rawResponse, array $headers, array $info) |
55
|
|
|
{ |
56
|
13 |
|
$this->statusCode = $statusCode; |
57
|
13 |
|
$this->rawResponse = $rawResponse; |
58
|
13 |
|
$this->headers = new Headers($headers); |
|
|
|
|
59
|
13 |
|
$this->info = $info; |
60
|
13 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return int |
64
|
|
|
*/ |
65
|
13 |
|
public function statusCode(): int |
66
|
|
|
{ |
67
|
13 |
|
return $this->statusCode; |
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
public function isError(): bool |
71
|
|
|
{ |
72
|
1 |
|
return $this->statusCode >= 300; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return Headers |
77
|
|
|
*/ |
78
|
|
|
public function headers(): Headers |
79
|
|
|
{ |
80
|
|
|
return $this->headers; |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
2 |
|
public function raw() |
84
|
|
|
{ |
85
|
2 |
|
return $this->rawResponse; |
86
|
|
|
} |
87
|
|
|
|
88
|
12 |
|
public function json() |
89
|
|
|
{ |
90
|
12 |
|
if (!preg_match(self::JSON_PATTERN, $this->headers['Content-Type'])) { |
91
|
1 |
|
return false; |
92
|
|
|
} |
93
|
|
|
|
94
|
11 |
|
return json_decode($this->rawResponse, true); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..