|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace BEAR\Resource; |
|
6
|
|
|
|
|
7
|
|
|
use BadFunctionCallException; |
|
8
|
|
|
use InvalidArgumentException; |
|
9
|
|
|
use Symfony\Contracts\HttpClient\HttpClientInterface; |
|
10
|
|
|
use Symfony\Contracts\HttpClient\ResponseInterface; |
|
11
|
|
|
|
|
12
|
|
|
use function count; |
|
13
|
|
|
use function is_array; |
|
14
|
|
|
use function strtoupper; |
|
15
|
|
|
use function ucwords; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @method HttpResourceObject get(AbstractUri|string $uri, array $params = []) |
|
19
|
|
|
* @method HttpResourceObject head(AbstractUri|string $uri, array $params = []) |
|
20
|
|
|
* @method HttpResourceObject put(AbstractUri|string $uri, array $params = []) |
|
21
|
|
|
* @method HttpResourceObject post(AbstractUri|string $uri, array $params = []) |
|
22
|
|
|
* @method HttpResourceObject patch(AbstractUri|string $uri, array $params = []) |
|
23
|
|
|
* @method HttpResourceObject delete(AbstractUri|string $uri, array $params = []) |
|
24
|
|
|
* @property-read string $code |
|
25
|
|
|
* @property-read array<string, string> $headers |
|
26
|
|
|
* @property-read array<string, string> $body |
|
27
|
|
|
* @property-read string $view |
|
28
|
|
|
*/ |
|
29
|
|
|
final class HttpResourceObject extends ResourceObject implements InvokeRequestInterface |
|
30
|
|
|
{ |
|
31
|
|
|
/** {@inheritDoc} */ |
|
32
|
|
|
public $body; |
|
33
|
|
|
|
|
34
|
|
|
/** @psalm-suppress PropertyNotSetInConstructor */ |
|
35
|
|
|
private ResponseInterface $response; |
|
36
|
|
|
|
|
37
|
|
|
public function __construct( |
|
38
|
|
|
private HttpClientInterface $client, |
|
39
|
|
|
) { |
|
40
|
|
|
unset($this->code, $this->headers, $this->body, $this->view); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param 'code'|'headers'|'body'|'view'|string $name |
|
|
|
|
|
|
45
|
|
|
* |
|
46
|
|
|
* @return array<int|string, mixed>|int|string |
|
47
|
|
|
*/ |
|
48
|
|
|
public function __get(string $name): array|int|string |
|
49
|
|
|
{ |
|
50
|
|
|
if ($name === 'code') { |
|
51
|
|
|
return $this->response->getStatusCode(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
if ($name === 'headers') { |
|
55
|
|
|
/** @var array<string, array<string>> $headers */ |
|
56
|
|
|
$headers = $this->response->getHeaders(); |
|
57
|
|
|
|
|
58
|
|
|
return $this->formatHeader($headers); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
if ($name === 'body') { |
|
62
|
|
|
return $this->response->toArray(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
if ($name === 'view') { |
|
66
|
|
|
return $this->response->getContent(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
throw new InvalidArgumentException($name); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param array<string, array<string>> $headers |
|
74
|
|
|
* |
|
75
|
|
|
* @return array<string, string|array<string>> |
|
76
|
|
|
*/ |
|
77
|
|
|
private function formatHeader(array $headers): array |
|
78
|
|
|
{ |
|
79
|
|
|
$formated = []; |
|
80
|
|
|
foreach ($headers as $key => $header) { |
|
81
|
|
|
$ucFirstKey = ucwords($key); |
|
82
|
|
|
$formated[$ucFirstKey] = count($header) === 1 ? $header[0] : $header; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return $formated; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function __set(string $name, mixed $value): void |
|
89
|
|
|
{ |
|
90
|
|
|
unset($value); |
|
91
|
|
|
|
|
92
|
|
|
throw new BadFunctionCallException($name); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function __isset(string $name): bool |
|
96
|
|
|
{ |
|
97
|
|
|
return isset($this->{$name}); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function __toString(): string |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->response->getContent(); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function _invokeRequest(InvokerInterface $invoker, AbstractRequest $request): ResourceObject |
|
106
|
|
|
{ |
|
107
|
|
|
unset($invoker); |
|
108
|
|
|
$uri = $request->resourceObject->uri; |
|
109
|
|
|
$method = strtoupper($uri->method); |
|
110
|
|
|
$options = $method === 'GET' ? ['query' => $uri->query] : ['body' => $uri->query]; |
|
111
|
|
|
$clientOptions = isset($uri->query['_options']) && is_array($uri->query['_options']) ? $uri->query['_options'] : []; |
|
112
|
|
|
$options += $clientOptions; |
|
113
|
|
|
$this->response = $this->client->request($method, (string) $uri, $options); |
|
114
|
|
|
|
|
115
|
|
|
return $this; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|