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\ContentApiSdk; |
19
|
|
|
use Superdesk\ContentApiSdk\Exception\ResponseException; |
20
|
|
|
use Superdesk\ContentApiSdk\Exception\RequestException; |
21
|
|
|
use Superdesk\ContentApiSdk\Exception\InvalidArgumentException; |
22
|
|
|
use Superdesk\ContentApiSdk\Exception\InvalidDataException; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* API Request object. |
26
|
|
|
*/ |
27
|
|
|
class Request implements RequestInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* Protocol for api request. |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $protocol = 'https'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Hostname for api request. |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $host; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Api port. |
45
|
|
|
* |
46
|
|
|
* @var int |
47
|
|
|
*/ |
48
|
|
|
protected $port = 80; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Request uri. |
52
|
|
|
* |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
protected $uri; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Parameters for request. |
59
|
|
|
* |
60
|
|
|
* @var mixed[] |
61
|
|
|
*/ |
62
|
|
|
protected $parameters = array(); |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Validate parameters, unset invalid ones. |
66
|
|
|
* |
67
|
|
|
* @var boolean |
68
|
|
|
*/ |
69
|
|
|
protected $parameterValidation = true; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Request headers. |
73
|
|
|
* |
74
|
|
|
* @var string[] |
75
|
|
|
*/ |
76
|
|
|
protected $headers = array(); |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Request options, usuably in clients, etc. |
80
|
|
|
* |
81
|
|
|
* @var mixed[] |
82
|
|
|
*/ |
83
|
|
|
protected $options = array(); |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Construct a request object. |
87
|
|
|
* |
88
|
|
|
* @param string $hostname Host name |
89
|
|
|
* @param string $uri Request uri |
90
|
|
|
* @param mixed[] $parameters Parameters |
91
|
|
|
* @param int $port Port |
92
|
|
|
*/ |
93
|
|
|
public function __construct($hostname = null, $uri = null, array $parameters = null, $port = null) |
94
|
|
|
{ |
95
|
|
|
if (is_string($hostname) && !empty($hostname)) { |
96
|
|
|
$this->setHost($hostname); |
97
|
|
|
} |
98
|
|
|
if (is_string($uri) && !empty($uri)) { |
99
|
|
|
$this->setUri($uri); |
100
|
|
|
} |
101
|
|
|
if (!empty($parameters)) { |
102
|
|
|
$this->setParameters($parameters); |
103
|
|
|
} |
104
|
|
|
if (is_int($port)) { |
105
|
|
|
$this->setPort($port); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritdoc} |
111
|
|
|
*/ |
112
|
|
|
public function getHost() |
113
|
|
|
{ |
114
|
|
|
return $this->host; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritdoc} |
119
|
|
|
*/ |
120
|
|
|
public function setHost($host) |
121
|
|
|
{ |
122
|
|
|
$this->host = $host; |
123
|
|
|
|
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* {@inheritdoc} |
129
|
|
|
*/ |
130
|
|
|
public function getPort() |
131
|
|
|
{ |
132
|
|
|
return $this->port; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* {@inheritdoc} |
137
|
|
|
*/ |
138
|
|
|
public function setPort($port) |
139
|
|
|
{ |
140
|
|
|
$this->port = $port; |
141
|
|
|
|
142
|
|
|
return $this; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* {@inheritdoc} |
147
|
|
|
*/ |
148
|
|
|
public function getUri() |
149
|
|
|
{ |
150
|
|
|
return $this->uri; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* {@inheritdoc} |
155
|
|
|
*/ |
156
|
|
|
public function setUri($uri) |
157
|
|
|
{ |
158
|
|
|
$this->uri = $uri; |
159
|
|
|
|
160
|
|
|
return $this; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* {@inheritdoc} |
165
|
|
|
*/ |
166
|
|
|
public function getParameters() |
167
|
|
|
{ |
168
|
|
|
return $this->parameters; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* {@inheritdoc} |
173
|
|
|
*/ |
174
|
|
|
public function setParameters(array $parameters) |
175
|
|
|
{ |
176
|
|
|
try { |
177
|
|
|
// TODO: Maybe we should move the parameter validation to this class? |
178
|
|
|
$this->parameters = ContentApiSdk::processParameters($parameters, $this->parameterValidation); |
179
|
|
|
} catch (InvalidArgumentException $e) { |
180
|
|
|
throw new RequestException($e->getMessage(), $e->getCode(), $e); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
return $this; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* {@inheritdoc} |
188
|
|
|
*/ |
189
|
|
|
public function getParameterValidation() |
190
|
|
|
{ |
191
|
|
|
return $this->parameterValidation; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* {@inheritdoc} |
196
|
|
|
*/ |
197
|
|
|
public function enableParameterValidation() |
198
|
|
|
{ |
199
|
|
|
$this->parameterValidation = true; |
200
|
|
|
|
201
|
|
|
return $this; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* {@inheritdoc} |
206
|
|
|
*/ |
207
|
|
|
public function disableParameterValidation() |
208
|
|
|
{ |
209
|
|
|
$this->parameterValidation = false; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* {@inheritdoc} |
214
|
|
|
*/ |
215
|
|
|
public function getHeaders() |
216
|
|
|
{ |
217
|
|
|
return $this->headers; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* {@inheritdoc} |
222
|
|
|
*/ |
223
|
|
|
public function setHeaders(array $headers) |
224
|
|
|
{ |
225
|
|
|
$this->headers = $headers; |
226
|
|
|
|
227
|
|
|
return $this; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* {@inheritdoc} |
232
|
|
|
*/ |
233
|
|
|
public function getOptions() |
234
|
|
|
{ |
235
|
|
|
return $this->options; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* {@inheritdoc} |
240
|
|
|
*/ |
241
|
|
|
public function setOptions(array $options) |
242
|
|
|
{ |
243
|
|
|
$this->options = $options; |
244
|
|
|
|
245
|
|
|
return $this; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* {@inheritdoc} |
250
|
|
|
*/ |
251
|
|
|
public function getBaseUrl() |
252
|
|
|
{ |
253
|
|
|
return sprintf('%s://%s:%s', $this->protocol, $this->host, $this->port); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* {@inheritdoc} |
258
|
|
|
*/ |
259
|
|
|
public function getFullUrl() |
260
|
|
|
{ |
261
|
|
|
return sprintf('%s/%s?%s', $this->getBaseUrl(), trim($this->uri, '/ '), http_build_query($this->parameters)); |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
|