|
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); |
|
|
|
|
|
|
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
|
|
|
// Dynamic property access via magic __get() returns mixed |
|
102
|
|
|
/** @psalm-suppress MixedMethodCall */ |
|
103
|
1 |
|
$resourceObject = $this->{$method}->uri($uri)->addQuery($query)->eager->request(); // @phpstan-ignore-line |
|
104
|
|
|
assert($resourceObject instanceof ResourceObject); |
|
105
|
1 |
|
|
|
106
|
|
|
return $resourceObject; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* {@inheritDoc} |
|
111
|
39 |
|
*/ |
|
112
|
|
|
#[Override] |
|
113
|
39 |
|
public function get(string $uri, array $query = []): ResourceObject |
|
114
|
34 |
|
{ |
|
115
|
|
|
return $this->methodUri(Request::GET, $uri)($query); |
|
116
|
39 |
|
} |
|
117
|
39 |
|
|
|
118
|
39 |
|
/** |
|
119
|
39 |
|
* {@inheritDoc} |
|
120
|
39 |
|
*/ |
|
121
|
|
|
#[Override] |
|
122
|
39 |
|
public function post(string $uri, array $query = []): ResourceObject |
|
123
|
|
|
{ |
|
124
|
|
|
return $this->methodUri(Request::POST, $uri)($query); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
3 |
|
* {@inheritDoc} |
|
129
|
|
|
*/ |
|
130
|
3 |
|
#[Override] |
|
131
|
|
|
public function put(string $uri, array $query = []): ResourceObject |
|
132
|
3 |
|
{ |
|
133
|
|
|
return $this->methodUri(Request::PUT, $uri)($query); |
|
134
|
|
|
} |
|
135
|
1 |
|
|
|
136
|
|
|
/** |
|
137
|
1 |
|
* {@inheritDoc} |
|
138
|
|
|
*/ |
|
139
|
1 |
|
#[Override] |
|
140
|
|
|
public function patch(string $uri, array $query = []): ResourceObject |
|
141
|
|
|
{ |
|
142
|
1 |
|
return $this->methodUri(Request::PATCH, $uri)($query); |
|
143
|
|
|
} |
|
144
|
1 |
|
|
|
145
|
|
|
/** |
|
146
|
1 |
|
* {@inheritDoc} |
|
147
|
|
|
*/ |
|
148
|
|
|
#[Override] |
|
149
|
1 |
|
public function delete(string $uri, array $query = []): ResourceObject |
|
150
|
|
|
{ |
|
151
|
1 |
|
return $this->methodUri(Request::DELETE, $uri)($query); |
|
152
|
|
|
} |
|
153
|
1 |
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* {@inheritDoc} |
|
156
|
1 |
|
*/ |
|
157
|
|
|
#[Override] |
|
158
|
1 |
|
public function options(string $uri, array $query = []): ResourceObject |
|
159
|
|
|
{ |
|
160
|
1 |
|
return $this->methodUri(Request::OPTIONS, $uri)($query); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
1 |
|
/** |
|
164
|
|
|
* {@inheritDoc} |
|
165
|
1 |
|
*/ |
|
166
|
|
|
#[Override] |
|
167
|
1 |
|
public function head(string $uri, array $query = []): ResourceObject |
|
168
|
|
|
{ |
|
169
|
|
|
return $this->methodUri(Request::HEAD, $uri)($query); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
private function methodUri(string $method, string $uri): RequestInterface |
|
173
|
|
|
{ |
|
174
|
|
|
$this->method = $method; |
|
175
|
|
|
|
|
176
|
|
|
return $this->uri($uri); |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|