1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AcquiroPay; |
6
|
|
|
|
7
|
|
|
trait MakesHttpRequests |
8
|
|
|
{ |
9
|
|
|
/** @var Api */ |
10
|
|
|
public $api; |
11
|
|
|
|
12
|
|
|
/** @var string */ |
13
|
|
|
public $serviceName; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param string $method |
17
|
|
|
* @param string $uri |
18
|
|
|
* @param array $parameters |
19
|
|
|
* @return array |
20
|
|
|
* @throws Exceptions\ForbiddenException |
21
|
|
|
* @throws Exceptions\NotFoundException |
22
|
|
|
*/ |
23
|
|
|
public function callService(string $method, string $uri, array $parameters = null): array |
24
|
|
|
{ |
25
|
|
|
$data = json_decode(json_encode($this->api->callService($this->serviceName, $method, $uri, $parameters)), true); |
26
|
|
|
|
27
|
|
|
if (!is_array($data)) { |
28
|
|
|
// todo throw error? |
29
|
|
|
return []; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
return $data; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $uri |
37
|
|
|
* |
38
|
|
|
* @param array|null $parameters |
39
|
|
|
* @return mixed |
40
|
|
|
* @throws Exceptions\ForbiddenException |
41
|
|
|
* @throws Exceptions\NotFoundException |
42
|
|
|
*/ |
43
|
|
|
protected function get(string $uri, array $parameters = null): array |
44
|
|
|
{ |
45
|
|
|
return $this->callService('GET', $uri, $parameters); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $uri |
50
|
|
|
* @param array|null $parameters |
51
|
|
|
* @return mixed |
52
|
|
|
* @throws Exceptions\ForbiddenException |
53
|
|
|
* @throws Exceptions\NotFoundException |
54
|
|
|
*/ |
55
|
|
|
protected function post(string $uri, array $parameters = null): array |
56
|
|
|
{ |
57
|
|
|
return $this->callService('POST', $uri, $parameters); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $uri |
62
|
|
|
* @param array|null $parameters |
63
|
|
|
* @return mixed |
64
|
|
|
* @throws Exceptions\ForbiddenException |
65
|
|
|
* @throws Exceptions\NotFoundException |
66
|
|
|
*/ |
67
|
|
|
protected function put(string $uri, array $parameters = null): array |
68
|
|
|
{ |
69
|
|
|
return $this->callService('PUT', $uri, $parameters); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string $uri |
74
|
|
|
* @param array|null $parameters |
75
|
|
|
* @return mixed |
76
|
|
|
* @throws Exceptions\ForbiddenException |
77
|
|
|
* @throws Exceptions\NotFoundException |
78
|
|
|
*/ |
79
|
|
|
protected function delete(string $uri, array $parameters = null): array |
80
|
|
|
{ |
81
|
|
|
return $this->callService('DELETE', $uri, $parameters); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|