1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the PHP SDK library for the Superdesk Content API. |
5
|
|
|
* |
6
|
|
|
* Copyright 2015 Sourcefabric z.u. and contributors. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please see the |
9
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @copyright 2015 Sourcefabric z.ú. |
12
|
|
|
* @license http://www.superdesk.org/license |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Superdesk\ContentApiSdk\Api; |
16
|
|
|
|
17
|
|
|
use Superdesk\ContentApiSdk\Api\Request\RequestInterface; |
18
|
|
|
use Superdesk\ContentApiSdk\Api\Request\RequestParameters; |
19
|
|
|
use Superdesk\ContentApiSdk\ContentApiSdk; |
20
|
|
|
use Superdesk\ContentApiSdk\Exception\ResponseException; |
21
|
|
|
use Superdesk\ContentApiSdk\Exception\RequestException; |
22
|
|
|
use Superdesk\ContentApiSdk\Exception\InvalidArgumentException; |
23
|
|
|
use Superdesk\ContentApiSdk\Exception\InvalidDataException; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* API Request object. |
27
|
|
|
*/ |
28
|
|
|
class Request implements RequestInterface |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* Protocol for api request. |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $protocol = 'https'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Hostname for api request. |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $host; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Api port. |
46
|
|
|
* |
47
|
|
|
* @var int |
48
|
|
|
*/ |
49
|
|
|
protected $port = 80; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Request uri. |
53
|
|
|
* |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
protected $uri; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Parameters for request. |
60
|
|
|
* |
61
|
|
|
* @var RequestParameters |
62
|
|
|
*/ |
63
|
|
|
protected $parameters; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* A list of parameters the Content API accepts. |
67
|
|
|
* |
68
|
|
|
* For a list of accepted parameters find the variable allowed_params in |
69
|
|
|
* the following file: |
70
|
|
|
* https://github.com/superdesk/superdesk-content-api/blob/master/content_api/items/service.py |
71
|
|
|
* |
72
|
|
|
* @var string[] |
73
|
|
|
*/ |
74
|
|
|
protected $validParameters = array( |
75
|
|
|
'start_date', 'end_date', 'q', 'max_results', 'page', |
76
|
|
|
'include_fields', 'exclude_fields' |
77
|
|
|
); |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Request headers. |
81
|
|
|
* |
82
|
|
|
* @var string[] |
83
|
|
|
*/ |
84
|
|
|
protected $headers = array(); |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Request options, usuably in clients, etc. |
88
|
|
|
* |
89
|
|
|
* @var mixed[] |
90
|
|
|
*/ |
91
|
|
|
protected $options = array(); |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Construct a request object. |
95
|
|
|
* |
96
|
|
|
* @param string $hostname Host name |
97
|
|
|
* @param string $uri Request uri |
98
|
|
|
* @param RequestParameters $parameters Parameters |
99
|
|
|
* @param int $port Port |
100
|
|
|
* @param string $protocol Protocol |
101
|
|
|
*/ |
102
|
|
|
public function __construct( |
103
|
|
|
$hostname = null, |
104
|
|
|
$uri = null, |
105
|
|
|
RequestParameters $parameters = null, |
106
|
|
|
$port = null, |
107
|
|
|
$protocol = null |
108
|
|
|
) { |
109
|
|
|
if (is_string($hostname) && !empty($hostname)) { |
110
|
|
|
$this->setHost($hostname); |
111
|
|
|
} |
112
|
|
|
if (is_string($uri) && !empty($uri)) { |
113
|
|
|
$this->setUri($uri); |
114
|
|
|
} |
115
|
|
|
if ($parameters !== null) { |
116
|
|
|
$this->setParameters($parameters); |
117
|
|
|
} else { |
118
|
|
|
$this->setParameters(new RequestParameters()); |
119
|
|
|
} |
120
|
|
|
if (is_int($port)) { |
121
|
|
|
$this->setPort($port); |
122
|
|
|
} |
123
|
|
|
if (is_string($protocol) && !empty($protocol)) { |
124
|
|
|
$this->setProtocol($protocol); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* {@inheritdoc} |
130
|
|
|
*/ |
131
|
|
|
public function getProtocol() |
132
|
|
|
{ |
133
|
|
|
return $this->protocol; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* {@inheritdoc} |
138
|
|
|
*/ |
139
|
|
|
public function setProtocol($protocol) |
140
|
|
|
{ |
141
|
|
|
if ($protocol !== 'http' && $protocol !== 'https') { |
142
|
|
|
throw new InvalidArgumentException('Property protocol can only have the values "http" or "https".'); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$this->protocol = $protocol; |
146
|
|
|
|
147
|
|
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* {@inheritdoc} |
152
|
|
|
*/ |
153
|
|
|
public function getHost() |
154
|
|
|
{ |
155
|
|
|
return $this->host; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* {@inheritdoc} |
160
|
|
|
*/ |
161
|
|
|
public function setHost($host) |
162
|
|
|
{ |
163
|
|
|
if ($host === null || !is_string($host) || empty($host)) { |
164
|
|
|
throw new InvalidArgumentException('Property host should be of type string and cannot be empty.'); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
$this->host = $host; |
168
|
|
|
|
169
|
|
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* {@inheritdoc} |
174
|
|
|
*/ |
175
|
|
|
public function getPort() |
176
|
|
|
{ |
177
|
|
|
return $this->port; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* {@inheritdoc} |
182
|
|
|
*/ |
183
|
|
|
public function setPort($port) |
184
|
|
|
{ |
185
|
|
|
if (!is_int($port)) { |
186
|
|
|
throw new InvalidArgumentException('Property port should be of type integer.'); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
$this->port = $port; |
190
|
|
|
|
191
|
|
|
return $this; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* {@inheritdoc} |
196
|
|
|
*/ |
197
|
|
|
public function getUri() |
198
|
|
|
{ |
199
|
|
|
return $this->uri; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* {@inheritdoc} |
204
|
|
|
*/ |
205
|
|
|
public function setUri($uri) |
206
|
|
|
{ |
207
|
|
|
$this->uri = $uri; |
208
|
|
|
|
209
|
|
|
return $this; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* {@inheritdoc} |
214
|
|
|
*/ |
215
|
|
|
public function getParameters() |
216
|
|
|
{ |
217
|
|
|
return $this->parameters; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* {@inheritdoc} |
222
|
|
|
*/ |
223
|
|
|
public function setParameters(RequestParameters $parameters) |
224
|
|
|
{ |
225
|
|
|
$this->parameters = $parameters; |
226
|
|
|
|
227
|
|
|
return $this; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* {@inheritdoc} |
232
|
|
|
*/ |
233
|
|
|
public function getHeaders() |
234
|
|
|
{ |
235
|
|
|
return $this->headers; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* {@inheritdoc} |
240
|
|
|
*/ |
241
|
|
|
public function setHeaders(array $headers) |
242
|
|
|
{ |
243
|
|
|
$this->headers = $headers; |
244
|
|
|
|
245
|
|
|
return $this; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* {@inheritdoc} |
250
|
|
|
*/ |
251
|
|
|
public function getOptions() |
252
|
|
|
{ |
253
|
|
|
return $this->options; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* {@inheritdoc} |
258
|
|
|
*/ |
259
|
|
|
public function setOptions(array $options) |
260
|
|
|
{ |
261
|
|
|
$this->options = $options; |
262
|
|
|
|
263
|
|
|
return $this; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* {@inheritdoc} |
268
|
|
|
*/ |
269
|
|
|
public function getBaseUrl() |
270
|
|
|
{ |
271
|
|
|
return sprintf('%s://%s:%s', $this->protocol, $this->host, $this->port); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* {@inheritdoc} |
276
|
|
|
*/ |
277
|
|
|
public function getFullUrl() |
278
|
|
|
{ |
279
|
|
|
return sprintf( |
280
|
|
|
'%s/%s?%s', |
281
|
|
|
$this->getBaseUrl(), |
282
|
|
|
trim($this->uri, '/ '), |
283
|
|
|
$this->parameters->getAllParameters(true) |
284
|
|
|
); |
285
|
|
|
} |
286
|
|
|
} |
287
|
|
|
|