1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BEAR\Resource; |
6
|
|
|
|
7
|
|
|
use Override; |
8
|
|
|
|
9
|
|
|
use function strtoupper; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @method HttpResourceObject get(AbstractUri|string $uri, array $params = []) |
13
|
|
|
* @method HttpResourceObject head(AbstractUri|string $uri, array $params = []) |
14
|
|
|
* @method HttpResourceObject put(AbstractUri|string $uri, array $params = []) |
15
|
|
|
* @method HttpResourceObject post(AbstractUri|string $uri, array $params = []) |
16
|
|
|
* @method HttpResourceObject patch(AbstractUri|string $uri, array $params = []) |
17
|
|
|
* @method HttpResourceObject delete(AbstractUri|string $uri, array $params = []) |
18
|
|
|
* @property-read string $code |
19
|
|
|
* @property-read array<string, string> $headers |
20
|
|
|
* @property-read array<string, string> $body |
21
|
|
|
* @property-read string $view |
22
|
|
|
* @psalm-import-type Query from Types |
23
|
|
|
*/ |
24
|
|
|
final class HttpResourceObject extends ResourceObject implements InvokeRequestInterface |
25
|
|
|
{ |
26
|
|
|
public function __construct( |
27
|
|
|
private readonly HttpRequestInterface $httpRequest, |
28
|
|
|
) { |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** @SuppressWarnings(PHPMD.CamelCaseMethodName) */ |
32
|
|
|
#[Override] |
33
|
|
|
public function _invokeRequest(InvokerInterface $invoker, AbstractRequest $request): ResourceObject |
34
|
|
|
{ |
35
|
|
|
unset($invoker); |
36
|
|
|
|
37
|
|
|
return $this->request($request); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function request(AbstractRequest $request): ResourceObject |
41
|
|
|
{ |
42
|
|
|
$uri = $request->resourceObject->uri; |
43
|
|
|
$method = strtoupper($uri->method); |
44
|
|
|
[ |
45
|
|
|
'code' => $this->code, |
|
|
|
|
46
|
|
|
'headers' => $this->headers, |
|
|
|
|
47
|
|
|
'body' => $this->body, |
|
|
|
|
48
|
|
|
'view' => $this->view, |
|
|
|
|
49
|
|
|
] = $this->httpRequest->request($method, (string) $uri, $uri->query); |
|
|
|
|
50
|
|
|
|
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function __toString(): string |
55
|
|
|
{ |
56
|
|
|
return $this->view; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|