1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace seregazhuk\HeadHunterApi; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
7
|
|
|
use GuzzleHttp\Psr7\Request as GuzzleRequest; |
8
|
|
|
|
9
|
|
|
class Request |
10
|
|
|
{ |
11
|
|
|
const BASE_URL = 'https://api.hh.ru/'; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var \GuzzleHttp\Client |
15
|
|
|
*/ |
16
|
|
|
protected $client; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
protected $headers = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $locale = 'RU'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $host = 'hh.ru'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string $token |
35
|
|
|
*/ |
36
|
|
|
public function __construct($token = null) |
37
|
|
|
{ |
38
|
|
|
$this->client = new Client(['base_uri' => self::BASE_URL]); |
39
|
|
|
|
40
|
|
|
if ($token) $this->addAuthHeader($token); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string $uri |
45
|
|
|
* @param array $params |
46
|
|
|
* @return array|null |
47
|
|
|
*/ |
48
|
|
|
public function get($uri, $params = []) |
49
|
|
|
{ |
50
|
|
|
if (!empty($params)) { |
51
|
|
|
$uri .= '?' . $this->makeQueryString($params); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $this->executeRequest('GET', $uri); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $uri |
59
|
|
|
* @param array $params |
60
|
|
|
* @return array|null |
61
|
|
|
*/ |
62
|
|
|
public function post($uri, $params = []) |
63
|
|
|
{ |
64
|
|
|
return $this->executeRequest( |
65
|
|
|
'POST', $uri, ['query' => $params] |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string $uri |
71
|
|
|
* @param array $params |
72
|
|
|
* @return array|null |
73
|
|
|
*/ |
74
|
|
|
public function postJson($uri, $params = []) |
75
|
|
|
{ |
76
|
|
|
return $this->executeRequest( |
77
|
|
|
'POST', $uri, ['json' => $params] |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $uri |
83
|
|
|
* @param array $params |
84
|
|
|
* @return array|null |
85
|
|
|
*/ |
86
|
|
|
public function postFile($uri, $params = []) |
87
|
|
|
{ |
88
|
|
|
return $this->executeRequest( |
89
|
|
|
'POST', $uri, ['multipart' => $params] |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string $uri |
95
|
|
|
* @param array $params |
96
|
|
|
* @return array|null |
97
|
|
|
*/ |
98
|
|
|
public function put($uri, $params = []) |
99
|
|
|
{ |
100
|
|
|
return $this->executeRequest( |
101
|
|
|
'PUT', $uri, ['query' => $params] |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param string $uri |
107
|
|
|
* @param array $params |
108
|
|
|
* @return array|null |
109
|
|
|
*/ |
110
|
|
|
public function putJson($uri, $params = []) |
111
|
|
|
{ |
112
|
|
|
return $this->executeRequest( |
113
|
|
|
'PUT', $uri, ['json' => $params] |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param string $uri |
119
|
|
|
* @return array|null |
120
|
|
|
*/ |
121
|
|
|
public function delete($uri) |
122
|
|
|
{ |
123
|
|
|
return $this->executeRequest('DELETE', $uri); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param ResponseInterface $response |
128
|
|
|
* @return array|null |
129
|
|
|
*/ |
130
|
|
|
protected function parseResponse(ResponseInterface $response) |
131
|
|
|
{ |
132
|
|
|
return json_decode($response->getBody(), true); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param string $method |
137
|
|
|
* @param string $uri |
138
|
|
|
* @param array $options |
139
|
|
|
* @return array|null |
140
|
|
|
*/ |
141
|
|
|
protected function executeRequest($method, $uri, array $options = []) |
142
|
|
|
{ |
143
|
|
|
$request = new GuzzleRequest($method, $uri, $this->headers); |
144
|
|
|
|
145
|
|
|
$response = $this->client->send($request, $options); |
146
|
|
|
|
147
|
|
|
return $this->parseResponse($response); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param string $locale |
152
|
|
|
* @return Request |
153
|
|
|
*/ |
154
|
|
|
public function setLocale($locale) |
155
|
|
|
{ |
156
|
|
|
$this->locale = $locale; |
157
|
|
|
|
158
|
|
|
return $this; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param $params |
163
|
|
|
* @return string |
164
|
|
|
*/ |
165
|
|
|
protected function makeQueryString($params = []) |
166
|
|
|
{ |
167
|
|
|
$customOptions = [ |
168
|
|
|
'host' => $this->host, |
169
|
|
|
'locale' => $this->locale, |
170
|
|
|
]; |
171
|
|
|
|
172
|
|
|
$params = array_merge( |
173
|
|
|
$params, $customOptions |
174
|
|
|
); |
175
|
|
|
|
176
|
|
|
return http_build_query($params); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param string $host |
181
|
|
|
* @return Request |
182
|
|
|
*/ |
183
|
|
|
public function setHost($host) |
184
|
|
|
{ |
185
|
|
|
$this->host = $host; |
186
|
|
|
|
187
|
|
|
return $this; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @param string $token |
192
|
|
|
*/ |
193
|
|
|
protected function addAuthHeader($token) |
194
|
|
|
{ |
195
|
|
|
$this->headers = ['Authorization' => 'Bearer ' . $token]; |
196
|
|
|
} |
197
|
|
|
} |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: