1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SafeCrow\Api; |
4
|
|
|
|
5
|
|
|
use Psr\Http\Message\ResponseInterface; |
6
|
|
|
use SafeCrow\Client; |
7
|
|
|
use SafeCrow\DataTransformer\DataTransformerInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class AbstractApi |
11
|
|
|
* @package SafeCrow\Api |
12
|
|
|
*/ |
13
|
|
|
class AbstractApi implements ApiInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var Client |
17
|
|
|
*/ |
18
|
|
|
protected $client; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* AbstractApi constructor. |
22
|
|
|
* @param Client $client |
23
|
|
|
*/ |
24
|
|
|
public function __construct(Client $client) |
25
|
|
|
{ |
26
|
|
|
$this->client = $client; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param string $path |
31
|
|
|
* @param array $parameters |
32
|
|
|
* @return ResponseInterface |
33
|
|
|
* @throws \Http\Client\Exception |
34
|
|
|
*/ |
35
|
|
|
protected function get(string $path, array $parameters = []): ResponseInterface |
36
|
|
|
{ |
37
|
|
|
return $this->client->getHttpClient()->get($path.'?'.http_build_query($parameters)); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $path |
42
|
|
|
* @param array $parameters |
43
|
|
|
* @return ResponseInterface |
44
|
|
|
*/ |
45
|
|
|
protected function post(string $path, array $parameters = []): ResponseInterface |
46
|
|
|
{ |
47
|
|
|
return $this->client->getHttpClient()->post($path, [], $parameters ? json_encode($parameters) : null); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $path |
52
|
|
|
* @param array $parameters |
53
|
|
|
* @return ResponseInterface |
54
|
|
|
*/ |
55
|
|
|
protected function path(string $path, array $parameters = []): ResponseInterface |
56
|
|
|
{ |
57
|
|
|
return $this->client->getHttpClient()->patch($path, [], $parameters ? json_encode($parameters) : null); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $path |
62
|
|
|
* @param array $parameters |
63
|
|
|
* @return ResponseInterface |
64
|
|
|
*/ |
65
|
|
|
protected function put(string $path, array $parameters = []): ResponseInterface |
66
|
|
|
{ |
67
|
|
|
return $this->client->getHttpClient()->put($path, [], json_encode($parameters)); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $path |
72
|
|
|
* @param array $parameters |
73
|
|
|
* @return ResponseInterface |
74
|
|
|
*/ |
75
|
|
|
protected function delete(string $path, array $parameters = []): ResponseInterface |
76
|
|
|
{ |
77
|
|
|
return $this->client->getHttpClient()->get($path.'?'.http_build_query($parameters)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param ResponseInterface $response |
82
|
|
|
* @param DataTransformerInterface $dataTransformer |
83
|
|
|
* @return mixed |
84
|
|
|
*/ |
85
|
|
|
protected function getResult(ResponseInterface $response, DataTransformerInterface $dataTransformer) |
86
|
|
|
{ |
87
|
|
|
$result = (array) json_decode($response->getBody(), true); |
88
|
|
|
|
89
|
|
|
return $dataTransformer->transform($result); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param ResponseInterface $response |
94
|
|
|
* @param DataTransformerInterface $dataTransformer |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
|
|
protected function getArrayResult(ResponseInterface $response, DataTransformerInterface $dataTransformer): array |
98
|
|
|
{ |
99
|
|
|
$results = (array) json_decode($response->getBody(), true); |
100
|
|
|
|
101
|
|
|
return array_map(function ($result) use ($dataTransformer) { |
102
|
|
|
return $dataTransformer->transform($result); |
103
|
|
|
}, $results); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param ResponseInterface $response |
108
|
|
|
* @param string $field |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
|
|
protected function getSingleResult(ResponseInterface $response, string $field): string |
112
|
|
|
{ |
113
|
|
|
$result = (array) json_decode($response->getBody(), true); |
114
|
|
|
|
115
|
|
|
return $result[$field]; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|