1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace SetBased\ClubCollect\Endpoint; |
5
|
|
|
|
6
|
|
|
use SetBased\ClubCollect\ClubCollectApiClient; |
7
|
|
|
use SetBased\ClubCollect\Exception\ClubCollectApiException; |
8
|
|
|
use SetBased\ClubCollect\Resource\BaseResource; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Abstract parent class for all end points. |
12
|
|
|
*/ |
13
|
|
|
abstract class Endpoint |
14
|
|
|
{ |
15
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
16
|
|
|
/** |
17
|
|
|
* The ClubCollect API client. |
18
|
|
|
* |
19
|
|
|
* @var ClubCollectApiClient |
20
|
|
|
*/ |
21
|
|
|
protected $client; |
22
|
|
|
|
23
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
24
|
|
|
/** |
25
|
|
|
* Object constructor. |
26
|
|
|
* |
27
|
|
|
* @param ClubCollectApiClient $client The ClubCollect API client. |
28
|
|
|
*/ |
29
|
|
|
public function __construct(ClubCollectApiClient $client) |
30
|
|
|
{ |
31
|
|
|
$this->client = $client; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
35
|
|
|
/** |
36
|
|
|
* Get the object that is used by this API endpoint. Every API endpoint uses one type of object. |
37
|
|
|
* |
38
|
|
|
* @param array $response The response from the API. |
39
|
|
|
* |
40
|
|
|
* @return BaseResource |
41
|
|
|
*/ |
42
|
|
|
abstract protected function createResourceObject(array $response); |
43
|
|
|
|
44
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
45
|
|
|
/** |
46
|
|
|
* Sends a DELETE request for a single object to the REST API. |
47
|
|
|
* |
48
|
|
|
* @param array $path The parts of the path. |
49
|
|
|
* @param array|null $query The query parameters. A map from key to value. |
50
|
|
|
* |
51
|
|
|
* @throws ClubCollectApiException |
52
|
|
|
*/ |
53
|
|
|
protected function restDelete(array $path, ?array $query = null): void |
54
|
|
|
{ |
55
|
|
|
$this->client->performHttpCall(ClubCollectApiClient::HTTP_DELETE, $path, $query); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
59
|
|
|
/** |
60
|
|
|
* Retrieves a single object from the REST API. |
61
|
|
|
* |
62
|
|
|
* @param array $path The parts of the path. |
63
|
|
|
* @param array|null $query The query parameters. A map from key to value. |
64
|
|
|
* |
65
|
|
|
* @return BaseResource |
66
|
|
|
* |
67
|
|
|
* @throws ClubCollectApiException |
68
|
|
|
*/ |
69
|
|
|
protected function restGet(array $path, ?array $query = null): BaseResource |
70
|
|
|
{ |
71
|
|
|
$result = $this->client->performHttpCall(ClubCollectApiClient::HTTP_GET, $path, $query); |
72
|
|
|
|
73
|
|
|
return $this->createResourceObject($result); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
77
|
|
|
/** |
78
|
|
|
* Retrieves a list of objects from the REST API. |
79
|
|
|
* |
80
|
|
|
* @param string $key The key of the objects in the response. |
81
|
|
|
* @param array $path The parts of the path. |
82
|
|
|
* @param array|null $query The query parameters. A map from key to value. |
83
|
|
|
* |
84
|
|
|
* @return array |
85
|
|
|
* |
86
|
|
|
* @throws ClubCollectApiException |
87
|
|
|
*/ |
88
|
|
|
protected function restGetList(string $key, array $path, ?array $query = null): array |
89
|
|
|
{ |
90
|
|
|
$list = []; |
91
|
|
|
$result = $this->client->performHttpCall(ClubCollectApiClient::HTTP_GET, $path, $query); |
92
|
|
|
foreach ($result[$key] as $import) |
93
|
|
|
{ |
94
|
|
|
$list[] = $this->createResourceObject($import); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $list; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
101
|
|
|
/** |
102
|
|
|
* Retrieves a list of objects from the REST API. |
103
|
|
|
* |
104
|
|
|
* @param string $key The key of the objects in the response. |
105
|
|
|
* @param int|null $from The first page. |
106
|
|
|
* @param int|null $to The last page. |
107
|
|
|
* @param array $path The parts of the path. |
108
|
|
|
* @param array|null $query The query parameters. A map from key to value. |
109
|
|
|
* |
110
|
|
|
* @return array |
111
|
|
|
* |
112
|
|
|
* @throws ClubCollectApiException |
113
|
|
|
*/ |
114
|
|
|
protected function restGetPages(string $key, ?int $from, ?int $to, array $path, ?array $query = null): array |
115
|
|
|
{ |
116
|
|
|
$list = []; |
117
|
|
|
$page = $from ?? 0; |
118
|
|
|
do |
119
|
|
|
{ |
120
|
|
|
++$page; |
121
|
|
|
|
122
|
|
|
$query['page_number'] = $page; |
123
|
|
|
$result = $this->client->performHttpCall(ClubCollectApiClient::HTTP_GET, $path, $query); |
124
|
|
|
|
125
|
|
|
foreach ($result[$key] as $import) |
126
|
|
|
{ |
127
|
|
|
$list[] = $this->createResourceObject($import); |
128
|
|
|
} |
129
|
|
|
} while ($page<($to ?? $result['page']['total_pages'])); |
130
|
|
|
|
131
|
|
|
return $list; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
135
|
|
|
/** |
136
|
|
|
* Creates or updates a single object from the REST API. |
137
|
|
|
* |
138
|
|
|
* @param array $path The parts of the path. |
139
|
|
|
* @param array|null $query The query parameters. A map from key to value. |
140
|
|
|
* @param array|null $body The body parameters. A map from key to value. |
141
|
|
|
* |
142
|
|
|
* @return BaseResource|null |
143
|
|
|
* |
144
|
|
|
* @throws ClubCollectApiException |
145
|
|
|
*/ |
146
|
|
|
protected function restPost(array $path, ?array $query = null, ?array $body = null): ?BaseResource |
147
|
|
|
{ |
148
|
|
|
$result = $this->client->performHttpCall(ClubCollectApiClient::HTTP_POST, $path, $query, $body); |
149
|
|
|
|
150
|
|
|
if ($result===null) return null; |
151
|
|
|
|
152
|
|
|
return $this->createResourceObject($result); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
156
|
|
|
/** |
157
|
|
|
* Updates a single object on the REST API. |
158
|
|
|
* |
159
|
|
|
* @param array $path The parts of the path. |
160
|
|
|
* @param array|null $query The query parameters. A map from key to value. |
161
|
|
|
* @param array|null $body The body parameters. A map from key to value. |
162
|
|
|
* |
163
|
|
|
* @return BaseResource The update resource. |
164
|
|
|
* |
165
|
|
|
* @throws ClubCollectApiException |
166
|
|
|
*/ |
167
|
|
|
protected function restPut(array $path, ?array $query = null, ?array $body = null): BaseResource |
168
|
|
|
{ |
169
|
|
|
$result = $this->client->performHttpCall(ClubCollectApiClient::HTTP_PUT, $path, $query, $body); |
170
|
|
|
|
171
|
|
|
return $this->createResourceObject($result); |
|
|
|
|
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
//---------------------------------------------------------------------------------------------------------------------- |
178
|
|
|
|