|
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\Answer; |
|
15
|
|
|
|
|
16
|
|
|
use BenatEspina\StackExchangeApiClient\Api\AnswerApi; |
|
17
|
|
|
use BenatEspina\StackExchangeApiClient\Authentication\Authentication; |
|
18
|
|
|
use BenatEspina\StackExchangeApiClient\Http\HttpClient; |
|
19
|
|
|
use BenatEspina\StackExchangeApiClient\Serializer\Serializer; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* https://api.stackexchange.com/docs/render-answer. |
|
23
|
|
|
* |
|
24
|
|
|
* @author Beñat Espiña <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
class RenderAnswer |
|
27
|
|
|
{ |
|
28
|
|
|
private const URL = '/questions/{id}/answers/render'; |
|
29
|
|
|
|
|
30
|
|
|
private $client; |
|
31
|
|
|
private $serializer; |
|
32
|
|
|
private $authentication; |
|
33
|
|
|
|
|
34
|
|
|
public function __construct(HttpClient $client, Serializer $serializer, ?Authentication $authentication) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->client = $client; |
|
37
|
|
|
$this->serializer = $serializer; |
|
38
|
|
|
$this->authentication = $authentication; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function __invoke(string $id, string $body, array $parameters = AnswerApi::QUERY_PARAMS) |
|
|
|
|
|
|
42
|
|
|
{ |
|
43
|
|
|
return $this->serializer->serialize( |
|
44
|
|
|
$this->client->post( |
|
45
|
|
|
$this->url($id), |
|
46
|
|
|
$this->mergeParameters($body, $parameters) |
|
47
|
|
|
) |
|
48
|
|
|
); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
private function url(string $id) : string |
|
52
|
|
|
{ |
|
53
|
|
|
return str_replace('{id}', $id, self::URL); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
private function mergeParameters(string $body, array $parameters) : array |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->mergeAuthenticationIntoParameters( |
|
59
|
|
|
$this->mergeBodyIntoParameters($body, $parameters) |
|
60
|
|
|
); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
private function mergeBodyIntoParameters(string $body, array $parameters) : array |
|
64
|
|
|
{ |
|
65
|
|
|
return array_merge([$parameters, 'body' => $body]); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
private function mergeAuthenticationIntoParameters(array $parameters) : array |
|
69
|
|
|
{ |
|
70
|
|
|
if ($this->checkAuthenticationIsAvailable()) { |
|
71
|
|
|
return $parameters; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return array_merge($parameters, $this->authentication->toArray()); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
private function checkAuthenticationIsAvailable() : bool |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->authentication instanceof Authentication; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
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
@returnannotation as described here.