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 | /** |
||
67 | * {@inheritDoc} |
||
68 | * |
||
69 | * @throws MethodException |
||
70 | */ |
||
71 | #[Override] |
||
72 | public function object(ResourceObject $ro): RequestInterface |
||
73 | { |
||
74 | return new Request($this->invoker, $ro, $this->method); |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * {@inheritDoc} |
||
79 | */ |
||
80 | #[Override] |
||
81 | public function uri($uri): RequestInterface |
||
82 | { |
||
83 | $method = $this->method; // save method, this may change on newInstance(), this is singleton! |
||
84 | $this->method = 'get'; |
||
85 | $ro = $this->newInstance($uri); |
||
86 | $ro->uri->method = $method; |
||
87 | $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 | * {@inheritDoc} |
||
94 | * |
||
95 | * @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 | assert($resourceObject instanceof ResourceObject); |
||
104 | |||
105 | return $resourceObject; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * {@inheritDoc} |
||
110 | */ |
||
111 | #[Override] |
||
112 | public function get(string $uri, array $query = []): ResourceObject |
||
113 | { |
||
114 | return $this->methodUri(Request::GET, $uri)($query); |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * {@inheritDoc} |
||
119 | */ |
||
120 | #[Override] |
||
121 | public function post(string $uri, array $query = []): ResourceObject |
||
122 | { |
||
123 | return $this->methodUri(Request::POST, $uri)($query); |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * {@inheritDoc} |
||
128 | */ |
||
129 | #[Override] |
||
130 | public function put(string $uri, array $query = []): ResourceObject |
||
131 | { |
||
132 | return $this->methodUri(Request::PUT, $uri)($query); |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * {@inheritDoc} |
||
137 | */ |
||
138 | #[Override] |
||
139 | public function patch(string $uri, array $query = []): ResourceObject |
||
140 | { |
||
141 | return $this->methodUri(Request::PATCH, $uri)($query); |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * {@inheritDoc} |
||
146 | */ |
||
147 | #[Override] |
||
148 | public function delete(string $uri, array $query = []): ResourceObject |
||
149 | { |
||
150 | return $this->methodUri(Request::DELETE, $uri)($query); |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * {@inheritDoc} |
||
155 | */ |
||
156 | #[Override] |
||
157 | public function options(string $uri, array $query = []): ResourceObject |
||
158 | { |
||
159 | return $this->methodUri(Request::OPTIONS, $uri)($query); |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * {@inheritDoc} |
||
164 | */ |
||
165 | #[Override] |
||
166 | public function head(string $uri, array $query = []): ResourceObject |
||
167 | { |
||
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 |