|
1
|
|
|
<?php namespace JobApis\Jobs\Client\Providers; |
|
2
|
|
|
|
|
3
|
|
|
use JobApis\Jobs\Client\Job; |
|
4
|
|
|
use JobApis\Jobs\Client\Collection; |
|
5
|
|
|
|
|
6
|
|
|
class GovtProvider extends AbstractProvider |
|
7
|
|
|
{ |
|
8
|
|
|
/** |
|
9
|
|
|
* Takes a job valid for multiple locations and turns it into multiple jobs |
|
10
|
|
|
* |
|
11
|
|
|
* @param array $item |
|
12
|
|
|
* |
|
13
|
|
|
* @return array |
|
14
|
|
|
*/ |
|
15
|
6 |
|
public function createJobArray($item) |
|
16
|
|
|
{ |
|
17
|
6 |
|
$jobs = []; |
|
18
|
6 |
|
if (isset($item['locations']) && count($item['locations']) > 1) { |
|
19
|
2 |
|
foreach ($item['locations'] as $location) { |
|
20
|
2 |
|
$item['location'] = $location; |
|
21
|
2 |
|
$jobs[] = $item; |
|
22
|
2 |
|
} |
|
23
|
2 |
|
} else { |
|
24
|
4 |
|
$item['location'] = $item['locations'][0]; |
|
25
|
4 |
|
$jobs[] = $item; |
|
26
|
|
|
} |
|
27
|
6 |
|
return $jobs; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Returns the standardized job object |
|
32
|
|
|
* |
|
33
|
|
|
* @param array $payload |
|
34
|
|
|
* |
|
35
|
|
|
* @return \JobApis\Jobs\Client\Job |
|
36
|
|
|
*/ |
|
37
|
4 |
|
public function createJobObject($payload) |
|
38
|
|
|
{ |
|
39
|
4 |
|
$job = new Job([ |
|
40
|
4 |
|
'sourceId' => $payload['id'], |
|
41
|
4 |
|
'title' => $payload['position_title'], |
|
42
|
4 |
|
'name' => $payload['position_title'], |
|
43
|
4 |
|
'url' => $payload['url'], |
|
44
|
4 |
|
'location' => $payload['location'], |
|
45
|
4 |
|
'maximumSalary' => $payload['maximum'], |
|
46
|
4 |
|
'startDate' => $payload['start_date'], |
|
47
|
4 |
|
'endDate' => $payload['end_date'], |
|
48
|
4 |
|
]); |
|
49
|
|
|
|
|
50
|
4 |
|
$location = static::parseLocation($payload['location']); |
|
51
|
|
|
|
|
52
|
4 |
|
$job->setCompany($payload['organization_name']) |
|
53
|
4 |
|
->setDatePostedAsString($payload['start_date']) |
|
54
|
4 |
|
->setMinimumSalary($payload['minimum']); |
|
55
|
|
|
|
|
56
|
4 |
|
if (isset($location[0])) { |
|
57
|
4 |
|
$job->setCity($location[0]); |
|
58
|
4 |
|
} |
|
59
|
4 |
|
if (isset($location[1])) { |
|
60
|
4 |
|
$job->setState($location[1]); |
|
61
|
4 |
|
} |
|
62
|
|
|
|
|
63
|
4 |
|
return $job; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Job response object default keys that should be set |
|
68
|
|
|
* |
|
69
|
|
|
* @return array |
|
70
|
|
|
*/ |
|
71
|
4 |
|
public function getDefaultResponseFields() |
|
72
|
|
|
{ |
|
73
|
|
|
return [ |
|
|
|
|
|
|
74
|
4 |
|
'id', |
|
75
|
4 |
|
'position_title', |
|
76
|
4 |
|
'organization_name', |
|
77
|
4 |
|
'location', |
|
78
|
4 |
|
'start_date', |
|
79
|
4 |
|
'end_date', |
|
80
|
4 |
|
'url', |
|
81
|
4 |
|
'minimum', |
|
82
|
|
|
'maximum' |
|
83
|
4 |
|
]; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Get listings path |
|
88
|
|
|
* |
|
89
|
|
|
* @return string |
|
90
|
|
|
*/ |
|
91
|
4 |
|
public function getListingsPath() |
|
92
|
|
|
{ |
|
93
|
4 |
|
return ''; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Create and get collection of jobs from given listings |
|
98
|
|
|
* |
|
99
|
|
|
* @param array $listings |
|
100
|
|
|
* |
|
101
|
|
|
* @return Collection |
|
102
|
|
|
*/ |
|
103
|
2 |
|
protected function getJobsCollectionFromListings(array $listings = array()) |
|
104
|
|
|
{ |
|
105
|
2 |
|
$collection = new Collection; |
|
106
|
2 |
|
array_map(function ($item) use ($collection) { |
|
107
|
2 |
|
$jobs = $this->createJobArray($item); |
|
108
|
2 |
|
foreach ($jobs as $item) { |
|
109
|
2 |
|
$item = static::parseAttributeDefaults($item, $this->getDefaultResponseFields()); |
|
110
|
2 |
|
$job = $this->createJobObject($item); |
|
111
|
2 |
|
$job->setQuery($this->query->getKeyword()) |
|
112
|
2 |
|
->setSource($this->getSource()); |
|
113
|
2 |
|
$collection->add($job); |
|
114
|
2 |
|
} |
|
115
|
2 |
|
}, $listings); |
|
116
|
2 |
|
return $collection; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
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.