|
1
|
|
|
<?php namespace JobApis\Jobs\Client; |
|
2
|
|
|
|
|
3
|
|
|
use JobApis\Jobs\Client\Providers\AbstractProvider; |
|
4
|
|
|
use JobApis\Jobs\Client\Queries\AbstractQuery; |
|
5
|
|
|
|
|
6
|
|
|
class JobsMulti |
|
7
|
|
|
{ |
|
8
|
|
|
/** |
|
9
|
|
|
* Search keyword |
|
10
|
|
|
* |
|
11
|
|
|
* @var string |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $keyword; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Search location |
|
17
|
|
|
* |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $location; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Maximum age of results (in days) |
|
24
|
|
|
* |
|
25
|
|
|
* @var integer |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $maxAge; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Maximum number of results to return in all results |
|
31
|
|
|
* |
|
32
|
|
|
* @var integer |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $maxResults; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Order of results |
|
38
|
|
|
* |
|
39
|
|
|
* @var string |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $order; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Field to order results by |
|
45
|
|
|
* |
|
46
|
|
|
* @var string |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $orderBy; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Results page number |
|
52
|
|
|
* |
|
53
|
|
|
* @var integer |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $pageNumber; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Results per page |
|
59
|
|
|
* |
|
60
|
|
|
* @var integer |
|
61
|
|
|
*/ |
|
62
|
|
|
protected $perPage; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Job board API providers |
|
66
|
|
|
* |
|
67
|
|
|
* @var array |
|
68
|
|
|
*/ |
|
69
|
|
|
protected $providers = []; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Job board API query objects |
|
73
|
|
|
* |
|
74
|
|
|
* @var array |
|
75
|
|
|
*/ |
|
76
|
|
|
protected $queries = []; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Creates query objects for each provider and creates this unified client. |
|
80
|
|
|
* |
|
81
|
|
|
* @param array $providers |
|
82
|
|
|
*/ |
|
83
|
20 |
|
public function __construct($providers = []) |
|
84
|
|
|
{ |
|
85
|
20 |
|
$this->setProviders($providers); |
|
86
|
20 |
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Gets jobs from all providers in a single go and returns a MultiCollection |
|
90
|
|
|
* |
|
91
|
|
|
* @return Collection |
|
92
|
|
|
*/ |
|
93
|
2 |
|
public function getAllJobs($options = []) |
|
94
|
|
|
{ |
|
95
|
|
|
// Set options that are passed in |
|
96
|
2 |
|
$this->setOptions($options); |
|
97
|
|
|
|
|
98
|
|
|
// Create a new Collection |
|
99
|
2 |
|
$collection = new Collection(); |
|
100
|
2 |
|
foreach ($this->providers as $providerName => $options) { |
|
101
|
2 |
|
$collection->addCollection($this->getJobsByProvider($providerName)); |
|
102
|
2 |
|
} |
|
103
|
|
|
|
|
104
|
|
|
// Apply sorting and ordering options and return the collection |
|
105
|
2 |
|
return $this->applyOptions($collection); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Gets jobs from a single provider and hydrates a new jobs collection. |
|
110
|
|
|
* |
|
111
|
|
|
* @var $name string Provider name. |
|
112
|
|
|
* |
|
113
|
|
|
* @return \JobApis\Jobs\Client\Collection |
|
114
|
|
|
*/ |
|
115
|
8 |
|
public function getJobsByProvider($name = null, $options = []) |
|
116
|
|
|
{ |
|
117
|
|
|
// Set options that are passed in |
|
118
|
8 |
|
$this->setOptions($options); |
|
119
|
|
|
|
|
120
|
|
|
try { |
|
121
|
|
|
// Instantiate the query with all our parameters |
|
122
|
8 |
|
$query = $this->instantiateQuery($name); |
|
123
|
|
|
|
|
124
|
|
|
// Instantiate the provider |
|
125
|
6 |
|
$provider = $this->instantiateProvider($name, $query); |
|
126
|
|
|
|
|
127
|
|
|
// Apply sorting and ordering options and return the collection |
|
128
|
6 |
|
return $this->applyOptions($provider->getJobs()); |
|
129
|
2 |
|
} catch (\Exception $e) { |
|
130
|
2 |
|
return (new Collection())->addError($e->getMessage()); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Sets a keyword on the query. |
|
136
|
|
|
* |
|
137
|
|
|
* @param $keyword string |
|
138
|
|
|
* |
|
139
|
|
|
* @return $this |
|
140
|
|
|
*/ |
|
141
|
8 |
|
public function setKeyword($keyword = null) |
|
142
|
|
|
{ |
|
143
|
8 |
|
$this->keyword = $keyword; |
|
144
|
|
|
|
|
145
|
8 |
|
return $this; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Sets a location on the query for each provider. |
|
150
|
|
|
* |
|
151
|
|
|
* @param $location |
|
152
|
|
|
* |
|
153
|
|
|
* @return $this |
|
154
|
|
|
*/ |
|
155
|
8 |
|
public function setLocation($location = null) |
|
156
|
|
|
{ |
|
157
|
8 |
|
if (!$this->isValidLocation($location)) { |
|
158
|
2 |
|
throw new \OutOfBoundsException("Location parameter must follow the pattern 'City, ST'."); |
|
159
|
|
|
} |
|
160
|
6 |
|
$this->location = $location; |
|
161
|
|
|
|
|
162
|
6 |
|
return $this; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Sets the options used for the resulting collection |
|
167
|
|
|
* |
|
168
|
|
|
* @param array $options |
|
169
|
|
|
* |
|
170
|
|
|
* @return $this |
|
171
|
|
|
*/ |
|
172
|
10 |
|
public function setOptions($options = []) |
|
173
|
|
|
{ |
|
174
|
10 |
|
if (isset($options['maxAge'])) { |
|
175
|
4 |
|
$this->maxAge = $options['maxAge']; |
|
176
|
4 |
|
} |
|
177
|
10 |
|
if (isset($options['maxResults'])) { |
|
178
|
4 |
|
$this->maxResults = $options['maxResults']; |
|
179
|
4 |
|
} |
|
180
|
10 |
|
if (isset($options['order'])) { |
|
181
|
4 |
|
$this->order = $options['order']; |
|
182
|
4 |
|
} |
|
183
|
10 |
|
if (isset($options['orderBy'])) { |
|
184
|
4 |
|
$this->orderBy = $options['orderBy']; |
|
185
|
4 |
|
} |
|
186
|
|
|
|
|
187
|
10 |
|
return $this; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Sets a page number and number of results per page for each provider. |
|
192
|
|
|
* |
|
193
|
|
|
* @param $pageNumber integer |
|
194
|
|
|
* @param $perPage integer |
|
195
|
|
|
* |
|
196
|
|
|
* @return $this |
|
197
|
|
|
*/ |
|
198
|
6 |
|
public function setPage($pageNumber = 1, $perPage = 10) |
|
199
|
|
|
{ |
|
200
|
6 |
|
$this->pageNumber = $pageNumber; |
|
201
|
6 |
|
$this->perPage = $perPage; |
|
202
|
|
|
|
|
203
|
6 |
|
return $this; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Sets an array of providers. |
|
208
|
|
|
* |
|
209
|
|
|
* @param $providers array |
|
210
|
|
|
* |
|
211
|
|
|
* @return $this |
|
212
|
|
|
*/ |
|
213
|
20 |
|
public function setProviders($providers = []) |
|
214
|
|
|
{ |
|
215
|
20 |
|
$this->providers = $providers; |
|
216
|
|
|
|
|
217
|
20 |
|
return $this; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Apply the options for this JobsMulti object to the Collection |
|
222
|
|
|
* |
|
223
|
|
|
* @param Collection $collection |
|
224
|
|
|
* |
|
225
|
|
|
* @return Collection |
|
226
|
|
|
*/ |
|
227
|
6 |
|
protected function applyOptions(Collection $collection) |
|
228
|
|
|
{ |
|
229
|
|
|
// Order the results |
|
230
|
6 |
|
if ($this->orderBy && $this->order) { |
|
231
|
2 |
|
$collection->orderBy($this->orderBy, $this->order); |
|
232
|
2 |
|
} |
|
233
|
|
|
|
|
234
|
|
|
// Filter older listings out |
|
235
|
6 |
|
if ($this->maxAge) { |
|
236
|
2 |
|
$collection->filter( |
|
237
|
2 |
|
'datePosted', |
|
238
|
2 |
|
new \DateTime($this->maxAge.' days ago'), |
|
|
|
|
|
|
239
|
|
|
'>' |
|
240
|
2 |
|
); |
|
241
|
2 |
|
} |
|
242
|
|
|
|
|
243
|
|
|
// Truncate to the maximum results (all by default) |
|
244
|
6 |
|
if ($this->maxResults) { |
|
245
|
2 |
|
$collection->truncate($this->maxResults); |
|
246
|
2 |
|
} |
|
247
|
|
|
|
|
248
|
6 |
|
return $collection; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* Gets an array of options from a translator array |
|
253
|
|
|
* |
|
254
|
|
|
* @param array $translator |
|
255
|
|
|
* |
|
256
|
|
|
* @return array |
|
257
|
|
|
*/ |
|
258
|
6 |
|
protected function getOptionsFromTranslator($translator = []) |
|
259
|
|
|
{ |
|
260
|
6 |
|
$options = []; |
|
261
|
6 |
|
foreach ($translator as $standardKey => $providerKey) { |
|
262
|
6 |
|
if (method_exists($this, $providerKey)) { |
|
263
|
3 |
|
$options = array_merge( |
|
264
|
3 |
|
$options, |
|
265
|
3 |
|
$this->$providerKey($this->{$standardKey}) |
|
266
|
3 |
|
); |
|
267
|
3 |
|
} else { |
|
268
|
6 |
|
$options[$providerKey] = $this->{$standardKey}; |
|
269
|
|
|
} |
|
270
|
6 |
|
} |
|
271
|
6 |
|
return $options; |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* Gets the options array based on the provider name. |
|
276
|
|
|
* |
|
277
|
|
|
* @param $name |
|
278
|
|
|
* |
|
279
|
|
|
* @return array |
|
280
|
|
|
*/ |
|
281
|
6 |
|
protected function getTranslatorForProvider($name) |
|
282
|
|
|
{ |
|
283
|
|
|
switch ($name) { |
|
284
|
6 |
|
case 'Careerbuilder': |
|
285
|
|
|
return [ |
|
286
|
|
|
'keyword' => 'Keywords', |
|
287
|
|
|
'location' => 'Location', |
|
288
|
|
|
'pageNumber' => 'PageNumber', |
|
289
|
|
|
'perPage' => 'PerPage', |
|
290
|
|
|
]; |
|
291
|
|
|
break; |
|
|
|
|
|
|
292
|
6 |
|
case 'Careercast': |
|
293
|
|
|
return [ |
|
294
|
|
|
'keyword' => 'keyword', |
|
295
|
|
|
'location' => 'location', |
|
296
|
|
|
'pageNumber' => 'page', |
|
297
|
|
|
'perPage' => 'rows', |
|
298
|
|
|
]; |
|
299
|
|
|
break; |
|
|
|
|
|
|
300
|
6 |
|
case 'Careerjet': |
|
301
|
|
|
return [ |
|
302
|
|
|
'keyword' => 'keywords', |
|
303
|
|
|
'location' => 'location', |
|
304
|
|
|
'pageNumber' => 'page', |
|
305
|
|
|
'perPage' => 'pagesize', |
|
306
|
|
|
]; |
|
307
|
|
|
break; |
|
|
|
|
|
|
308
|
6 |
|
case 'Dice': |
|
309
|
|
|
return [ |
|
310
|
2 |
|
'keyword' => 'text', |
|
311
|
2 |
|
'location' => 'getCityAndState', |
|
312
|
2 |
|
'pageNumber' => 'page', |
|
313
|
2 |
|
'perPage' => 'pgcnt', |
|
314
|
2 |
|
]; |
|
315
|
|
|
break; |
|
|
|
|
|
|
316
|
6 |
|
case 'Github': |
|
317
|
|
|
return [ |
|
318
|
2 |
|
'keyword' => 'search', |
|
319
|
2 |
|
'location' => 'location', |
|
320
|
2 |
|
'pageNumber' => 'getPageMinusOne', |
|
321
|
2 |
|
]; |
|
322
|
|
|
break; |
|
|
|
|
|
|
323
|
6 |
|
case 'Govt': |
|
324
|
|
|
return [ |
|
325
|
3 |
|
'keyword' => 'getQueryWithKeywordAndLocation', |
|
326
|
3 |
|
'pageNumber' => 'getFrom', |
|
327
|
3 |
|
'perPage' => 'size', |
|
328
|
3 |
|
]; |
|
329
|
|
|
break; |
|
|
|
|
|
|
330
|
5 |
|
case 'Ieee': |
|
331
|
|
|
return [ |
|
332
|
2 |
|
'keyword' => 'keyword', |
|
333
|
2 |
|
'location' => 'location', |
|
334
|
2 |
|
'pageNumber' => 'page', |
|
335
|
2 |
|
'perPage' => 'rows', |
|
336
|
2 |
|
]; |
|
337
|
|
|
break; |
|
|
|
|
|
|
338
|
5 |
|
case 'Indeed': |
|
339
|
|
|
return [ |
|
340
|
|
|
'keyword' => 'q', |
|
341
|
|
|
'location' => 'l', |
|
342
|
|
|
'pageNumber' => 'getStart', |
|
343
|
|
|
'perPage' => 'limit', |
|
344
|
|
|
]; |
|
345
|
|
|
break; |
|
|
|
|
|
|
346
|
5 |
|
case 'Jobinventory': |
|
347
|
|
|
return [ |
|
348
|
4 |
|
'keyword' => 'q', |
|
349
|
4 |
|
'location' => 'l', |
|
350
|
4 |
|
'pageNumber' => 'p', |
|
351
|
4 |
|
'perPage' => 'limit', |
|
352
|
4 |
|
]; |
|
353
|
|
|
break; |
|
|
|
|
|
|
354
|
3 |
|
case 'Juju': |
|
355
|
|
|
return [ |
|
356
|
|
|
'keyword' => 'k', |
|
357
|
|
|
'location' => 'l', |
|
358
|
|
|
'pageNumber' => 'page', |
|
359
|
|
|
'perPage' => 'jpp', |
|
360
|
|
|
]; |
|
361
|
|
|
break; |
|
|
|
|
|
|
362
|
3 |
|
case 'Stackoverflow': |
|
363
|
|
|
return [ |
|
364
|
3 |
|
'keyword' => 'q', |
|
365
|
3 |
|
'location' => 'l', |
|
366
|
3 |
|
'pageNumber' => 'pg', |
|
367
|
3 |
|
]; |
|
368
|
|
|
break; |
|
|
|
|
|
|
369
|
|
|
case 'Usajobs': |
|
370
|
|
|
return [ |
|
371
|
|
|
'keyword' => 'Keyword', |
|
372
|
|
|
'location' => 'LocationName', |
|
373
|
|
|
'pageNumber' => 'Page', |
|
374
|
|
|
'perPage' => 'ResultsPerPage', |
|
375
|
|
|
]; |
|
376
|
|
|
break; |
|
|
|
|
|
|
377
|
|
|
case 'Ziprecruiter': |
|
378
|
|
|
return [ |
|
379
|
|
|
'keyword' => 'search', |
|
380
|
|
|
'location' => 'location', |
|
381
|
|
|
'pageNumber' => 'page', |
|
382
|
|
|
'perPage' => 'jobs_per_page', |
|
383
|
|
|
]; |
|
384
|
|
|
break; |
|
|
|
|
|
|
385
|
|
|
default: |
|
386
|
|
|
throw new \Exception("Provider {$name} not found"); |
|
387
|
|
|
} |
|
388
|
|
|
} |
|
389
|
|
|
|
|
390
|
|
|
/** |
|
391
|
|
|
* Instantiates a provider using a query object. |
|
392
|
|
|
* |
|
393
|
|
|
* @param null $name |
|
394
|
|
|
* @param AbstractQuery $query |
|
395
|
|
|
* |
|
396
|
|
|
* @return AbstractProvider |
|
397
|
|
|
*/ |
|
398
|
6 |
|
protected function instantiateProvider($name, AbstractQuery $query) |
|
399
|
|
|
{ |
|
400
|
6 |
|
$path = 'JobApis\\Jobs\\Client\\Providers\\' . $name . 'Provider'; |
|
401
|
|
|
|
|
402
|
6 |
|
return new $path($query); |
|
403
|
|
|
} |
|
404
|
|
|
|
|
405
|
|
|
/** |
|
406
|
|
|
* Instantiates a query using a client name. |
|
407
|
|
|
* |
|
408
|
|
|
* @param null $name |
|
409
|
|
|
* |
|
410
|
|
|
* @return AbstractQuery |
|
411
|
|
|
*/ |
|
412
|
8 |
|
protected function instantiateQuery($name) |
|
413
|
|
|
{ |
|
414
|
8 |
|
$path = 'JobApis\\Jobs\\Client\\Queries\\' . $name . 'Query'; |
|
415
|
|
|
|
|
416
|
8 |
|
$options = array_merge( |
|
417
|
8 |
|
$this->providers[$name], |
|
418
|
6 |
|
$this->getOptionsFromTranslator($this->getTranslatorForProvider($name)) |
|
|
|
|
|
|
419
|
6 |
|
); |
|
420
|
|
|
|
|
421
|
6 |
|
return new $path($options); |
|
422
|
|
|
} |
|
423
|
|
|
|
|
424
|
|
|
/** |
|
425
|
|
|
* Get the city and state as an array from a location string. |
|
426
|
|
|
* |
|
427
|
|
|
* @return array |
|
428
|
|
|
*/ |
|
429
|
2 |
|
private function getCityAndState() |
|
430
|
|
|
{ |
|
431
|
2 |
|
if ($this->location) { |
|
432
|
2 |
|
$locationArr = explode(', ', $this->location); |
|
433
|
|
|
return [ |
|
434
|
2 |
|
'city' => $locationArr[0], |
|
435
|
2 |
|
'state' => $locationArr[1], |
|
436
|
2 |
|
]; |
|
437
|
|
|
} |
|
438
|
|
|
return []; |
|
439
|
|
|
} |
|
440
|
|
|
|
|
441
|
|
|
/** |
|
442
|
|
|
* Gets a from value. |
|
443
|
|
|
* |
|
444
|
|
|
* @return array |
|
445
|
|
|
*/ |
|
446
|
3 |
|
private function getFrom() |
|
447
|
|
|
{ |
|
448
|
3 |
|
if ($this->pageNumber && $this->perPage) { |
|
449
|
|
|
return [ |
|
450
|
3 |
|
'from' => ($this->pageNumber * $this->perPage) - $this->perPage, |
|
451
|
3 |
|
]; |
|
452
|
|
|
} |
|
453
|
|
|
return []; |
|
454
|
|
|
} |
|
455
|
|
|
|
|
456
|
|
|
/** |
|
457
|
|
|
* Gets page number minus 1. |
|
458
|
|
|
* |
|
459
|
|
|
* @return array |
|
460
|
|
|
*/ |
|
461
|
2 |
|
private function getPageMinusOne() |
|
462
|
|
|
{ |
|
463
|
2 |
|
if ($this->pageNumber) { |
|
464
|
|
|
return [ |
|
465
|
2 |
|
'page' => $this->pageNumber - 1, |
|
466
|
2 |
|
]; |
|
467
|
|
|
} |
|
468
|
|
|
return []; |
|
469
|
|
|
} |
|
470
|
|
|
|
|
471
|
|
|
/** |
|
472
|
|
|
* Get the query with keyword and location. |
|
473
|
|
|
* |
|
474
|
|
|
* @return array |
|
475
|
|
|
*/ |
|
476
|
3 |
|
private function getQueryWithKeywordAndLocation() |
|
477
|
|
|
{ |
|
478
|
3 |
|
$queryString = $this->keyword; |
|
479
|
|
|
|
|
480
|
3 |
|
if ($this->location) { |
|
481
|
3 |
|
$queryString .= ' in '.$this->location; |
|
482
|
3 |
|
} |
|
483
|
|
|
|
|
484
|
|
|
return [ |
|
485
|
3 |
|
'query' => $queryString, |
|
486
|
3 |
|
]; |
|
487
|
|
|
} |
|
488
|
|
|
|
|
489
|
|
|
/** |
|
490
|
|
|
* Gets a start at value. |
|
491
|
|
|
* |
|
492
|
|
|
* @return array |
|
493
|
|
|
*/ |
|
494
|
|
|
private function getStart() |
|
495
|
|
|
{ |
|
496
|
|
|
if ($this->pageNumber && $this->perPage) { |
|
497
|
|
|
return [ |
|
498
|
|
|
'start' => ($this->pageNumber * $this->perPage) - $this->perPage, |
|
499
|
|
|
]; |
|
500
|
|
|
} |
|
501
|
|
|
return []; |
|
502
|
|
|
} |
|
503
|
|
|
|
|
504
|
|
|
/** |
|
505
|
|
|
* Tests whether location string follows valid convention (City, ST). |
|
506
|
|
|
* |
|
507
|
|
|
* @param string $location |
|
508
|
|
|
* |
|
509
|
|
|
* @return bool |
|
510
|
|
|
*/ |
|
511
|
8 |
|
private function isValidLocation($location = null) |
|
512
|
|
|
{ |
|
513
|
8 |
|
preg_match("/([^,]+),\s*(\w{2})/", $location, $matches); |
|
514
|
8 |
|
return isset($matches[1]) && isset($matches[2]) ? true : false; |
|
515
|
|
|
} |
|
516
|
|
|
} |
|
517
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: