1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace seregazhuk\HeadHunterApi; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use GuzzleHttp\Psr7\Response; |
7
|
|
|
use GuzzleHttp\Psr7\Request as GuzzleRequest; |
8
|
|
|
|
9
|
|
|
class Request |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var \GuzzleHttp\Client |
13
|
|
|
*/ |
14
|
|
|
protected $client; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $headers = []; |
20
|
|
|
|
21
|
|
|
public function __construct($baseUrl, $token = null) |
22
|
|
|
{ |
23
|
|
|
$this->client = new Client(['base_uri' => $baseUrl]); |
24
|
|
|
|
25
|
|
|
if($token) $this->setHeaders(['Authorization' => 'Bearer ' . $token]); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param string $uri |
30
|
|
|
* @param array $params |
31
|
|
|
* @return array|null |
32
|
|
|
*/ |
33
|
|
|
public function get($uri, $params = []) |
34
|
|
|
{ |
35
|
|
|
if(!empty($params)){ |
36
|
|
|
$uri .= '?'. http_build_query($params); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return $this->executeRequest('GET', $uri); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $uri |
44
|
|
|
* @param array $params |
45
|
|
|
* @return array|null |
46
|
|
|
*/ |
47
|
|
|
public function post($uri, $params = []) |
48
|
|
|
{ |
49
|
|
|
return $this->executeRequest( |
50
|
|
|
'POST', $uri, ['query' => $params] |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string $uri |
56
|
|
|
* @param array $params |
57
|
|
|
* @return array|null |
58
|
|
|
*/ |
59
|
|
|
public function postJson($uri, $params = []) |
60
|
|
|
{ |
61
|
|
|
return $this->executeRequest( |
62
|
|
|
'POST', $uri, ['json' => $params] |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $uri |
68
|
|
|
* @param array $params |
69
|
|
|
* @return array|null |
70
|
|
|
*/ |
71
|
|
|
public function postFile($uri, $params = []) |
72
|
|
|
{ |
73
|
|
|
return $this->executeRequest( |
74
|
|
|
'POST', $uri, ['multipart' => $params] |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $uri |
80
|
|
|
* @param array $params |
81
|
|
|
* @return array|null |
82
|
|
|
*/ |
83
|
|
|
public function put($uri, $params = []) |
84
|
|
|
{ |
85
|
|
|
return $this->executeRequest( |
86
|
|
|
'PUT', $uri, ['query' => $params] |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $uri |
92
|
|
|
* @param array $params |
93
|
|
|
* @return array|null |
94
|
|
|
*/ |
95
|
|
|
public function putJson($uri, $params = []) |
96
|
|
|
{ |
97
|
|
|
return $this->executeRequest( |
98
|
|
|
'PUT', $uri, ['json' => $params] |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $uri |
104
|
|
|
* @return array|null |
105
|
|
|
*/ |
106
|
|
|
public function delete($uri) |
107
|
|
|
{ |
108
|
|
|
return $this->executeRequest('DELETE', $uri); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param Response $response |
113
|
|
|
* @return array|null |
114
|
|
|
*/ |
115
|
|
|
private function parseResponse(Response $response) |
116
|
|
|
{ |
117
|
|
|
return json_decode($response->getBody(), true); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param string $method |
122
|
|
|
* @param string $uri |
123
|
|
|
* @param array $options |
124
|
|
|
* @return array|null |
125
|
|
|
*/ |
126
|
|
|
protected function executeRequest($method, $uri, array $options = []) |
127
|
|
|
{ |
128
|
|
|
$request = new GuzzleRequest($method, $uri, $this->headers); |
129
|
|
|
|
130
|
|
|
$response = $this->client->send($request, $options); |
131
|
|
|
|
132
|
|
|
return $this->parseResponse($response); |
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param mixed $headers |
137
|
|
|
* @return $this |
138
|
|
|
*/ |
139
|
|
|
public function setHeaders($headers) |
140
|
|
|
{ |
141
|
|
|
$this->headers = $headers; |
|
|
|
|
142
|
|
|
|
143
|
|
|
return $this; |
144
|
|
|
} |
145
|
|
|
} |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.