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\Request; |
16
|
|
|
|
17
|
|
|
use Superdesk\ContentApiSdk\Exception\InvalidArgumentException; |
18
|
|
|
use DateTime; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Request parameter class. |
22
|
|
|
*/ |
23
|
|
|
class RequestParameters |
24
|
|
|
{ |
25
|
|
|
const DEFAULT_PAGE = 1; |
26
|
|
|
const DEFAULT_MAX_RESULTS = 25; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Start date. |
30
|
|
|
* |
31
|
|
|
* @var DateTime|null |
32
|
|
|
*/ |
33
|
|
|
protected $startDate; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* End date. |
37
|
|
|
* |
38
|
|
|
* @var DateTime|null |
39
|
|
|
*/ |
40
|
|
|
protected $endDate; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Query for text search. |
44
|
|
|
* |
45
|
|
|
* @var string|null |
46
|
|
|
*/ |
47
|
|
|
protected $query; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Page. |
51
|
|
|
* |
52
|
|
|
* @var int |
53
|
|
|
*/ |
54
|
|
|
protected $page = self::DEFAULT_PAGE; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Max results per page. |
58
|
|
|
* |
59
|
|
|
* @var int |
60
|
|
|
*/ |
61
|
|
|
protected $maxResults = self::DEFAULT_MAX_RESULTS; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Include fields list. These fields will be set in your packages or items. |
65
|
|
|
* |
66
|
|
|
* @var array|null |
67
|
|
|
*/ |
68
|
|
|
protected $includeFields; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Exclude fields lists. These fields won't be set in your packages or items. |
72
|
|
|
* |
73
|
|
|
* @var array|null |
74
|
|
|
*/ |
75
|
|
|
protected $excludeFields; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Mapping array, which links correct parameter name for API with proper |
79
|
|
|
* method. |
80
|
|
|
* |
81
|
|
|
* @var string[] |
82
|
|
|
*/ |
83
|
|
|
protected $propertyMapping = array( |
84
|
|
|
'start_date' => 'getStartDate', |
85
|
|
|
'end_date' => 'getEndDate', |
86
|
|
|
'q' => 'getQuery', |
87
|
|
|
'page' => 'getPage', |
88
|
|
|
'max_results' => 'getMaxResults', |
89
|
|
|
'include_fields' => 'getIncludeFields', |
90
|
|
|
'exclude_fields' => 'getExcludeFields', |
91
|
|
|
); |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Returns start date. |
95
|
|
|
* |
96
|
|
|
* @return DateTime|null |
97
|
|
|
*/ |
98
|
|
|
public function getStartDate() |
99
|
|
|
{ |
100
|
|
|
return $this->startDate; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Sets start date. Will accept a string formatted 'yyyy-mm-dd' or DateTime |
105
|
|
|
* object. |
106
|
|
|
* |
107
|
|
|
* @param string|DateTime|null $startDate |
108
|
|
|
*/ |
109
|
|
View Code Duplication |
public function setStartDate($startDate) |
|
|
|
|
110
|
|
|
{ |
111
|
|
|
if ($startDate !== null) { |
112
|
|
|
try { |
113
|
|
|
$this->startDate = $this->validateDate($startDate); |
114
|
|
|
} catch (InvalidArgumentException $e) { |
115
|
|
|
throw new InvalidArgumentException('Invalid value for start_date parameter.', $e->getCode(), $e); |
116
|
|
|
} |
117
|
|
|
} else { |
118
|
|
|
$this->startDate = null; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Returns end date. |
126
|
|
|
* |
127
|
|
|
* @return DateTime|null |
128
|
|
|
*/ |
129
|
|
|
public function getEndDate() |
130
|
|
|
{ |
131
|
|
|
return $this->endDate; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Sets end date. Will accept string formatted 'yyyy-mm-dd' or DateTIme |
136
|
|
|
* object. |
137
|
|
|
* |
138
|
|
|
* @param string|DateTime|null $endDate |
139
|
|
|
*/ |
140
|
|
View Code Duplication |
public function setEndDate($endDate) |
|
|
|
|
141
|
|
|
{ |
142
|
|
|
if ($endDate !== null) { |
143
|
|
|
try { |
144
|
|
|
$this->endDate = $this->validateDate($endDate); |
145
|
|
|
} catch (InvalidArgumentException $e) { |
146
|
|
|
throw new InvalidArgumentException('Invalid value for end_date parameter.', $e->getCode(), $e); |
147
|
|
|
} |
148
|
|
|
} else { |
149
|
|
|
$this->endDate = $endDate; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Returns text query. |
157
|
|
|
* |
158
|
|
|
* @return string |
159
|
|
|
*/ |
160
|
|
|
public function getQuery() |
161
|
|
|
{ |
162
|
|
|
return $this->query; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Sets query parameters. |
167
|
|
|
* |
168
|
|
|
* @param string|null $query |
169
|
|
|
*/ |
170
|
|
|
public function setQuery($query) |
171
|
|
|
{ |
172
|
|
|
if (!is_string($query) && $query !== null) { |
173
|
|
|
throw new InvalidArgumentException('Parameter query should be of type string or null.'); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
$this->query = $query; |
177
|
|
|
|
178
|
|
|
return $this; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Returns page number. |
183
|
|
|
* |
184
|
|
|
* @return int |
185
|
|
|
*/ |
186
|
|
|
public function getPage() |
187
|
|
|
{ |
188
|
|
|
return $this->page; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Sets page number. If null is supplied, resets to class default. |
193
|
|
|
* |
194
|
|
|
* @param int|null $page |
195
|
|
|
*/ |
196
|
|
View Code Duplication |
public function setPage($page) |
|
|
|
|
197
|
|
|
{ |
198
|
|
|
if ($page === null) { |
199
|
|
|
$page = self::DEFAULT_PAGE; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
try { |
203
|
|
|
$this->page = $this->validateNumeric($page); |
204
|
|
|
} catch (InvalidArgumentException $e) { |
205
|
|
|
throw new InvalidArgumentException('Invalid value for page parameter.', $e->getCode(), $e); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
return $this; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Returns maximum results per page. |
213
|
|
|
* |
214
|
|
|
* @return int |
215
|
|
|
*/ |
216
|
|
|
public function getMaxResults() |
217
|
|
|
{ |
218
|
|
|
return $this->maxResults; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Sets maximum results per page. If null is supplied, resets to class |
223
|
|
|
* default. |
224
|
|
|
* |
225
|
|
|
* @param int|nul $maxResults |
226
|
|
|
*/ |
227
|
|
View Code Duplication |
public function setMaxResults($maxResults) |
|
|
|
|
228
|
|
|
{ |
229
|
|
|
if ($maxResults === null) { |
230
|
|
|
$maxResults = self::DEFAULT_MAX_RESULTS; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
try { |
234
|
|
|
$this->maxResults = $this->validateNumeric($maxResults); |
235
|
|
|
} catch (InvalidArgumentException $e) { |
236
|
|
|
throw new InvalidArgumentException('Invalid value for maxResults parameter.', $e->getCode(), $e); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
return $this; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Returns include fields. |
244
|
|
|
* |
245
|
|
|
* @return array |
246
|
|
|
*/ |
247
|
|
|
public function getIncludeFields() |
248
|
|
|
{ |
249
|
|
|
return $this->includeFields; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Sets include fields. |
254
|
|
|
* |
255
|
|
|
* @param array|null $includeFields |
256
|
|
|
*/ |
257
|
|
View Code Duplication |
public function setIncludeFields($includeFields) |
|
|
|
|
258
|
|
|
{ |
259
|
|
|
if ($includeFields !== null) { |
260
|
|
|
try { |
261
|
|
|
$this->includeFields = $this->validateStringOrArray($includeFields); |
262
|
|
|
} catch (InvalidArgumentException $e) { |
263
|
|
|
throw new InvalidArgumentException('Invalid value for include_fields parameter.', $e->getCode(), $e); |
264
|
|
|
} |
265
|
|
|
} else { |
266
|
|
|
$this->includeFields = null; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
return $this; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Gets exclude fields. |
274
|
|
|
* |
275
|
|
|
* @return array |
276
|
|
|
*/ |
277
|
|
|
public function getExcludeFields() |
278
|
|
|
{ |
279
|
|
|
return $this->excludeFields; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Sets exclude fields |
284
|
|
|
* |
285
|
|
|
* @param array|null |
286
|
|
|
*/ |
287
|
|
View Code Duplication |
public function setExcludeFields($excludeFields) |
|
|
|
|
288
|
|
|
{ |
289
|
|
|
if ($excludeFields !== null) { |
290
|
|
|
try { |
291
|
|
|
$this->excludeFields = $this->validateStringOrArray($excludeFields); |
292
|
|
|
} catch (InvalidArgumentException $e) { |
293
|
|
|
throw new InvalidArgumentException('Invalid value for exclude_fields parameter.', $e->getCode(), $e); |
294
|
|
|
} |
295
|
|
|
} else { |
296
|
|
|
$this->excludeFields = null; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
return $this; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Validate date parameter input and converts to DateTime. |
304
|
|
|
* |
305
|
|
|
* @param string|DateTime $date When string format yyyy-mm-dd should be used |
306
|
|
|
* |
307
|
|
|
* @return DateTime |
308
|
|
|
*/ |
309
|
|
|
private function validateDate($date) |
310
|
|
|
{ |
311
|
|
|
if (!is_string($date) && !($date instanceof \DateTime)) { |
312
|
|
|
throw new InvalidArgumentException('Parameter should be of type string or DateTime.'); |
313
|
|
|
} elseif (is_string($date)) { |
314
|
|
|
if (!preg_match('/\d\d\d\d\-\d\d\-\d\d/', $date)) { |
315
|
|
|
throw new InvalidArgumentException('Parameter %s has invalid format, please use yyyy-mm-dd.'); |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
$date = new DateTime($date); |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
return $date; |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* Arguments of type string and array are valid. String will be split on , |
326
|
|
|
* and converted to an array. |
327
|
|
|
* |
328
|
|
|
* @param string|array $value |
329
|
|
|
* |
330
|
|
|
* @return array |
331
|
|
|
*/ |
332
|
|
|
private function validateStringOrArray($value) |
333
|
|
|
{ |
334
|
|
|
if (!is_string($value) && !is_array($value)) { |
335
|
|
|
throw new InvalidArgumentException('Parameter should be of type string or array.'); |
336
|
|
|
} elseif (is_string($value)) { |
337
|
|
|
$value = array_map('trim', explode(',', $value)); |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
return $value; |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* Validates if value is numeric. |
345
|
|
|
* |
346
|
|
|
* @param string|int $value |
347
|
|
|
* |
348
|
|
|
* @return int |
349
|
|
|
*/ |
350
|
|
|
private function validateNumeric($value) |
351
|
|
|
{ |
352
|
|
|
if (!is_int($value) && !ctype_digit($value)) { |
353
|
|
|
throw new InvalidArgumentException('Parameter should be of type integer.'); |
354
|
|
|
} elseif (!is_int($value)) { |
355
|
|
|
$value = (int) $value; |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
return $value; |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
/** |
362
|
|
|
* Returns all properties either as an array or http query string. |
363
|
|
|
* |
364
|
|
|
* @param boolean $buildHttpQuery Build query from array |
365
|
|
|
* |
366
|
|
|
* @return mixed[]|string |
367
|
|
|
*/ |
368
|
|
|
public function getAllParameters($buildHttpQuery = false) |
369
|
|
|
{ |
370
|
|
|
$httpQuery = array(); |
371
|
|
|
|
372
|
|
|
foreach ($this->propertyMapping as $uriParameter => $method) { |
373
|
|
|
|
374
|
|
|
$value = $this->$method(); |
375
|
|
|
|
376
|
|
|
if ($value instanceof DateTIme) { |
377
|
|
|
$value = $value->format('Y-m-d'); |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
$httpQuery[$uriParameter] = $value; |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
return ($buildHttpQuery) ? http_build_query($httpQuery) : $httpQuery; |
384
|
|
|
} |
385
|
|
|
} |
386
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.