1
|
|
|
<?php namespace JobApis\Jobs\Client\Providers; |
2
|
|
|
|
3
|
|
|
use GuzzleHttp\Client as HttpClient; |
4
|
|
|
use JobApis\Jobs\Client\Collection; |
5
|
|
|
use JobApis\Jobs\Client\Exceptions\MissingParameterException; |
6
|
|
|
use JobApis\Jobs\Client\Queries\AbstractQuery; |
7
|
|
|
|
8
|
|
|
abstract class AbstractProvider |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* HTTP Client |
12
|
|
|
* |
13
|
|
|
* @var HttpClient |
14
|
|
|
*/ |
15
|
|
|
protected $client; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Query params |
19
|
|
|
* |
20
|
|
|
* @var AbstractQuery |
21
|
|
|
*/ |
22
|
|
|
protected $query; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Create new client |
26
|
|
|
* |
27
|
|
|
* @param array $parameters |
|
|
|
|
28
|
|
|
*/ |
29
|
16 |
|
public function __construct(AbstractQuery $query) |
30
|
|
|
{ |
31
|
16 |
|
$this->setQuery($query) |
32
|
16 |
|
->setClient(new HttpClient); |
33
|
16 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Returns the standardized job object |
37
|
|
|
* |
38
|
|
|
* @param array|object $payload |
39
|
|
|
* |
40
|
|
|
* @return \JobApis\Jobs\Client\Job |
41
|
|
|
*/ |
42
|
|
|
abstract public function createJobObject($payload); |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Job response object default keys that should be set |
46
|
|
|
* |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
abstract public function getDefaultResponseFields(); |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get listings path |
53
|
|
|
* |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
|
|
abstract public function getListingsPath(); |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Uses the Query to make a call to the client |
60
|
|
|
* |
61
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
62
|
|
|
*/ |
63
|
6 |
|
public function getClientResponse() |
64
|
|
|
{ |
65
|
|
|
// Create a local copy of the client object |
66
|
6 |
|
$client = $this->client; |
67
|
|
|
|
68
|
|
|
// GET or POST request |
69
|
6 |
|
$verb = strtolower($this->query->getVerb()); |
70
|
|
|
|
71
|
|
|
// The URL string |
72
|
6 |
|
$url = $this->query->getUrl(); |
73
|
|
|
|
74
|
|
|
// HTTP method options |
75
|
6 |
|
$options = $this->query->getHttpMethodOptions(); |
76
|
|
|
|
77
|
|
|
// Get the response |
78
|
6 |
|
return $client->{$verb}($url, $options); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get format |
83
|
|
|
* |
84
|
|
|
* @return string Currently only 'json' and 'xml' supported |
85
|
|
|
*/ |
86
|
6 |
|
public function getFormat() |
87
|
|
|
{ |
88
|
6 |
|
return 'json'; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Makes the api call and returns a collection of job objects |
93
|
|
|
* |
94
|
|
|
* @return \JobApis\Jobs\Client\Collection |
95
|
|
|
* @throws MissingParameterException |
96
|
|
|
*/ |
97
|
6 |
|
public function getJobs() |
98
|
|
|
{ |
99
|
|
|
// Verify that all required query vars are set |
100
|
6 |
|
if ($this->query->isValid()) { |
101
|
|
|
// Get the response from the client using the query |
102
|
4 |
|
$response = $this->getClientResponse(); |
103
|
|
|
// Get the response body as a string |
104
|
4 |
|
$body = (string) $response->getBody(); |
105
|
|
|
// Parse the string |
106
|
4 |
|
$payload = $this->parseAsFormat($body, $this->getFormat()); |
107
|
|
|
// Gets listings if they're nested |
108
|
4 |
|
$listings = is_array($payload) ? $this->getRawListings($payload) : []; |
109
|
|
|
// Return a job collection |
110
|
4 |
|
return $this->getJobsCollectionFromListings($listings); |
111
|
|
|
} else { |
112
|
2 |
|
throw new MissingParameterException("All Required parameters for this provider must be set"); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Get source attribution |
118
|
|
|
* |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
6 |
|
public function getSource() |
122
|
|
|
{ |
123
|
6 |
|
$classSuffix = "Provider"; |
124
|
|
|
|
125
|
6 |
|
$className = (new \ReflectionClass(get_class($this)))->getShortName(); |
126
|
|
|
|
127
|
|
|
// Strip off the suffix from the provider |
128
|
6 |
|
if ($this->stringEndsWith($className, $classSuffix)) { |
129
|
6 |
|
$className = substr($className, 0, strlen($className) - strlen($classSuffix)); |
130
|
6 |
|
} |
131
|
|
|
|
132
|
6 |
|
return $className; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Parse job attributes against defaults |
137
|
|
|
* |
138
|
|
|
* @param array $attributes |
139
|
|
|
* @param array $defaults |
140
|
|
|
* |
141
|
|
|
* @return array |
142
|
|
|
*/ |
143
|
6 |
|
public static function parseAttributeDefaults(array $attributes, array $defaults = array()) |
144
|
|
|
{ |
145
|
|
|
array_map(function ($attribute) use (&$attributes) { |
146
|
6 |
|
if (!isset($attributes[$attribute])) { |
147
|
6 |
|
$attributes[$attribute] = null; |
148
|
6 |
|
} |
149
|
6 |
|
}, $defaults); |
150
|
6 |
|
return $attributes; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Parse location string into components. |
155
|
|
|
* |
156
|
|
|
* @param string $location |
157
|
|
|
* |
158
|
|
|
* @return array |
159
|
|
|
**/ |
160
|
2 |
|
public static function parseLocation($location, $separator = ', ') |
161
|
|
|
{ |
162
|
2 |
|
return explode($separator, $location); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Sets http client |
167
|
|
|
* |
168
|
|
|
* @param HttpClient $client |
169
|
|
|
* |
170
|
|
|
* @return AbstractProvider |
171
|
|
|
*/ |
172
|
16 |
|
public function setClient(HttpClient $client) |
173
|
|
|
{ |
174
|
16 |
|
$this->client = $client; |
175
|
|
|
|
176
|
16 |
|
return $this; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Sets query object |
181
|
|
|
* |
182
|
|
|
* @param AbstractQuery $query |
183
|
|
|
* |
184
|
|
|
* @return AbstractProvider |
185
|
|
|
*/ |
186
|
16 |
|
public function setQuery(AbstractQuery $query) |
187
|
|
|
{ |
188
|
16 |
|
$this->query = $query; |
189
|
|
|
|
190
|
16 |
|
return $this; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Adds a single job item to a collection (passed by reference) |
195
|
|
|
* |
196
|
|
|
* @param $collection Collection |
197
|
|
|
* @param $item array |
198
|
|
|
* |
199
|
|
|
* @return $collection |
|
|
|
|
200
|
|
|
*/ |
201
|
4 |
|
protected function addJobItemToCollection(&$collection, $item = []) |
202
|
|
|
{ |
203
|
4 |
|
if ($item) { |
|
|
|
|
204
|
4 |
|
$item = static::parseAttributeDefaults( |
205
|
4 |
|
$item, |
206
|
4 |
|
$this->getDefaultResponseFields() |
|
|
|
|
207
|
4 |
|
); |
208
|
4 |
|
$job = $this->createJobObject($item); |
209
|
4 |
|
$job->setQuery($this->query->getKeyword()) |
210
|
4 |
|
->setSource($this->getSource()); |
211
|
4 |
|
$collection->add($job); |
212
|
4 |
|
} |
213
|
4 |
|
return $collection; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Create and get collection of jobs from given listings |
218
|
|
|
* |
219
|
|
|
* @param array $listings |
220
|
|
|
* |
221
|
|
|
* @return Collection |
222
|
|
|
*/ |
223
|
4 |
|
protected function getJobsCollectionFromListings(array $listings = []) |
224
|
|
|
{ |
225
|
4 |
|
$collection = new Collection; |
226
|
|
|
|
227
|
4 |
|
array_map(function ($item) use ($collection) { |
228
|
4 |
|
$this->addJobItemToCollection($collection, $item); |
229
|
4 |
|
}, $listings); |
230
|
|
|
|
231
|
4 |
|
return $collection; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Get raw listings from payload |
236
|
|
|
* |
237
|
|
|
* @param array $payload |
238
|
|
|
* |
239
|
|
|
* @return array |
240
|
|
|
*/ |
241
|
4 |
|
protected function getRawListings(array $payload = array()) |
242
|
|
|
{ |
243
|
4 |
|
$path = $this->getListingsPath(); |
244
|
|
|
|
245
|
4 |
|
if (!empty($path)) { |
246
|
4 |
|
$index = explode('.', $path); |
247
|
|
|
|
248
|
4 |
|
$listings = self::getValue($index, $payload); |
249
|
|
|
|
250
|
|
|
// Listings should be returned as an array of arrays |
251
|
4 |
|
if (reset($listings) && is_array(reset($listings))) { |
252
|
2 |
|
return $listings; |
253
|
|
|
} |
254
|
2 |
|
return [0 => $listings]; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
return (array) $payload; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Navigate through a payload array looking for a particular index |
262
|
|
|
* |
263
|
|
|
* @param array $index The index sequence we are navigating down |
264
|
|
|
* @param array $value The portion of the config array to process |
265
|
|
|
* |
266
|
|
|
* @return mixed |
267
|
|
|
*/ |
268
|
4 |
|
protected static function getValue($index, $value) |
269
|
|
|
{ |
270
|
4 |
|
$current_index = self::getValueCurrentIndex($index); |
271
|
|
|
|
272
|
4 |
|
if (isset($value[$current_index])) { |
273
|
4 |
|
$index_array = self::isArrayNotEmpty($index); |
274
|
4 |
|
$value_array = self::isArrayNotEmpty($value[$current_index]); |
275
|
|
|
|
276
|
4 |
|
if ($index_array && $value_array) { |
277
|
|
|
return self::getValue($index, $value[$current_index]); |
278
|
|
|
} else { |
279
|
4 |
|
return $value[$current_index]; |
280
|
|
|
} |
281
|
|
|
} else { |
282
|
|
|
throw new \OutOfRangeException("Attempt to access missing variable: $current_index"); |
283
|
|
|
} |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Attempt to parse string as given format |
288
|
|
|
* |
289
|
|
|
* @param string $string |
290
|
|
|
* @param string $format |
291
|
|
|
* |
292
|
|
|
* @return array |
293
|
|
|
*/ |
294
|
4 |
|
protected function parseAsFormat($string, $format) |
295
|
|
|
{ |
296
|
4 |
|
$method = 'parseAs'.ucfirst(strtolower($format)); |
297
|
|
|
|
298
|
4 |
|
if (method_exists($this, $method)) { |
299
|
4 |
|
return $this->$method($string); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
return []; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Get value current index |
307
|
|
|
* |
308
|
|
|
* @param mixed $index |
309
|
|
|
* |
310
|
|
|
* @return array|null |
311
|
|
|
*/ |
312
|
4 |
|
private static function getValueCurrentIndex(&$index) |
313
|
|
|
{ |
314
|
4 |
|
return is_array($index) && count($index) ? array_shift($index) : null; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* Checks if given value is an array and that it has contents |
319
|
|
|
* |
320
|
|
|
* @param mixed $array |
321
|
|
|
* |
322
|
|
|
* @return boolean |
323
|
|
|
*/ |
324
|
4 |
|
private static function isArrayNotEmpty($array) |
325
|
|
|
{ |
326
|
4 |
|
return is_array($array) && count($array); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Attempt to parse as Json |
331
|
|
|
* |
332
|
|
|
* @param string $string |
333
|
|
|
* |
334
|
|
|
* @return array |
335
|
|
|
*/ |
336
|
4 |
|
private function parseAsJson($string) |
337
|
|
|
{ |
338
|
|
|
try { |
339
|
4 |
|
$json = json_decode($string, true); |
340
|
|
|
|
341
|
4 |
|
if (json_last_error() != JSON_ERROR_NONE) { |
342
|
|
|
throw new \Exception; |
343
|
|
|
} |
344
|
|
|
|
345
|
4 |
|
return $json; |
346
|
|
|
} catch (\Exception $e) { |
347
|
|
|
// Ignore malformed json. |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
return []; |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* Attempt to parse as XML |
355
|
|
|
* |
356
|
|
|
* @param string $string |
357
|
|
|
* |
358
|
|
|
* @return array |
359
|
|
|
*/ |
360
|
|
|
private function parseAsXml($string) |
361
|
|
|
{ |
362
|
|
|
try { |
363
|
|
|
return json_decode( |
364
|
|
|
json_encode( |
365
|
|
|
simplexml_load_string( |
366
|
|
|
$string, |
367
|
|
|
null, |
368
|
|
|
LIBXML_NOCDATA |
369
|
|
|
) |
370
|
|
|
), |
371
|
|
|
true |
372
|
|
|
); |
373
|
|
|
} catch (\Exception $e) { |
374
|
|
|
// Ignore malformed xml. |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
return []; |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
/** |
381
|
|
|
* Determine whether a string ends with another string |
382
|
|
|
* |
383
|
|
|
* @param $string |
384
|
|
|
* @param $test |
385
|
|
|
* |
386
|
|
|
* @return bool |
387
|
|
|
*/ |
388
|
6 |
|
private function stringEndsWith($string, $test) |
389
|
|
|
{ |
390
|
6 |
|
$stringLen = strlen($string); |
391
|
6 |
|
$testLen = strlen($test); |
392
|
6 |
|
if ($testLen > $stringLen) { |
393
|
|
|
return false; |
394
|
|
|
} |
395
|
6 |
|
return substr_compare($string, $test, $stringLen - $testLen, $testLen) === 0; |
396
|
|
|
} |
397
|
|
|
} |
398
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.