1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rs\VersionEye\Api; |
4
|
|
|
|
5
|
|
|
use Rs\VersionEye\Http\HttpClient; |
6
|
|
|
use Rs\VersionEye\Http\Pager; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* BaseApi. |
10
|
|
|
* |
11
|
|
|
* @author Robert Schönthal <[email protected]> |
12
|
|
|
*/ |
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) |
24
|
|
|
{ |
25
|
49 |
|
$this->client = $client; |
26
|
49 |
|
} |
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) |
58
|
|
|
{ |
59
|
9 |
|
return str_replace(['/', '.'], [':', '~'], $name); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* removes empty query string parameters. |
64
|
|
|
* |
65
|
|
|
* @param string $query |
66
|
|
|
* |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
36 |
|
private function sanitizeQuery($query) |
70
|
|
|
{ |
71
|
36 |
|
$parts = parse_url($query); |
72
|
36 |
|
$path = $parts['path']; |
73
|
|
|
|
74
|
36 |
|
if (!isset($parts['query'])) { |
75
|
29 |
|
return $query; |
76
|
|
|
} |
77
|
|
|
|
78
|
7 |
|
$vars = explode('&', $parts['query']); |
79
|
|
|
|
80
|
7 |
|
$final = []; |
81
|
|
|
|
82
|
7 |
|
if (!empty($vars)) { |
83
|
7 |
|
foreach ($vars as $var) { |
84
|
7 |
|
$parts = explode('=', $var); |
85
|
|
|
|
86
|
7 |
|
$key = $parts[0]; |
87
|
7 |
|
$val = $parts[1]; |
88
|
|
|
|
89
|
7 |
|
if (!array_key_exists($key, $final) && !empty($val)) { |
90
|
7 |
|
$final[$key] = $val; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
7 |
|
return $path . '?' . http_build_query($final); |
96
|
|
|
} |
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 = []) |
109
|
|
|
{ |
110
|
1 |
|
while (next($response)) { |
111
|
1 |
|
if ('paging' === key($response)) { |
112
|
1 |
|
prev($response); |
113
|
1 |
|
break; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
1 |
|
$pageableKey = key($response); |
118
|
|
|
|
119
|
1 |
|
$response[$pageableKey] = new Pager($response, $pageableKey, $this->client, $method, $url, $params); |
120
|
|
|
|
121
|
1 |
|
reset($response); |
122
|
|
|
|
123
|
1 |
|
return $response; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|