1
|
|
|
<?php namespace JobApis\Jobs\Client\Providers; |
2
|
|
|
|
3
|
|
|
use JobApis\Jobs\Client\Exceptions\MissingParameterException; |
4
|
|
|
use JobApis\Jobs\Client\Job; |
5
|
|
|
|
6
|
|
|
class CareersInGovProvider extends AbstractProvider |
7
|
|
|
{ |
8
|
|
|
/** |
9
|
|
|
* Returns the standardized job object |
10
|
|
|
* |
11
|
|
|
* @param array $payload |
12
|
|
|
* |
13
|
|
|
* @return \JobApis\Jobs\Client\Job |
14
|
|
|
*/ |
15
|
|
|
public function createJobObject($payload) |
16
|
|
|
{ |
17
|
|
|
$job = new Job([ |
18
|
|
|
'description' => trim($payload['description']), |
19
|
|
|
'name' => $payload['title'], |
20
|
|
|
'title' => $payload['title'], |
21
|
|
|
'url' => $payload['link'], |
22
|
|
|
]); |
23
|
|
|
|
24
|
|
|
// Set date posted |
25
|
|
|
$job->setDatePostedAsString($payload['pubDate']); |
26
|
|
|
|
27
|
|
|
return $job; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Job response object default keys that should be set |
32
|
|
|
* |
33
|
|
|
* @return array |
34
|
|
|
*/ |
35
|
|
|
public function getDefaultResponseFields() |
36
|
|
|
{ |
37
|
|
|
return [ |
|
|
|
|
38
|
|
|
'description', |
39
|
|
|
'link', |
40
|
|
|
'pubDate', |
41
|
|
|
'title', |
42
|
|
|
]; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Get format |
47
|
|
|
* |
48
|
|
|
* @return string Currently only 'json' and 'xml' supported |
49
|
|
|
*/ |
50
|
|
|
public function getFormat() |
51
|
|
|
{ |
52
|
|
|
return 'xml'; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Makes the api call and returns a collection of job objects |
57
|
|
|
* NOTE: This method overwrites the AbstractProvider in order to trim whitespace from the body |
58
|
|
|
* |
59
|
|
|
* @return \JobApis\Jobs\Client\Collection |
60
|
|
|
* @throws MissingParameterException |
61
|
|
|
*/ |
62
|
|
|
public function getJobs() |
63
|
|
|
{ |
64
|
|
|
// Verify that all required query vars are set |
65
|
|
|
if ($this->query->isValid()) { |
66
|
|
|
// Get the response from the client using the query |
67
|
|
|
$response = $this->getClientResponse(); |
68
|
|
|
// Get the response body as a string |
69
|
|
|
$body = trim((string) $response->getBody()); |
70
|
|
|
// Parse the string |
71
|
|
|
$payload = $this->parseAsFormat($body, $this->getFormat()); |
72
|
|
|
// Gets listings if they're nested |
73
|
|
|
$listings = is_array($payload) ? $this->getRawListings($payload) : []; |
74
|
|
|
// Return a job collection |
75
|
|
|
return $this->getJobsCollectionFromListings($listings); |
76
|
|
|
} else { |
77
|
|
|
throw new MissingParameterException("All Required parameters for this provider must be set"); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get listings path |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
public function getListingsPath() |
87
|
|
|
{ |
88
|
|
|
return 'channel.item'; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
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_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.