1
|
|
|
<?php namespace JobApis\Jobs\Client\Queries; |
2
|
|
|
|
3
|
|
|
abstract class AbstractQuery |
4
|
|
|
{ |
5
|
|
|
/** |
6
|
|
|
* Create new query from parameters and defaults |
7
|
|
|
* |
8
|
|
|
* @param array $parameters |
9
|
|
|
*/ |
10
|
28 |
|
public function __construct($parameters = []) |
11
|
|
|
{ |
12
|
28 |
|
$parameters = array_merge($this->defaultAttributes(), $parameters); |
13
|
|
|
|
14
|
28 |
|
foreach ($parameters as $key => $value) { |
15
|
28 |
|
$this->set($key, $value); |
16
|
28 |
|
} |
17
|
28 |
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Get baseUrl |
21
|
|
|
* |
22
|
|
|
* @return string Value of the base url to this api |
23
|
|
|
*/ |
24
|
|
|
abstract public function getBaseUrl(); |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Get keyword |
28
|
|
|
* |
29
|
|
|
* @return string Attribute being used as the search keyword |
30
|
|
|
*/ |
31
|
|
|
abstract public function getKeyword(); |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Attempts to get an attribute by key |
35
|
|
|
* |
36
|
|
|
* @param string $key |
37
|
|
|
* |
38
|
|
|
* @return AbstractQuery |
39
|
|
|
*/ |
40
|
14 |
|
public function get($key) |
41
|
|
|
{ |
42
|
14 |
|
if (!$this->isValidParameter($key)) { |
43
|
2 |
|
throw new \OutOfRangeException(sprintf( |
44
|
2 |
|
'%s does not contain a property by the name of "%s"', |
45
|
2 |
|
__CLASS__, |
46
|
|
|
$key |
47
|
2 |
|
)); |
48
|
|
|
} |
49
|
12 |
|
if (method_exists($this, 'get'.self::toStudlyCase($key))) { |
50
|
6 |
|
return $this->{'get'.self::toStudlyCase($key)}(); |
51
|
|
|
} |
52
|
6 |
|
return $this->$key; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get http method options based on current client. Good for adding POST parameters. |
57
|
|
|
* |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
2 |
|
public function getHttpMethodOptions() |
61
|
|
|
{ |
62
|
2 |
|
return []; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get query string for client based on properties |
67
|
|
|
* |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
4 |
|
public function getQueryString() |
71
|
|
|
{ |
72
|
4 |
|
return '?'.http_build_query($this->getQueryAttributes()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get url |
77
|
|
|
* |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
2 |
|
public function getUrl() |
81
|
|
|
{ |
82
|
2 |
|
return $this->getBaseUrl().$this->getQueryString(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Get http verb to use when making request |
87
|
|
|
* |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
2 |
|
public function getVerb() |
91
|
|
|
{ |
92
|
2 |
|
return 'GET'; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Attempts to update attribute with key/value pair |
97
|
|
|
* |
98
|
|
|
* @param string $key |
99
|
|
|
* @param string $value |
100
|
|
|
* |
101
|
|
|
* @return AbstractQuery |
102
|
|
|
*/ |
103
|
28 |
|
public function set($key, $value) |
104
|
|
|
{ |
105
|
|
|
// Then check to see if there's a query parameter |
106
|
28 |
|
if (!$this->isValidParameter($key)) { |
107
|
2 |
|
throw new \OutOfRangeException(sprintf( |
108
|
2 |
|
'%s does not contain a property by the name of "%s"', |
109
|
2 |
|
__CLASS__, |
110
|
|
|
$key |
111
|
2 |
|
)); |
112
|
|
|
} |
113
|
28 |
|
if (method_exists($this, 'set'.self::toStudlyCase($key))) { |
114
|
2 |
|
return $this->{'set'.self::toStudlyCase($key)}($value); |
115
|
|
|
} |
116
|
28 |
|
$this->$key = $value; |
117
|
28 |
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Determines whether the query is valid and ready to use |
122
|
|
|
* |
123
|
|
|
* @return bool Validity of the Query |
124
|
|
|
*/ |
125
|
4 |
|
public function isValid() |
126
|
|
|
{ |
127
|
4 |
|
foreach ($this->requiredAttributes() as $key) { |
128
|
4 |
|
if (!isset($this->$key)) { |
129
|
2 |
|
return false; |
130
|
|
|
} |
131
|
2 |
|
} |
132
|
2 |
|
return true; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Gets the attributes to use for this API's query |
137
|
|
|
* |
138
|
|
|
* @var array |
139
|
|
|
*/ |
140
|
4 |
|
protected function getQueryAttributes() |
141
|
|
|
{ |
142
|
4 |
|
return get_object_vars($this); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Default parameters |
147
|
|
|
* |
148
|
|
|
* @var array |
149
|
|
|
*/ |
150
|
28 |
|
protected function defaultAttributes() |
151
|
|
|
{ |
152
|
28 |
|
return []; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Attempts to get an attribute by key |
157
|
|
|
* |
158
|
|
|
* @param string $key |
159
|
|
|
* |
160
|
|
|
* @return bool |
161
|
|
|
*/ |
162
|
28 |
|
protected function isValidParameter($key) |
163
|
|
|
{ |
164
|
28 |
|
if (property_exists($this, $key)) { |
165
|
28 |
|
return true; |
166
|
|
|
} |
167
|
4 |
|
return false; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Required attributes for the query |
172
|
|
|
* |
173
|
|
|
* @var array |
174
|
|
|
*/ |
175
|
4 |
|
protected function requiredAttributes() |
176
|
|
|
{ |
177
|
4 |
|
return []; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Converts snake case or underscores to camelcase |
182
|
|
|
* |
183
|
|
|
* @param string |
184
|
|
|
* |
185
|
|
|
* @return string |
186
|
|
|
*/ |
187
|
28 |
|
protected static function toStudlyCase($value) |
188
|
|
|
{ |
189
|
28 |
|
return str_replace(' ', '', ucwords(str_replace(array('-', '_'), ' ', $value))); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|