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