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\MeAnswers; |
25
|
|
|
use BenatEspina\StackExchangeApiClient\Authentication\Authentication; |
26
|
|
|
use BenatEspina\StackExchangeApiClient\Authentication\AuthenticationIsRequired; |
27
|
|
|
use BenatEspina\StackExchangeApiClient\Http\HttpClient; |
28
|
|
|
use BenatEspina\StackExchangeApiClient\Serializer\Serializer; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @author Beñat Espiña <[email protected]> |
32
|
|
|
*/ |
33
|
|
|
class AnswerApi |
34
|
|
|
{ |
35
|
|
|
public const QUERY_PARAMS = [ |
36
|
|
|
'order' => 'desc', |
37
|
|
|
'sort' => 'activity', |
38
|
|
|
'site' => 'stackoverflow', |
39
|
|
|
'filter' => HttpClient::FILTER_ALL, |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
private $client; |
43
|
|
|
private $serializer; |
44
|
|
|
private $authentication; |
45
|
|
|
|
46
|
|
|
public function __construct(HttpClient $client, Serializer $serializer, Authentication $authentication = null) |
47
|
|
|
{ |
48
|
|
|
$this->client = $client; |
49
|
|
|
$this->serializer = $serializer; |
50
|
|
|
$this->authentication = $authentication; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function acceptAnswer(string $id, array $parameters = self::QUERY_PARAMS) |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
$this->checkAuthenticationIsEnabled(); |
56
|
|
|
|
57
|
|
|
return (new AcceptAnswer( |
58
|
|
|
$this->client, |
59
|
|
|
$this->serializer, |
60
|
|
|
$this->authentication) |
|
|
|
|
61
|
|
|
)->__invoke($id, $parameters); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function answers(array $parameters = self::QUERY_PARAMS) |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
return (new Answers( |
67
|
|
|
$this->client, |
68
|
|
|
$this->serializer |
69
|
|
|
))->__invoke($parameters); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function answersByIds($ids, array $parameters = self::QUERY_PARAMS) |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
return (new AnswersByIds( |
75
|
|
|
$this->client, |
76
|
|
|
$this->serializer |
77
|
|
|
))->__invoke($ids, $parameters); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function answersOnQuestions($ids, array $parameters = self::QUERY_PARAMS) |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
return (new AnswersOnQuestions( |
83
|
|
|
$this->client, |
84
|
|
|
$this->serializer |
85
|
|
|
))->__invoke($ids, $parameters); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function answersOnUsers($ids, array $parameters = self::QUERY_PARAMS) |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
return (new AnswersOnUsers( |
91
|
|
|
$this->client, |
92
|
|
|
$this->serializer |
93
|
|
|
))->__invoke($ids, $parameters); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function createAnswer(string $questionId, string $body, array $parameters = self::QUERY_PARAMS) |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
$this->checkAuthenticationIsEnabled(); |
99
|
|
|
|
100
|
|
|
return (new CreateAnswer( |
101
|
|
|
$this->client, |
102
|
|
|
$this->serializer, |
103
|
|
|
$this->authentication |
|
|
|
|
104
|
|
|
))->__invoke($questionId, $body, $parameters); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function createAnswerFlag(string $id, array $parameters = self::QUERY_PARAMS) |
|
|
|
|
108
|
|
|
{ |
109
|
|
|
$this->checkAuthenticationIsEnabled(); |
110
|
|
|
|
111
|
|
|
return (new CreateAnswerFlag( |
112
|
|
|
$this->client, |
113
|
|
|
$this->serializer, |
114
|
|
|
$this->authentication |
|
|
|
|
115
|
|
|
))->__invoke($id, $parameters); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function deleteAnswer(string $id, array $parameters = self::QUERY_PARAMS) |
|
|
|
|
119
|
|
|
{ |
120
|
|
|
$this->checkAuthenticationIsEnabled(); |
121
|
|
|
|
122
|
|
|
return (new DeleteAnswer( |
123
|
|
|
$this->client, |
124
|
|
|
$this->serializer, |
125
|
|
|
$this->authentication |
|
|
|
|
126
|
|
|
))->__invoke($id, $parameters); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function meAnswers(array $parameters = self::QUERY_PARAMS) |
|
|
|
|
130
|
|
|
{ |
131
|
|
|
$this->checkAuthenticationIsEnabled(); |
132
|
|
|
|
133
|
|
|
return (new MeAnswers( |
134
|
|
|
$this->client, |
135
|
|
|
$this->serializer, |
136
|
|
|
$this->authentication |
|
|
|
|
137
|
|
|
))->__invoke($parameters); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
private function checkAuthenticationIsEnabled() : void |
141
|
|
|
{ |
142
|
|
|
if (!$this->authentication instanceof Authentication) { |
143
|
|
|
throw new AuthenticationIsRequired(); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
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.