1: | <?php |
2: | |
3: | declare(strict_types=1); |
4: | |
5: | |
6: | |
7: | |
8: | |
9: | |
10: | |
11: | namespace NxSys\Library\Clients\Brex\API\Expenses\Runtime\Client; |
12: | |
13: | use Http\Message\MultipartStream\MultipartStreamBuilder; |
14: | use Psr\Http\Message\ResponseInterface; |
15: | use Symfony\Component\OptionsResolver\OptionsResolver; |
16: | use Symfony\Component\Serializer\SerializerInterface; |
17: | |
18: | abstract class BaseEndpoint implements Endpoint |
19: | { |
20: | protected $queryParameters = []; |
21: | protected $headerParameters = []; |
22: | protected $body; |
23: | |
24: | abstract public function getMethod(): string; |
25: | |
26: | abstract public function getBody(SerializerInterface $serializer, $streamFactory = null): array; |
27: | |
28: | abstract public function getUri(): string; |
29: | |
30: | abstract public function getAuthenticationScopes(): array; |
31: | |
32: | abstract protected function transformResponseBody(ResponseInterface $response, SerializerInterface $serializer, ?string $contentType = null); |
33: | |
34: | protected function getExtraHeaders(): array |
35: | { |
36: | return []; |
37: | } |
38: | |
39: | public function getQueryString(): string |
40: | { |
41: | $optionsResolved = $this->getQueryOptionsResolver()->resolve($this->queryParameters); |
42: | $optionsResolved = array_map(function ($value) { |
43: | return null !== $value ? $value : ''; |
44: | }, $optionsResolved); |
45: | |
46: | return http_build_query($optionsResolved, '', '&', PHP_QUERY_RFC3986); |
47: | } |
48: | |
49: | public function getHeaders(array $baseHeaders = []): array |
50: | { |
51: | return array_merge($this->getExtraHeaders(), $baseHeaders, $this->getHeadersOptionsResolver()->resolve($this->headerParameters)); |
52: | } |
53: | |
54: | protected function getQueryOptionsResolver(): OptionsResolver |
55: | { |
56: | return new OptionsResolver(); |
57: | } |
58: | |
59: | protected function getHeadersOptionsResolver(): OptionsResolver |
60: | { |
61: | return new OptionsResolver(); |
62: | } |
63: | |
64: | |
65: | |
66: | protected function getFormBody(): array |
67: | { |
68: | return [['Content-Type' => ['application/x-www-form-urlencoded']], http_build_query($this->getFormOptionsResolver()->resolve($this->formParameters))]; |
69: | } |
70: | |
71: | protected function getMultipartBody($streamFactory = null): array |
72: | { |
73: | $bodyBuilder = new MultipartStreamBuilder($streamFactory); |
74: | $formParameters = $this->getFormOptionsResolver()->resolve($this->formParameters); |
75: | foreach ($formParameters as $key => $value) { |
76: | $bodyBuilder->addResource($key, $value); |
77: | } |
78: | |
79: | return [['Content-Type' => ['multipart/form-data; boundary="' . ($bodyBuilder->getBoundary() . '"')]], $bodyBuilder->build()]; |
80: | } |
81: | |
82: | protected function getFormOptionsResolver(): OptionsResolver |
83: | { |
84: | return new OptionsResolver(); |
85: | } |
86: | |
87: | protected function getSerializedBody(SerializerInterface $serializer): array |
88: | { |
89: | return [['Content-Type' => ['application/json']], $serializer->serialize($this->body, 'json')]; |
90: | } |
91: | } |
92: | |