1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace BEAR\Resource; |
||
6 | |||
7 | use BEAR\Resource\Exception\MethodException; |
||
8 | use Override; |
||
9 | |||
10 | use function assert; |
||
11 | use function is_string; |
||
12 | |||
13 | /** |
||
14 | * @property $this $get |
||
15 | * @property $this $post |
||
16 | * @property $this $put |
||
17 | * @property $this $patch |
||
18 | * @property $this $delete |
||
19 | * @property $this $head |
||
20 | * @property $this $options |
||
21 | * @psalm-import-type Query from Types |
||
22 | */ |
||
23 | final class Resource implements ResourceInterface |
||
24 | { |
||
25 | /** @psalm-suppress PropertyNotSetInConstructor */ |
||
26 | private Request $request; |
||
27 | private string $method = 'get'; |
||
28 | /** @noinspection MoreThanThreeArgumentsInspection */ |
||
29 | |||
30 | /** |
||
31 | * @param FactoryInterface $factory Resource factory |
||
32 | * @param InvokerInterface $invoker Resource request invoker |
||
33 | * @param AnchorInterface $anchor Resource anchor |
||
34 | * @param LinkerInterface $linker Resource linker |
||
35 | * @param UriFactory $uri URI factory |
||
36 | */ |
||
37 | public function __construct( |
||
38 | private readonly FactoryInterface $factory, |
||
39 | private readonly InvokerInterface $invoker, |
||
40 | private readonly AnchorInterface $anchor, |
||
41 | private readonly LinkerInterface $linker, |
||
42 | private readonly UriFactory $uri, |
||
43 | ) { |
||
44 | } |
||
45 | |||
46 | public function __get(string $name): self |
||
47 | { |
||
48 | $this->method = $name; |
||
49 | |||
50 | return $this; |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * {@inheritDoc} |
||
55 | */ |
||
56 | #[Override] |
||
57 | public function newInstance($uri): ResourceObject |
||
58 | { |
||
59 | if (is_string($uri)) { |
||
60 | $uri = ($this->uri)($uri); |
||
61 | } |
||
62 | |||
63 | return $this->factory->newInstance($uri); |
||
64 | } |
||
65 | |||
66 | 47 | /** |
|
67 | * {@inheritDoc} |
||
68 | * |
||
69 | * @throws MethodException |
||
70 | */ |
||
71 | #[Override] |
||
72 | 47 | public function object(ResourceObject $ro): RequestInterface |
|
73 | 47 | { |
|
74 | 47 | return new Request($this->invoker, $ro, $this->method); |
|
75 | 47 | } |
|
76 | 47 | ||
77 | /** |
||
78 | * {@inheritDoc} |
||
79 | */ |
||
80 | #[Override] |
||
81 | public function uri($uri): RequestInterface |
||
82 | { |
||
83 | 34 | $method = $this->method; // save method, this may change on newInstance(), this is singleton! |
|
84 | $this->method = 'get'; |
||
85 | 34 | $ro = $this->newInstance($uri); |
|
86 | $ro->uri->method = $method; |
||
87 | 34 | $this->request = new Request($this->invoker, $ro, $ro->uri->method, $ro->uri->query, [], $this->linker); |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
88 | |||
89 | return $this->request; |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | 40 | * {@inheritDoc} |
|
94 | * |
||
95 | 40 | * @psalm-suppress MixedPropertyFetch |
|
96 | */ |
||
97 | #[Override] |
||
98 | public function href(string $rel, array $query = []): ResourceObject |
||
99 | { |
||
100 | [$method, $uri] = $this->anchor->href($rel, $this->request, $query); |
||
101 | /** @psalm-suppress MixedMethodCall */ |
||
102 | $resourceObject = $this->{$method}->uri($uri)->addQuery($query)->eager->request(); |
||
103 | 1 | assert($resourceObject instanceof ResourceObject); |
|
104 | |||
105 | 1 | return $resourceObject; |
|
106 | } |
||
107 | |||
108 | /** |
||
109 | * {@inheritDoc} |
||
110 | */ |
||
111 | 39 | #[Override] |
|
112 | public function get(string $uri, array $query = []): ResourceObject |
||
113 | 39 | { |
|
114 | 34 | return $this->methodUri(Request::GET, $uri)($query); |
|
115 | } |
||
116 | 39 | ||
117 | 39 | /** |
|
118 | 39 | * {@inheritDoc} |
|
119 | 39 | */ |
|
120 | 39 | #[Override] |
|
121 | public function post(string $uri, array $query = []): ResourceObject |
||
122 | 39 | { |
|
123 | return $this->methodUri(Request::POST, $uri)($query); |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * {@inheritDoc} |
||
128 | 3 | */ |
|
129 | #[Override] |
||
130 | 3 | public function put(string $uri, array $query = []): ResourceObject |
|
131 | { |
||
132 | 3 | return $this->methodUri(Request::PUT, $uri)($query); |
|
133 | } |
||
134 | |||
135 | 1 | /** |
|
136 | * {@inheritDoc} |
||
137 | 1 | */ |
|
138 | #[Override] |
||
139 | 1 | public function patch(string $uri, array $query = []): ResourceObject |
|
140 | { |
||
141 | return $this->methodUri(Request::PATCH, $uri)($query); |
||
142 | 1 | } |
|
143 | |||
144 | 1 | /** |
|
145 | * {@inheritDoc} |
||
146 | 1 | */ |
|
147 | #[Override] |
||
148 | public function delete(string $uri, array $query = []): ResourceObject |
||
149 | 1 | { |
|
150 | return $this->methodUri(Request::DELETE, $uri)($query); |
||
151 | 1 | } |
|
152 | |||
153 | 1 | /** |
|
154 | * {@inheritDoc} |
||
155 | */ |
||
156 | 1 | #[Override] |
|
157 | public function options(string $uri, array $query = []): ResourceObject |
||
158 | 1 | { |
|
159 | return $this->methodUri(Request::OPTIONS, $uri)($query); |
||
160 | 1 | } |
|
161 | |||
162 | /** |
||
163 | 1 | * {@inheritDoc} |
|
164 | */ |
||
165 | 1 | #[Override] |
|
166 | public function head(string $uri, array $query = []): ResourceObject |
||
167 | 1 | { |
|
168 | return $this->methodUri(Request::HEAD, $uri)($query); |
||
169 | } |
||
170 | |||
171 | private function methodUri(string $method, string $uri): RequestInterface |
||
172 | { |
||
173 | $this->method = $method; |
||
174 | |||
175 | return $this->uri($uri); |
||
176 | } |
||
177 | } |
||
178 |