|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Phraseanet SDK. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Alchemy <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace PhraseanetSDK\Http; |
|
13
|
|
|
|
|
14
|
|
|
use PhraseanetSDK\Exception\InvalidArgumentException; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Response object from a Phraseanet API call |
|
18
|
|
|
*/ |
|
19
|
|
|
class ApiResponse |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var \stdClass |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $result; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var \stdClass |
|
28
|
|
|
*/ |
|
29
|
|
|
private $meta; |
|
30
|
71 |
|
|
|
31
|
|
|
/** |
|
32
|
71 |
|
* @param \stdClass $response |
|
33
|
1 |
|
*/ |
|
34
|
|
|
public function __construct(\stdClass $response) |
|
35
|
|
|
{ |
|
36
|
70 |
|
if (!isset($response->meta) || !isset($response->response)) { |
|
37
|
70 |
|
throw new InvalidArgumentException('Malformed API response'); |
|
38
|
70 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
$this->meta = $response->meta; |
|
41
|
|
|
$this->result = $response->response; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
16 |
|
* Returns the result of the response |
|
46
|
|
|
* |
|
47
|
16 |
|
* @return \stdClass |
|
48
|
|
|
*/ |
|
49
|
|
|
public function getResult() |
|
50
|
|
|
{ |
|
51
|
|
|
return $this->result; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
2 |
|
* Returns the HTTP code |
|
56
|
|
|
* |
|
57
|
2 |
|
* @return integer |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getStatusCode() |
|
60
|
|
|
{ |
|
61
|
|
|
return (int)$this->meta->http_code; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
1 |
|
* Returns true is the response is successful |
|
66
|
|
|
* |
|
67
|
1 |
|
* @return Boolean |
|
68
|
|
|
*/ |
|
69
|
|
|
public function isOk() |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->getStatusCode() < 400; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
4 |
|
* Returns true if the response content is empty |
|
76
|
|
|
* |
|
77
|
4 |
|
* @return Boolean |
|
78
|
|
|
*/ |
|
79
|
|
|
public function isEmpty() |
|
80
|
|
|
{ |
|
81
|
|
|
return count(get_object_vars($this->result)) === 0; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
1 |
|
* Returns the error message |
|
86
|
|
|
* |
|
87
|
1 |
|
* @return string|null |
|
88
|
|
|
*/ |
|
89
|
|
|
public function getErrorMessage() |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->meta->error_message; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
1 |
|
* Returns error details |
|
96
|
|
|
* |
|
97
|
1 |
|
* @return string|null |
|
98
|
|
|
*/ |
|
99
|
|
|
public function getErrorDetails() |
|
100
|
|
|
{ |
|
101
|
|
|
return $this->meta->error_details; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
1 |
|
* Returns the response datetime |
|
106
|
|
|
* |
|
107
|
1 |
|
* @return \DateTime |
|
108
|
|
|
*/ |
|
109
|
|
|
public function getResponseTime() |
|
110
|
|
|
{ |
|
111
|
|
|
return \DateTime::createFromFormat(DATE_ATOM, $this->meta->response_time); |
|
|
|
|
|
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
1 |
|
* Returns the requested URI |
|
116
|
|
|
* |
|
117
|
1 |
|
* @return string |
|
118
|
|
|
*/ |
|
119
|
1 |
|
public function getUri() |
|
120
|
|
|
{ |
|
121
|
|
|
$request = explode(' ', $this->meta->request); |
|
122
|
|
|
|
|
123
|
|
|
return $request[1]; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
1 |
|
* Returns the requested method |
|
128
|
|
|
* |
|
129
|
1 |
|
* @return string |
|
130
|
|
|
*/ |
|
131
|
1 |
|
public function getMethod() |
|
132
|
|
|
{ |
|
133
|
|
|
$request = explode(' ', $this->meta->request); |
|
134
|
|
|
|
|
135
|
|
|
return $request[0]; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
1 |
|
* Returns the response charset |
|
140
|
|
|
* |
|
141
|
1 |
|
* @return string |
|
142
|
|
|
*/ |
|
143
|
|
|
public function getCharset() |
|
144
|
|
|
{ |
|
145
|
|
|
return $this->meta->charset; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
1 |
|
* Returns the API version |
|
150
|
|
|
* |
|
151
|
1 |
|
* @return string |
|
152
|
|
|
*/ |
|
153
|
|
|
public function getApiVersion() |
|
154
|
|
|
{ |
|
155
|
|
|
return $this->meta->api_version; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Returns true if the response has the given property |
|
160
|
|
|
* |
|
161
|
49 |
|
* @param string $property The property name |
|
162
|
|
|
* |
|
163
|
49 |
|
* @return Boolean |
|
164
|
|
|
*/ |
|
165
|
|
|
public function hasProperty($property) |
|
166
|
|
|
{ |
|
167
|
|
|
return property_exists($this->result, $property); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Returns the response property, null if the property does not exist |
|
172
|
|
|
* |
|
173
|
28 |
|
* @param string $property The property name |
|
174
|
|
|
* |
|
175
|
28 |
|
* @return \stdClass|\stdClass[]|null |
|
176
|
|
|
*/ |
|
177
|
|
|
public function getProperty($property) |
|
178
|
|
|
{ |
|
179
|
|
|
return $this->hasProperty($property) ? $this->result->{$property} : null; |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|