|
1
|
|
|
<?php namespace JobApis\Jobs\Client\Providers; |
|
2
|
|
|
|
|
3
|
|
|
use JobApis\Jobs\Client\Job; |
|
4
|
|
|
|
|
5
|
|
|
class CareerbuilderProvider extends AbstractProvider |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* Returns the standardized job object |
|
9
|
|
|
* |
|
10
|
|
|
* @param array $payload Raw job payload from the API |
|
11
|
|
|
* |
|
12
|
|
|
* @return \JobApis\Jobs\Client\Job |
|
13
|
|
|
*/ |
|
14
|
10 |
|
public function createJobObject($payload = []) |
|
15
|
|
|
{ |
|
16
|
10 |
|
$job = new Job([ |
|
17
|
10 |
|
'description' => $payload['DescriptionTeaser'], |
|
18
|
10 |
|
'employmentType' => $payload['EmploymentType'], |
|
19
|
10 |
|
'title' => $payload['JobTitle'], |
|
20
|
10 |
|
'name' => $payload['JobTitle'], |
|
21
|
10 |
|
'url' => $payload['JobDetailsURL'], |
|
22
|
10 |
|
'educationRequirements' => $payload['EducationRequired'], |
|
23
|
10 |
|
'experienceRequirements' => $payload['ExperienceRequired'], |
|
24
|
10 |
|
'sourceId' => $payload['DID'], |
|
25
|
10 |
|
]); |
|
26
|
|
|
|
|
27
|
10 |
|
$pay = $this->parseSalariesFromString($payload['Pay']); |
|
28
|
|
|
|
|
29
|
10 |
|
$job->setOccupationalCategoryWithCodeAndTitle( |
|
30
|
10 |
|
$payload['OnetCode'], |
|
31
|
10 |
|
$payload['ONetFriendlyTitle'] |
|
32
|
10 |
|
)->setCompany($payload['Company']) |
|
33
|
10 |
|
->setCompanyUrl($payload['CompanyDetailsURL']) |
|
34
|
10 |
|
->setLocation( |
|
35
|
10 |
|
$this->parseLocationElement($payload['City']) |
|
36
|
10 |
|
.', '. |
|
37
|
10 |
|
$this->parseLocationElement($payload['State']) |
|
38
|
10 |
|
) |
|
39
|
10 |
|
->setCity($this->parseLocationElement($payload['City'])) |
|
40
|
10 |
|
->setState($this->parseLocationElement($payload['State'])) |
|
41
|
10 |
|
->setDatePostedAsString($payload['PostedDate']) |
|
42
|
10 |
|
->setCompanyLogo($payload['CompanyImageURL']) |
|
43
|
10 |
|
->setMinimumSalary($pay['min']) |
|
44
|
10 |
|
->setMaximumSalary($pay['max']); |
|
45
|
|
|
|
|
46
|
10 |
|
if (isset($payload['Skills']['Skill'])) { |
|
47
|
10 |
|
$job->setSkills($this->parseSkillSet($payload['Skills']['Skill'])); |
|
48
|
10 |
|
} |
|
49
|
|
|
|
|
50
|
10 |
|
return $job; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Job response object default keys that should be set |
|
55
|
|
|
* |
|
56
|
|
|
* @return string |
|
57
|
|
|
*/ |
|
58
|
4 |
|
public function getDefaultResponseFields() |
|
59
|
|
|
{ |
|
60
|
|
|
return [ |
|
|
|
|
|
|
61
|
4 |
|
'Company', |
|
62
|
4 |
|
'CompanyDetailsURL', |
|
63
|
4 |
|
'DescriptionTeaser', |
|
64
|
4 |
|
'DID', |
|
65
|
4 |
|
'OnetCode', |
|
66
|
4 |
|
'ONetFriendlyTitle', |
|
67
|
4 |
|
'EmploymentType', |
|
68
|
4 |
|
'EducationRequired', |
|
69
|
4 |
|
'ExperienceRequired', |
|
70
|
4 |
|
'JobDetailsURL', |
|
71
|
4 |
|
'Location', |
|
72
|
4 |
|
'City', |
|
73
|
4 |
|
'State', |
|
74
|
4 |
|
'PostedDate', |
|
75
|
4 |
|
'Pay', |
|
76
|
4 |
|
'JobTitle', |
|
77
|
4 |
|
'CompanyImageURL', |
|
78
|
4 |
|
'Skills', |
|
79
|
4 |
|
]; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Get data format |
|
84
|
|
|
* |
|
85
|
|
|
* @return string |
|
86
|
|
|
*/ |
|
87
|
4 |
|
public function getFormat() |
|
88
|
|
|
{ |
|
89
|
4 |
|
return 'xml'; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Get listings path |
|
94
|
|
|
* |
|
95
|
|
|
* @return string |
|
96
|
|
|
*/ |
|
97
|
4 |
|
public function getListingsPath() |
|
98
|
|
|
{ |
|
99
|
4 |
|
return 'Results.JobSearchResult'; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Get min and max salary numbers from string |
|
104
|
|
|
* |
|
105
|
|
|
* @return array |
|
106
|
|
|
*/ |
|
107
|
22 |
|
public function parseSalariesFromString($input = null) |
|
108
|
|
|
{ |
|
109
|
|
|
$salary = [ |
|
110
|
22 |
|
'min' => null, |
|
111
|
|
|
'max' => null |
|
112
|
22 |
|
]; |
|
113
|
|
|
$expressions = [ |
|
114
|
22 |
|
'annualRange' => "/^.\d+k\s-\s.\d+k\/year$/", |
|
115
|
22 |
|
'annualFixed' => "/^.\d+k\/year$/", |
|
116
|
22 |
|
'hourlyRange' => "/^.\d+.\d+\s-\s.\d+.\d+\/hour$/", |
|
117
|
22 |
|
'hourlyFixed' => "/^.\d+.\d+\/hour$/", |
|
118
|
22 |
|
]; |
|
119
|
|
|
|
|
120
|
22 |
|
foreach ($expressions as $key => $expression) { |
|
121
|
22 |
|
if (preg_match($expression, $input)) { |
|
122
|
8 |
|
$method = 'parse'.$key; |
|
123
|
8 |
|
$salary = $this->$method($salary, $input); |
|
124
|
8 |
|
} |
|
125
|
22 |
|
} |
|
126
|
|
|
|
|
127
|
22 |
|
return $salary; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Parse annual salary range from CB API |
|
132
|
|
|
* |
|
133
|
|
|
* @return array |
|
134
|
|
|
*/ |
|
135
|
2 |
|
protected function parseAnnualRange($salary = [], $input = null) |
|
136
|
|
|
{ |
|
137
|
|
|
preg_replace_callback("/(.\d+k)\s.\s(.\d+k)/", function ($matches) use (&$salary) { |
|
138
|
2 |
|
$salary['min'] = str_replace('k', '000', $matches[1]); |
|
139
|
2 |
|
$salary['max'] = str_replace('k', '000', $matches[2]); |
|
140
|
2 |
|
}, $input); |
|
141
|
|
|
|
|
142
|
2 |
|
return $salary; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Parse fixed annual salary from CB API |
|
147
|
|
|
* |
|
148
|
|
|
* @return array |
|
149
|
|
|
*/ |
|
150
|
2 |
|
protected function parseAnnualFixed($salary = [], $input = null) |
|
151
|
|
|
{ |
|
152
|
|
|
preg_replace_callback("/(.\d+k)/", function ($matches) use (&$salary) { |
|
153
|
2 |
|
$salary['min'] = str_replace('k', '000', $matches[1]); |
|
154
|
2 |
|
}, $input); |
|
155
|
|
|
|
|
156
|
2 |
|
return $salary; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Parse hourly payrate range from CB API |
|
161
|
|
|
* |
|
162
|
|
|
* @return array |
|
163
|
|
|
*/ |
|
164
|
2 |
|
protected function parseHourlyRange($salary = [], $input = null) |
|
165
|
|
|
{ |
|
166
|
|
|
preg_replace_callback("/(.\d+.\d+)\s.\s(.\d+.\d+)/", function ($matches) use (&$salary) { |
|
167
|
2 |
|
$salary['min'] = $matches[1]; |
|
168
|
2 |
|
$salary['max'] = $matches[2]; |
|
169
|
2 |
|
}, $input); |
|
170
|
|
|
|
|
171
|
2 |
|
return $salary; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Parse fixed hourly payrate from CB API |
|
176
|
|
|
* |
|
177
|
|
|
* @return array |
|
178
|
|
|
*/ |
|
179
|
|
|
protected function parseHourlyFixed($salary = [], $input = null) |
|
180
|
|
|
{ |
|
181
|
2 |
|
preg_replace_callback("/(.\d+.\d+)/", function ($matches) use (&$salary) { |
|
182
|
2 |
|
$salary['min'] = $matches[1]; |
|
183
|
2 |
|
}, $input); |
|
184
|
|
|
|
|
185
|
2 |
|
return $salary; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Makes sure that city/state is a string |
|
190
|
|
|
* |
|
191
|
|
|
* @param $element mixed |
|
192
|
|
|
* |
|
193
|
|
|
* @return string|null |
|
194
|
|
|
*/ |
|
195
|
10 |
|
protected function parseLocationElement($element) |
|
196
|
|
|
{ |
|
197
|
10 |
|
if (is_string($element)) { |
|
198
|
10 |
|
return $element; |
|
199
|
|
|
} |
|
200
|
2 |
|
return ''; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* Parse skills array into string |
|
205
|
|
|
* |
|
206
|
|
|
* @return array |
|
207
|
|
|
*/ |
|
208
|
10 |
|
protected function parseSkillSet($skills) |
|
209
|
|
|
{ |
|
210
|
10 |
|
if (is_array($skills)) { |
|
211
|
4 |
|
return implode(', ', $skills); |
|
212
|
6 |
|
} elseif (is_string($skills)) { |
|
213
|
2 |
|
return $skills; |
|
214
|
|
|
} |
|
215
|
4 |
|
return null; |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.