1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Stack Exchange Api Client library. |
5
|
|
|
* |
6
|
|
|
* (c) Beñat Espiña <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace BenatEspina\StackExchangeApiClient\Api; |
15
|
|
|
|
16
|
|
|
use BenatEspina\StackExchangeApiClient\Api\Answer\AcceptAnswer; |
17
|
|
|
use BenatEspina\StackExchangeApiClient\Api\Answer\Answers; |
18
|
|
|
use BenatEspina\StackExchangeApiClient\Api\Answer\AnswersByIds; |
19
|
|
|
use BenatEspina\StackExchangeApiClient\Api\Answer\AnswersOnQuestions; |
20
|
|
|
use BenatEspina\StackExchangeApiClient\Api\Answer\AnswersOnUsers; |
21
|
|
|
use BenatEspina\StackExchangeApiClient\Api\Answer\CreateAnswer; |
22
|
|
|
use BenatEspina\StackExchangeApiClient\Api\Answer\CreateAnswerFlag; |
23
|
|
|
use BenatEspina\StackExchangeApiClient\Api\Answer\DeleteAnswer; |
24
|
|
|
use BenatEspina\StackExchangeApiClient\Api\Answer\DownvoteAnswer; |
25
|
|
|
use BenatEspina\StackExchangeApiClient\Api\Answer\EditAnswer; |
26
|
|
|
use BenatEspina\StackExchangeApiClient\Api\Answer\MeAnswers; |
27
|
|
|
use BenatEspina\StackExchangeApiClient\Api\Answer\MeTagsTopAnswers; |
28
|
|
|
use BenatEspina\StackExchangeApiClient\Api\Answer\RenderAnswer; |
29
|
|
|
use BenatEspina\StackExchangeApiClient\Api\Answer\TopUserAnswersInTags; |
30
|
|
|
use BenatEspina\StackExchangeApiClient\Api\Answer\UndoAcceptAnswer; |
31
|
|
|
use BenatEspina\StackExchangeApiClient\Api\Answer\UndoDownvoteAnswer; |
32
|
|
|
use BenatEspina\StackExchangeApiClient\Api\Answer\UndoUpvoteAnswer; |
33
|
|
|
use BenatEspina\StackExchangeApiClient\Api\Answer\UpvoteAnswer; |
34
|
|
|
use BenatEspina\StackExchangeApiClient\Authentication\Authentication; |
35
|
|
|
use BenatEspina\StackExchangeApiClient\Authentication\AuthenticationIsRequired; |
36
|
|
|
use BenatEspina\StackExchangeApiClient\Http\HttpClient; |
37
|
|
|
use BenatEspina\StackExchangeApiClient\Serializer\Serializer; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @author Beñat Espiña <[email protected]> |
41
|
|
|
*/ |
42
|
|
|
class AnswerApi |
43
|
|
|
{ |
44
|
|
|
public const QUERY_PARAMS = [ |
45
|
|
|
'order' => 'desc', |
46
|
|
|
'sort' => 'activity', |
47
|
|
|
'site' => 'stackoverflow', |
48
|
|
|
'filter' => HttpClient::FILTER_ALL, |
49
|
|
|
]; |
50
|
|
|
|
51
|
|
|
private $client; |
52
|
|
|
private $serializer; |
53
|
|
|
private $authentication; |
54
|
|
|
|
55
|
|
|
public function __construct(HttpClient $client, Serializer $serializer, Authentication $authentication = null) |
56
|
|
|
{ |
57
|
|
|
$this->client = $client; |
58
|
|
|
$this->serializer = $serializer; |
59
|
|
|
$this->authentication = $authentication; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function acceptAnswer(string $id, array $parameters = self::QUERY_PARAMS) |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$this->checkAuthenticationIsEnabled(); |
65
|
|
|
|
66
|
|
|
return (new AcceptAnswer( |
67
|
|
|
$this->client, |
68
|
|
|
$this->serializer, |
69
|
|
|
$this->authentication) |
|
|
|
|
70
|
|
|
)->__invoke($id, $parameters); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function answers(array $parameters = self::QUERY_PARAMS) |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
return (new Answers( |
76
|
|
|
$this->client, |
77
|
|
|
$this->serializer |
78
|
|
|
))->__invoke($parameters); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function answersByIds($ids, array $parameters = self::QUERY_PARAMS) |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
return (new AnswersByIds( |
84
|
|
|
$this->client, |
85
|
|
|
$this->serializer |
86
|
|
|
))->__invoke($ids, $parameters); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function answersOnQuestions($ids, array $parameters = self::QUERY_PARAMS) |
|
|
|
|
90
|
|
|
{ |
91
|
|
|
return (new AnswersOnQuestions( |
92
|
|
|
$this->client, |
93
|
|
|
$this->serializer |
94
|
|
|
))->__invoke($ids, $parameters); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function answersOnUsers($ids, array $parameters = self::QUERY_PARAMS) |
|
|
|
|
98
|
|
|
{ |
99
|
|
|
return (new AnswersOnUsers( |
100
|
|
|
$this->client, |
101
|
|
|
$this->serializer |
102
|
|
|
))->__invoke($ids, $parameters); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function createAnswer(string $questionId, string $body, array $parameters = self::QUERY_PARAMS) |
|
|
|
|
106
|
|
|
{ |
107
|
|
|
$this->checkAuthenticationIsEnabled(); |
108
|
|
|
|
109
|
|
|
return (new CreateAnswer( |
110
|
|
|
$this->client, |
111
|
|
|
$this->serializer, |
112
|
|
|
$this->authentication |
|
|
|
|
113
|
|
|
))->__invoke($questionId, $body, $parameters); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function createAnswerFlag(string $id, array $parameters = self::QUERY_PARAMS) |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
$this->checkAuthenticationIsEnabled(); |
119
|
|
|
|
120
|
|
|
return (new CreateAnswerFlag( |
121
|
|
|
$this->client, |
122
|
|
|
$this->serializer, |
123
|
|
|
$this->authentication |
|
|
|
|
124
|
|
|
))->__invoke($id, $parameters); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function deleteAnswer(string $id, array $parameters = self::QUERY_PARAMS) |
|
|
|
|
128
|
|
|
{ |
129
|
|
|
$this->checkAuthenticationIsEnabled(); |
130
|
|
|
|
131
|
|
|
return (new DeleteAnswer( |
132
|
|
|
$this->client, |
133
|
|
|
$this->serializer, |
134
|
|
|
$this->authentication |
|
|
|
|
135
|
|
|
))->__invoke($id, $parameters); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function downvoteAnswer(string $id, array $parameters = self::QUERY_PARAMS) |
|
|
|
|
139
|
|
|
{ |
140
|
|
|
$this->checkAuthenticationIsEnabled(); |
141
|
|
|
|
142
|
|
|
return (new DownvoteAnswer( |
143
|
|
|
$this->client, |
144
|
|
|
$this->serializer, |
145
|
|
|
$this->authentication |
|
|
|
|
146
|
|
|
))->__invoke($id, $parameters); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function editAnswer(string $id, string $body, array $parameters = self::QUERY_PARAMS) |
|
|
|
|
150
|
|
|
{ |
151
|
|
|
$this->checkAuthenticationIsEnabled(); |
152
|
|
|
|
153
|
|
|
return (new EditAnswer( |
154
|
|
|
$this->client, |
155
|
|
|
$this->serializer, |
156
|
|
|
$this->authentication |
|
|
|
|
157
|
|
|
))->__invoke($id, $body, $parameters); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function meAnswers(array $parameters = self::QUERY_PARAMS) |
|
|
|
|
161
|
|
|
{ |
162
|
|
|
$this->checkAuthenticationIsEnabled(); |
163
|
|
|
|
164
|
|
|
return (new MeAnswers( |
165
|
|
|
$this->client, |
166
|
|
|
$this->serializer, |
167
|
|
|
$this->authentication |
|
|
|
|
168
|
|
|
))->__invoke($parameters); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function meTagsTopAnswers($tags, array $parameters = self::QUERY_PARAMS) |
|
|
|
|
172
|
|
|
{ |
173
|
|
|
$this->checkAuthenticationIsEnabled(); |
174
|
|
|
|
175
|
|
|
return (new MeTagsTopAnswers( |
176
|
|
|
$this->client, |
177
|
|
|
$this->serializer, |
178
|
|
|
$this->authentication |
|
|
|
|
179
|
|
|
))->__invoke($tags, $parameters); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public function renderAnswer(string $id, string $body, array $parameters = self::QUERY_PARAMS) |
|
|
|
|
183
|
|
|
{ |
184
|
|
|
$this->checkAuthenticationIsEnabled(); |
185
|
|
|
|
186
|
|
|
return (new RenderAnswer( |
187
|
|
|
$this->client, |
188
|
|
|
$this->serializer, |
189
|
|
|
$this->authentication |
190
|
|
|
))->__invoke($id, $body, $parameters); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
public function topUserAnswersInTags(string $userId, $tags, array $parameters = self::QUERY_PARAMS) |
|
|
|
|
194
|
|
|
{ |
195
|
|
|
return (new TopUserAnswersInTags( |
196
|
|
|
$this->client, |
197
|
|
|
$this->serializer |
198
|
|
|
))->__invoke($userId, $tags, $parameters); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public function undoAcceptAnswer(string $id, array $parameters = self::QUERY_PARAMS) |
|
|
|
|
202
|
|
|
{ |
203
|
|
|
$this->checkAuthenticationIsEnabled(); |
204
|
|
|
|
205
|
|
|
return (new UndoAcceptAnswer( |
206
|
|
|
$this->client, |
207
|
|
|
$this->serializer, |
208
|
|
|
$this->authentication |
|
|
|
|
209
|
|
|
))->__invoke($id, $parameters); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
public function undoDownvoteAnswer(string $id, array $parameters = self::QUERY_PARAMS) |
|
|
|
|
213
|
|
|
{ |
214
|
|
|
$this->checkAuthenticationIsEnabled(); |
215
|
|
|
|
216
|
|
|
return (new UndoDownvoteAnswer( |
217
|
|
|
$this->client, |
218
|
|
|
$this->serializer, |
219
|
|
|
$this->authentication |
|
|
|
|
220
|
|
|
))->__invoke($id, $parameters); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
public function undoUpvoteAnswer(string $id, array $parameters = self::QUERY_PARAMS) |
|
|
|
|
224
|
|
|
{ |
225
|
|
|
$this->checkAuthenticationIsEnabled(); |
226
|
|
|
|
227
|
|
|
return (new UndoUpvoteAnswer( |
228
|
|
|
$this->client, |
229
|
|
|
$this->serializer, |
230
|
|
|
$this->authentication |
|
|
|
|
231
|
|
|
))->__invoke($id, $parameters); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
public function upvoteAnswer(string $id, array $parameters = self::QUERY_PARAMS) |
|
|
|
|
235
|
|
|
{ |
236
|
|
|
$this->checkAuthenticationIsEnabled(); |
237
|
|
|
|
238
|
|
|
return (new UpvoteAnswer( |
239
|
|
|
$this->client, |
240
|
|
|
$this->serializer, |
241
|
|
|
$this->authentication |
|
|
|
|
242
|
|
|
))->__invoke($id, $parameters); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
private function checkAuthenticationIsEnabled() : void |
246
|
|
|
{ |
247
|
|
|
if (!$this->authentication instanceof Authentication) { |
248
|
|
|
throw new AuthenticationIsRequired(); |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.