1 | <?php |
||
13 | abstract class BaseApi |
||
14 | { |
||
15 | /** |
||
16 | * @var HttpClient |
||
17 | */ |
||
18 | protected $client; |
||
19 | |||
20 | /** |
||
21 | * @param HttpClient $client |
||
22 | */ |
||
23 | 49 | public function __construct(HttpClient $client) |
|
27 | |||
28 | /** |
||
29 | * performs the request. |
||
30 | * |
||
31 | * @param string $url |
||
32 | * @param string $method |
||
33 | * @param array $params |
||
34 | * |
||
35 | * @return array |
||
36 | */ |
||
37 | 36 | protected function request($url, $method = 'GET', array $params = []) |
|
38 | { |
||
39 | 36 | $url = $this->sanitizeQuery($url); |
|
40 | |||
41 | 36 | $response = $this->client->request($method, $url, $params); |
|
42 | |||
43 | 36 | if (is_array($response) && isset($response['paging'])) { |
|
44 | 1 | $response = $this->injectPager($response, $method, $url, $params); |
|
45 | } |
||
46 | |||
47 | 36 | return $response; |
|
48 | } |
||
49 | |||
50 | /** |
||
51 | * converts names to the needed url path format. |
||
52 | * |
||
53 | * @param string $name |
||
54 | * |
||
55 | * @return string |
||
56 | */ |
||
57 | 9 | protected function transform($name) |
|
61 | |||
62 | /** |
||
63 | * removes empty query string parameters. |
||
64 | * |
||
65 | * @param string $query |
||
66 | * |
||
67 | * @return string |
||
68 | */ |
||
69 | 36 | private function sanitizeQuery($query) |
|
97 | |||
98 | /** |
||
99 | * converts the pageable data into a real pager. |
||
100 | * |
||
101 | * @param array $response |
||
102 | * @param string $method |
||
103 | * @param string $url |
||
104 | * @param array $params |
||
105 | * |
||
106 | * @return array |
||
107 | */ |
||
108 | 1 | private function injectPager(array $response, $method, $url, array $params = []) |
|
125 | } |
||
126 |