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