1
|
|
|
<?php namespace JobApis\Jobs\Client; |
2
|
|
|
|
3
|
|
|
use JobApis\Jobs\Client\Queries\AbstractQuery; |
4
|
|
|
|
5
|
|
|
class JobsMulti |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Job board API query objects |
9
|
|
|
* |
10
|
|
|
* @var AbstractQuery |
11
|
|
|
*/ |
12
|
|
|
protected $queries = []; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Creates query objects for each provider and creates this unified client. |
16
|
|
|
* |
17
|
|
|
* @param array $providers |
18
|
|
|
*/ |
19
|
14 |
|
public function __construct($providers = []) |
20
|
|
|
{ |
21
|
14 |
|
foreach ($providers as $provider => $options) { |
22
|
14 |
|
$query = $provider.'Query'; |
23
|
14 |
|
$className = 'JobApis\\Jobs\\Client\\Queries\\'.$query; |
24
|
14 |
|
$this->addQuery($provider, $className, $options); |
25
|
14 |
|
} |
26
|
14 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Overrides get<Provider>Jobs() methods |
30
|
|
|
* |
31
|
|
|
* @param $method |
32
|
|
|
* @param $args |
33
|
|
|
* |
34
|
|
|
* @return mixed |
35
|
|
|
*/ |
36
|
2 |
|
public function __call($method, $args) |
37
|
|
|
{ |
38
|
2 |
|
if ($this->isGetJobsByProviderMethod($method)) { |
39
|
|
|
return $this->getJobsByProvider($this->getProviderFromMethod($method)); |
40
|
|
|
} |
41
|
|
|
|
42
|
2 |
|
throw new \BadMethodCallException(sprintf( |
43
|
2 |
|
'%s does not contain a method by the name of "%s"', |
44
|
2 |
|
__CLASS__, |
45
|
|
|
$method |
46
|
2 |
|
)); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Instantiates a Query object and adds it to the queries array. |
51
|
|
|
* |
52
|
|
|
* @param $key string Query name |
53
|
|
|
* @param $className string Query class name |
54
|
|
|
* @param $options array Parameters to add to constructor |
55
|
|
|
* |
56
|
|
|
* @return $this |
57
|
|
|
*/ |
58
|
14 |
|
public function addQuery($key, $className, $options = []) |
59
|
|
|
{ |
60
|
14 |
|
$this->queries[$key] = new $className($options); |
61
|
14 |
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Gets jobs from all providers in a single go and returns an array of Collection objects. |
66
|
|
|
* |
67
|
|
|
* @return array |
68
|
|
|
*/ |
69
|
|
|
public function getAllJobs() |
70
|
|
|
{ |
71
|
|
|
$jobs = []; |
72
|
|
|
foreach ($this->queries as $key => $query) { |
|
|
|
|
73
|
|
|
$jobs[$key] = $this->getJobsByProvider($key); |
74
|
|
|
} |
75
|
|
|
return $jobs; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Gets jobs from a single provider and hydrates a new jobs collection. |
80
|
|
|
* |
81
|
|
|
* @return \JobApis\Jobs\Client\Collection |
82
|
|
|
*/ |
83
|
|
|
public function getJobsByProvider($provider) |
84
|
|
|
{ |
85
|
|
|
$providerName = 'JobApis\\Jobs\\Client\\Providers\\'.$provider.'Provider'; |
86
|
|
|
$client = new $providerName($this->queries[$provider]); |
87
|
|
|
return $client->getJobs(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Sets a keyword on the query for each provider. |
92
|
|
|
* |
93
|
|
|
* @param $keyword string |
94
|
|
|
* |
95
|
|
|
* @return $this |
96
|
|
|
*/ |
97
|
2 |
|
public function setKeyword($keyword) |
98
|
|
|
{ |
99
|
2 |
|
foreach ($this->queries as $provider => $query) { |
|
|
|
|
100
|
|
|
switch ($provider) { |
101
|
2 |
|
case 'Careerbuilder': |
102
|
2 |
|
$query->set('Keywords', $keyword); |
103
|
2 |
|
break; |
104
|
2 |
|
case 'Careercast': |
105
|
2 |
|
$query->set('keyword', $keyword); |
106
|
2 |
|
break; |
107
|
2 |
|
case 'Dice': |
108
|
2 |
|
$query->set('text', $keyword); |
109
|
2 |
|
break; |
110
|
2 |
|
case 'Github': |
111
|
2 |
|
$query->set('search', $keyword); |
112
|
2 |
|
break; |
113
|
2 |
|
case 'Govt': |
114
|
2 |
|
$query->set('query', $keyword); |
115
|
2 |
|
break; |
116
|
2 |
|
case 'Indeed': |
117
|
2 |
|
$query->set('q', $keyword); |
118
|
2 |
|
break; |
119
|
2 |
|
case 'Juju': |
120
|
2 |
|
$query->set('k', $keyword); |
121
|
2 |
|
break; |
122
|
2 |
|
case 'Usajobs': |
123
|
2 |
|
$query->set('Keyword', $keyword); |
124
|
2 |
|
break; |
125
|
|
|
default: |
126
|
|
|
throw new \Exception("Provider {$provider} not found"); |
127
|
|
|
} |
128
|
2 |
|
} |
129
|
2 |
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Sets a location on the query for each provider. |
134
|
|
|
* |
135
|
|
|
* @param $location |
136
|
|
|
* |
137
|
|
|
* @return $this |
138
|
|
|
*/ |
139
|
4 |
|
public function setLocation($location) |
140
|
|
|
{ |
141
|
4 |
|
if (!$this->isValidLocation($location)) { |
142
|
2 |
|
throw new \OutOfBoundsException("Location parameter must follow the pattern 'City, ST'."); |
143
|
|
|
} |
144
|
|
|
|
145
|
2 |
|
$locationArr = explode(', ', $location); |
146
|
2 |
|
$city = $locationArr[0]; |
147
|
2 |
|
$state = $locationArr[1]; |
148
|
|
|
|
149
|
2 |
|
foreach ($this->queries as $provider => $query) { |
|
|
|
|
150
|
|
|
switch ($provider) { |
151
|
2 |
|
case 'Careerbuilder': |
152
|
2 |
|
$query->set('UseFacets', 'true'); |
153
|
2 |
|
$query->set('FacetCityState', $location); |
154
|
2 |
|
break; |
155
|
2 |
|
case 'Careercast': |
156
|
2 |
|
$query->set('location', $location); |
157
|
2 |
|
break; |
158
|
2 |
|
case 'Dice': |
159
|
2 |
|
$query->set('city', $city); |
160
|
2 |
|
$query->set('state', $state); |
161
|
2 |
|
break; |
162
|
2 |
|
case 'Github': |
163
|
2 |
|
$query->set('location', $location); |
164
|
2 |
|
break; |
165
|
2 |
|
case 'Govt': |
166
|
2 |
|
$queryString = $query->get('query').' in '.$location; |
167
|
2 |
|
$query->set('query', $queryString); |
168
|
2 |
|
break; |
169
|
2 |
|
case 'Indeed': |
170
|
2 |
|
$query->set('l', $location); |
171
|
2 |
|
break; |
172
|
2 |
|
case 'Juju': |
173
|
2 |
|
$query->set('l', $location); |
174
|
2 |
|
break; |
175
|
2 |
|
case 'Usajobs': |
176
|
2 |
|
$query->set('LocationName', $location); |
177
|
2 |
|
break; |
178
|
|
|
default: |
179
|
|
|
throw new \Exception("Provider {$provider} not found"); |
180
|
|
|
} |
181
|
2 |
|
} |
182
|
2 |
|
return $this; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Sets a page number and number of results per page for each provider. |
187
|
|
|
* |
188
|
|
|
* @param $page integer |
189
|
|
|
* @param $perPage integer |
190
|
|
|
* |
191
|
|
|
* @return $this |
192
|
|
|
*/ |
193
|
2 |
|
public function setPage($page = 1, $perPage = 10) |
194
|
|
|
{ |
195
|
2 |
|
foreach ($this->queries as $provider => $query) { |
|
|
|
|
196
|
|
|
switch ($provider) { |
197
|
2 |
|
case 'Careerbuilder': |
198
|
2 |
|
$query->set('PageNumber', $page); |
199
|
2 |
|
$query->set('PerPage', $perPage); |
200
|
2 |
|
break; |
201
|
2 |
|
case 'Careercast': |
202
|
2 |
|
$query->set('page', $page); |
203
|
2 |
|
$query->set('rows', $perPage); |
204
|
2 |
|
break; |
205
|
2 |
|
case 'Dice': |
206
|
2 |
|
$query->set('page', $page); |
207
|
2 |
|
$query->set('pgcnt', $perPage); |
208
|
2 |
|
break; |
209
|
2 |
|
case 'Github': |
210
|
2 |
|
$query->set('page', $page-1); |
211
|
|
|
// No per_page option |
212
|
2 |
|
break; |
213
|
2 |
|
case 'Govt': |
214
|
2 |
|
$query->set('size', $perPage); |
215
|
2 |
|
$query->set('from', $this->getStartFrom($page, $perPage)); |
216
|
2 |
|
break; |
217
|
2 |
|
case 'Indeed': |
218
|
2 |
|
$query->set('limit', $perPage); |
219
|
2 |
|
$query->set('start', $this->getStartFrom($page, $perPage)); |
220
|
2 |
|
break; |
221
|
2 |
|
case 'Juju': |
222
|
2 |
|
$query->set('page', $page); |
223
|
2 |
|
$query->set('jpp', $perPage); |
224
|
2 |
|
break; |
225
|
2 |
|
case 'Usajobs': |
226
|
2 |
|
$query->set('Page', $page); |
227
|
2 |
|
$query->set('ResultsPerPage', $perPage); |
228
|
2 |
|
break; |
229
|
|
|
default: |
230
|
|
|
throw new \Exception("Provider {$provider} not found"); |
231
|
|
|
} |
232
|
2 |
|
} |
233
|
2 |
|
return $this; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Gets a start from count for APIs that use per_page and start_from pattern. |
238
|
|
|
* |
239
|
|
|
* @param int $page |
240
|
|
|
* @param int $perPage |
241
|
|
|
* |
242
|
|
|
* @return int |
243
|
|
|
*/ |
244
|
2 |
|
private function getStartFrom($page = 1, $perPage = 10) |
245
|
|
|
{ |
246
|
2 |
|
return ($page * $perPage) - $perPage; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Tests whether location string follows valid convention (City, ST). |
251
|
|
|
* |
252
|
|
|
* @param string $location |
253
|
|
|
* |
254
|
|
|
* @return bool |
255
|
|
|
*/ |
256
|
4 |
|
private function isValidLocation($location = null) |
257
|
|
|
{ |
258
|
4 |
|
preg_match("/([^,]+),\s*(\w{2})/", $location, $matches); |
259
|
4 |
|
return isset($matches[1]) && isset($matches[2]) ? true : false; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Tests whether the method is a valid get<Provider>Jobs() method. |
264
|
|
|
* |
265
|
|
|
* @param $method |
266
|
|
|
* |
267
|
|
|
* @return bool |
268
|
|
|
*/ |
269
|
2 |
|
private function isGetJobsByProviderMethod($method) |
270
|
|
|
{ |
271
|
2 |
|
return preg_match('/(get)(.*?)(Jobs)/', $method, $matches) && $matches[2] && isset($this->queries[$matches[2]]); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Get the provider name from the method. |
276
|
|
|
* |
277
|
|
|
* @param $method |
278
|
|
|
* |
279
|
|
|
* @return string |
280
|
|
|
*/ |
281
|
|
|
private function getProviderFromMethod($method) |
282
|
|
|
{ |
283
|
|
|
preg_match('/(get)(.*?)(Jobs)/', $method, $matches); |
284
|
|
|
return $matches[2]; |
285
|
|
|
} |
286
|
|
|
} |
287
|
|
|
|