1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Polidog\Esa; |
6
|
|
|
|
7
|
|
|
use Polidog\Esa\Client\Client; |
8
|
|
|
use Polidog\Esa\Client\ClientInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class ApiMethods. |
12
|
|
|
*/ |
13
|
|
|
class Api |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var ClientInterface |
17
|
|
|
*/ |
18
|
|
|
private $client; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
private $currentTeam; |
24
|
|
|
|
25
|
|
|
public function __construct(ClientInterface $client, string $currentTeam) |
26
|
|
|
{ |
27
|
39 |
|
$this->client = $client; |
28
|
|
|
$this->currentTeam = $currentTeam; |
29
|
39 |
|
} |
30
|
39 |
|
|
31
|
39 |
|
public function user(array $params = []): array |
32
|
|
|
{ |
33
|
|
|
return $this->client->request('GET', 'user', [ |
34
|
|
|
'query' => $params, |
35
|
|
|
]); |
36
|
|
|
} |
37
|
|
|
|
38
|
1 |
|
public function teams(): array |
39
|
|
|
{ |
40
|
1 |
|
return $this->client->request('GET', 'teams'); |
41
|
1 |
|
} |
42
|
|
|
|
43
|
|
|
public function team(string $name = null): array |
44
|
|
|
{ |
45
|
|
|
return $this->client->request('GET', "teams/{$name}"); |
46
|
|
|
} |
47
|
|
|
|
48
|
1 |
|
public function stats(): array |
49
|
|
|
{ |
50
|
1 |
|
return $this->client->request('GET', $this->getCurrentTeamUrl('stats')); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function members(): array |
54
|
|
|
{ |
55
|
|
|
return $this->client->request('GET', $this->getCurrentTeamUrl('members')); |
56
|
|
|
} |
57
|
|
|
|
58
|
1 |
|
public function posts(array $params = []): array |
59
|
|
|
{ |
60
|
1 |
|
return $this->client->request('GET', $this->getCurrentTeamUrl('posts'), [ |
61
|
|
|
'query' => $params, |
62
|
|
|
]); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function post(int $number): array |
66
|
1 |
|
{ |
67
|
|
|
return $this->client->request('GET', $this->getCurrentTeamUrl("posts/{$number}")); |
68
|
1 |
|
} |
69
|
|
|
|
70
|
|
|
public function createPost(array $data): array |
71
|
|
|
{ |
72
|
|
|
return $this->client->request('POST', $this->getCurrentTeamUrl('posts'), [ |
73
|
|
|
'json' => [ |
74
|
1 |
|
'post' => $data, |
75
|
|
|
], |
76
|
1 |
|
]); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function updatePost(int $number, array $data): array |
80
|
|
|
{ |
81
|
|
|
return $this->client->request('PATCH', $this->getCurrentTeamUrl("posts/{$number}"), [ |
82
|
|
|
'json' => [ |
83
|
|
|
'post' => $data, |
84
|
1 |
|
], |
85
|
|
|
]); |
86
|
1 |
|
} |
87
|
1 |
|
|
88
|
|
|
public function deletePost(int $number): array |
89
|
|
|
{ |
90
|
|
|
return $this->client->request('DELETE', $this->getCurrentTeamUrl("posts/{$number}")); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function comments(int $number = null, array $params = []): array |
94
|
|
|
{ |
95
|
|
|
if (null === $number) { |
96
|
1 |
|
return $this->client->request('GET', $this->getCurrentTeamUrl('comments'), [ |
97
|
|
|
'query' => $params, |
98
|
1 |
|
]); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $this->client->request('GET', $this->getCurrentTeamUrl("posts/{$number}/comments"), [ |
102
|
|
|
'query' => $params, |
103
|
|
|
]); |
104
|
|
|
} |
105
|
|
|
|
106
|
1 |
|
public function comment(int $commentId, array $params = []): array |
107
|
|
|
{ |
108
|
1 |
|
return $this->client->request('GET', $this->getCurrentTeamUrl("comments/{$commentId}"), [ |
109
|
|
|
'query' => $params, |
110
|
1 |
|
]); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function createComment(int $postNumber, array $data): array |
114
|
|
|
{ |
115
|
|
|
return $this->client->request('POST', $this->getCurrentTeamUrl("posts/{$postNumber}/comments"), [ |
116
|
|
|
'json' => [ |
117
|
|
|
'comment' => $data, |
118
|
|
|
], |
119
|
|
|
]); |
120
|
|
|
} |
121
|
1 |
|
|
122
|
|
|
public function updateComment(int $commentId, array $data): array |
123
|
1 |
|
{ |
124
|
|
|
return $this->client->request('PATCH', $this->getCurrentTeamUrl("comments/{$commentId}"), [ |
125
|
1 |
|
'json' => [ |
126
|
|
|
'comment' => $data, |
127
|
|
|
], |
128
|
|
|
]); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function deleteComment(int $commentId): array |
132
|
|
|
{ |
133
|
|
|
return $this->client->request('DELETE', $this->getCurrentTeamUrl("comments/{$commentId}")); |
134
|
|
|
} |
135
|
1 |
|
|
136
|
|
|
public function createSharing(int $postNumber): array |
137
|
1 |
|
{ |
138
|
|
|
return $this->client->request('POST', $this->getCurrentTeamUrl("posts/{$postNumber}/sharing")); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function deleteSharing(int $postNumber): array |
142
|
|
|
{ |
143
|
|
|
return $this->client->request('DELETE', $this->getCurrentTeamUrl("posts/{$postNumber}/sharing")); |
144
|
|
|
} |
145
|
|
|
|
146
|
2 |
|
public function postStargazers(int $postNumber, array $params = []): array |
147
|
|
|
{ |
148
|
2 |
|
return $this->client->request('GET', $this->getCurrentTeamUrl("posts/{$postNumber}/stargazers"), [ |
149
|
1 |
|
'query' => $params, |
150
|
1 |
|
]); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function addPostStar(int $postNumber, array $params = []): array |
154
|
1 |
|
{ |
155
|
1 |
|
return $this->client->request('POST', $this->getCurrentTeamUrl("posts/{$postNumber}/star"), [ |
156
|
|
|
'json' => $params, |
157
|
|
|
]); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function deletePostStar(int $postNumber): array |
161
|
|
|
{ |
162
|
|
|
return $this->client->request('DELETE', $this->getCurrentTeamUrl("posts/{$postNumber}/star")); |
163
|
|
|
} |
164
|
|
|
|
165
|
1 |
|
public function commentStargazers(int $commentId, array $params = []): array |
166
|
|
|
{ |
167
|
1 |
|
return $this->client->request('GET', $this->getCurrentTeamUrl("comments/{$commentId}/stargazers"), [ |
168
|
1 |
|
'query' => $params, |
169
|
|
|
]); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function addCommentStar(int $commentId, array $params = []): array |
173
|
|
|
{ |
174
|
|
|
return $this->client->request('POST', $this->getCurrentTeamUrl("comments/{$commentId}/star"), [ |
175
|
|
|
'json' => $params, |
176
|
|
|
]); |
177
|
|
|
} |
178
|
1 |
|
|
179
|
|
|
public function deleteCommentStar(int $commentId): array |
180
|
1 |
|
{ |
181
|
|
|
return $this->client->request('DELETE', $this->getCurrentTeamUrl("comments/{$commentId}/star")); |
182
|
1 |
|
} |
183
|
|
|
|
184
|
|
|
public function watchers(int $postNumber, array $params = []): array |
185
|
|
|
{ |
186
|
|
|
return $this->client->request('GET', $this->getCurrentTeamUrl("posts/{$postNumber}/watchers"), [ |
187
|
|
|
'query' => $params, |
188
|
|
|
]); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function addWatch(int $postNumber): array |
192
|
|
|
{ |
193
|
1 |
|
return $this->client->request('POST', $this->getCurrentTeamUrl("posts/{$postNumber}/watch")); |
194
|
|
|
} |
195
|
1 |
|
|
196
|
|
|
public function deleteWatch(int $postNumber): array |
197
|
1 |
|
{ |
198
|
|
|
return $this->client->request('DELETE', $this->getCurrentTeamUrl("posts/{$postNumber}/watch")); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public function categories(): array |
202
|
|
|
{ |
203
|
|
|
return $this->client->request('GET', $this->getCurrentTeamUrl('categories')); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
public function batchMoveCategory(array $params = []): array |
207
|
1 |
|
{ |
208
|
|
|
return $this->client->request('POST', $this->getCurrentTeamUrl('categories/batch_move'), [ |
209
|
1 |
|
'json' => $params, |
210
|
|
|
]); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
public function tags(): array |
214
|
|
|
{ |
215
|
|
|
return $this->client->request('GET', $this->getCurrentTeamUrl('tags')); |
216
|
|
|
} |
217
|
1 |
|
|
218
|
|
|
public function invitation(): array |
219
|
1 |
|
{ |
220
|
|
|
return $this->client->request('GET', $this->getCurrentTeamUrl('invitation')); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
public function regenerateInvitation(): array |
224
|
|
|
{ |
225
|
|
|
return $this->client->request('POST', $this->getCurrentTeamUrl('invitation_regenerator')); |
226
|
|
|
} |
227
|
1 |
|
|
228
|
|
|
public function pendingInvitations(array $params = []): array |
229
|
1 |
|
{ |
230
|
|
|
return $this->client->request('GET', $this->getCurrentTeamUrl('invitations'), [ |
231
|
|
|
'query' => $params, |
232
|
|
|
]); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
public function sendInvitation(array $emails): array |
236
|
|
|
{ |
237
|
|
|
return $this->client->request('POST', $this->getCurrentTeamUrl('invitations'), [ |
238
|
1 |
|
'json' => [ |
239
|
|
|
'members' => ['emails' => $emails], |
240
|
1 |
|
], |
241
|
1 |
|
]); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
public function cancelInvitation(string $code): array |
245
|
|
|
{ |
246
|
|
|
return $this->client->request('DELETE', $this->getCurrentTeamUrl("invitations/{$code}")); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
public function emojis(array $params = []): array |
250
|
|
|
{ |
251
|
1 |
|
return $this->client->request('GET', $this->getCurrentTeamUrl('emojis'), [ |
252
|
|
|
'query' => $params, |
253
|
1 |
|
]); |
254
|
1 |
|
} |
255
|
|
|
|
256
|
|
|
public function createEmoji(array $data): array |
257
|
|
|
{ |
258
|
|
|
return $this->client->request('POST', $this->getCurrentTeamUrl('emojis'), [ |
259
|
|
|
'json' => [ |
260
|
|
|
'emoji' => $data, |
261
|
|
|
], |
262
|
|
|
]); |
263
|
1 |
|
} |
264
|
|
|
|
265
|
1 |
|
public function deleteEmoji(string $code): array |
266
|
|
|
{ |
267
|
|
|
return $this->client->request('DELETE', "teams/{$this->currentTeam}/emojis/{$code}"); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
public static function factory(string $accessToken, string $currentTeam): self |
271
|
|
|
{ |
272
|
|
|
$client = Client::factory($accessToken); |
273
|
|
|
|
274
|
1 |
|
return new self($client, $currentTeam); |
275
|
|
|
} |
276
|
1 |
|
|
277
|
1 |
|
private function getCurrentTeamUrl(string $path = ''): string |
278
|
|
|
{ |
279
|
|
|
return "teams/{$this->currentTeam}/$path"; |
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
|