1 | <?php |
||||
2 | |||||
3 | declare(strict_types=1); |
||||
4 | |||||
5 | namespace BEAR\Resource; |
||||
6 | |||||
7 | use BEAR\Resource\Annotation\Link; |
||||
8 | use BEAR\Resource\Exception\LinkQueryException; |
||||
9 | use BEAR\Resource\Exception\LinkRelException; |
||||
10 | use BEAR\Resource\Exception\MethodException; |
||||
11 | use BEAR\Resource\Exception\UriException; |
||||
12 | use Override; |
||||
13 | use Ray\Aop\ReflectionMethod; |
||||
14 | |||||
15 | use function array_filter; |
||||
16 | use function array_key_exists; |
||||
17 | use function array_keys; |
||||
18 | use function array_pop; |
||||
19 | use function assert; |
||||
20 | use function count; |
||||
21 | use function is_array; |
||||
22 | use function ucfirst; |
||||
23 | use function uri_template; |
||||
24 | |||||
25 | /** |
||||
26 | * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
||||
27 | * @psalm-import-type Body from Types |
||||
28 | */ |
||||
29 | final class Linker implements LinkerInterface |
||||
30 | { |
||||
31 | /** |
||||
32 | * memory cache for linker |
||||
33 | * |
||||
34 | * @var array<string, mixed> |
||||
35 | */ |
||||
36 | private array $cache = []; |
||||
37 | |||||
38 | public function __construct( |
||||
39 | private readonly InvokerInterface $invoker, |
||||
40 | private readonly FactoryInterface $factory, |
||||
41 | ) { |
||||
42 | } |
||||
43 | |||||
44 | /** |
||||
45 | * {@inheritDoc} |
||||
46 | */ |
||||
47 | #[Override] |
||||
48 | public function invoke(AbstractRequest $request) |
||||
49 | { |
||||
50 | $this->cache = []; |
||||
51 | |||||
52 | return $this->invokeRecursive($request); |
||||
53 | } |
||||
54 | |||||
55 | /** |
||||
56 | * @throws LinkQueryException |
||||
57 | * @throws LinkRelException |
||||
58 | */ |
||||
59 | private function invokeRecursive(AbstractRequest $request): ResourceObject |
||||
60 | { |
||||
61 | $this->invoker->invoke($request); |
||||
62 | $current = clone $request->resourceObject; |
||||
63 | if ($current->code >= Code::BAD_REQUEST) { |
||||
64 | return $current; |
||||
65 | } |
||||
66 | |||||
67 | foreach ($request->links as $link) { |
||||
68 | /** @var array<mixed> $nextBody */ |
||||
69 | $nextBody = $this->annotationLink($link, $current, $request)->body; |
||||
70 | $current = $this->nextLink($link, $current, $nextBody); |
||||
71 | } |
||||
72 | |||||
73 | return $current; |
||||
74 | } |
||||
75 | |||||
76 | /** |
||||
77 | * @param array<mixed> $nextResource |
||||
78 | * |
||||
79 | * @return ResourceObject |
||||
80 | */ |
||||
81 | private function nextLink(LinkType $link, ResourceObject $ro, array $nextResource): ResourceObject |
||||
82 | { |
||||
83 | /** @psalm-suppress MixedAssignment */ |
||||
84 | $nextBody = $nextResource; |
||||
85 | |||||
86 | if ($link->type === LinkType::SELF_LINK) { |
||||
87 | $ro->body = $nextBody; |
||||
88 | |||||
89 | return $ro; |
||||
90 | } |
||||
91 | |||||
92 | if ($link->type === LinkType::NEW_LINK) { |
||||
93 | assert(is_array($ro->body) || $ro->body === null); |
||||
94 | $ro->body[$link->key] = $nextBody; |
||||
95 | |||||
96 | return $ro; |
||||
97 | } |
||||
98 | |||||
99 | // crawl |
||||
100 | return $ro; |
||||
101 | } |
||||
102 | |||||
103 | /** |
||||
104 | * Annotation link |
||||
105 | * |
||||
106 | * @throws MethodException |
||||
107 | * @throws LinkRelException |
||||
108 | * @throws Exception\LinkQueryException |
||||
109 | */ |
||||
110 | private function annotationLink(LinkType $link, ResourceObject $current, AbstractRequest $request): ResourceObject |
||||
111 | { |
||||
112 | if (! is_array($current->body)) { |
||||
113 | throw new Exception\LinkQueryException('Only array is allowed for link in ' . $current::class, 500); |
||||
114 | } |
||||
115 | |||||
116 | $classMethod = 'on' . ucfirst($request->method); |
||||
117 | /** @var list<Link> $annotations */ |
||||
118 | $annotations = (new ReflectionMethod($current::class, $classMethod))->getAnnotations(); |
||||
119 | if ($link->type === LinkType::CRAWL_LINK) { |
||||
120 | return $this->annotationCrawl($annotations, $link, $current); |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
121 | } |
||||
122 | |||||
123 | /* @noinspection ExceptionsAnnotatingAndHandlingInspection */ |
||||
124 | return $this->annotationRel($annotations, $link, $current); |
||||
0 ignored issues
–
show
$annotations of type BEAR\Resource\list is incompatible with the type array expected by parameter $annotations of BEAR\Resource\Linker::annotationRel() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
125 | } |
||||
126 | |||||
127 | /** |
||||
128 | * Annotation link (new, self) |
||||
129 | * |
||||
130 | * @param Link[] $annotations |
||||
131 | * |
||||
132 | * @throws UriException |
||||
133 | * @throws MethodException |
||||
134 | * @throws Exception\LinkQueryException |
||||
135 | * @throws Exception\LinkRelException |
||||
136 | */ |
||||
137 | private function annotationRel(array $annotations, LinkType $link, ResourceObject $current): ResourceObject |
||||
138 | { |
||||
139 | /* @noinspection LoopWhichDoesNotLoopInspection */ |
||||
140 | foreach ($annotations as $annotation) { |
||||
141 | if ($annotation->rel !== $link->key) { |
||||
142 | continue; |
||||
143 | } |
||||
144 | |||||
145 | $uri = uri_template($annotation->href, (array) $current->body); |
||||
146 | $rel = $this->factory->newInstance($uri); |
||||
147 | /* @noinspection UnnecessaryParenthesesInspection */ |
||||
148 | $query = (new Uri($uri))->query; |
||||
149 | $request = new Request($this->invoker, $rel, Request::GET, $query); |
||||
0 ignored issues
–
show
$query of type BEAR\Resource\Query is incompatible with the type array expected by parameter $query of BEAR\Resource\Request::__construct() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
150 | |||||
151 | return $this->invoker->invoke($request); |
||||
152 | } |
||||
153 | |||||
154 | throw new LinkRelException("rel:{$link->key} class:" . $current::class, 500); |
||||
155 | } |
||||
156 | |||||
157 | /** |
||||
158 | * Link annotation crawl |
||||
159 | * |
||||
160 | * @param array<object> $annotations |
||||
161 | * |
||||
162 | * @throws MethodException |
||||
163 | */ |
||||
164 | private function annotationCrawl(array $annotations, LinkType $link, ResourceObject $current): ResourceObject |
||||
165 | { |
||||
166 | $isList = $this->isList($current->body); |
||||
167 | /** @var array<array<string, mixed>> $bodyList */ |
||||
168 | $bodyList = $isList ? (array) $current->body : [$current->body]; |
||||
169 | foreach ($bodyList as &$body) { |
||||
170 | $this->crawl($annotations, $link, $body); |
||||
171 | } |
||||
172 | |||||
173 | unset($body); |
||||
174 | $current->body = $isList ? $bodyList : $bodyList[0]; |
||||
175 | |||||
176 | return $current; |
||||
177 | } |
||||
178 | |||||
179 | /** |
||||
180 | * @param array<object> $annotations |
||||
181 | * @param Body $body |
||||
0 ignored issues
–
show
The type
BEAR\Resource\Body was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||||
182 | * |
||||
183 | * @throws LinkQueryException |
||||
184 | * @throws MethodException |
||||
185 | * @throws LinkRelException |
||||
186 | * @throws UriException |
||||
187 | * |
||||
188 | * @param-out array $body |
||||
189 | */ |
||||
190 | private function crawl(array $annotations, LinkType $link, array &$body): void |
||||
191 | { |
||||
192 | foreach ($annotations as $annotation) { |
||||
193 | if (! $annotation instanceof Link || $annotation->crawl !== $link->key) { |
||||
194 | continue; |
||||
195 | } |
||||
196 | |||||
197 | $uri = uri_template($annotation->href, $body); |
||||
198 | $rel = $this->factory->newInstance($uri); |
||||
199 | /* @noinspection UnnecessaryParenthesesInspection */ |
||||
200 | $query = (new Uri($uri))->query; |
||||
201 | $request = new Request($this->invoker, $rel, Request::GET, $query, [$link], $this); |
||||
0 ignored issues
–
show
$query of type BEAR\Resource\Query is incompatible with the type array expected by parameter $query of BEAR\Resource\Request::__construct() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
202 | $hash = $request->hash(); |
||||
203 | if (array_key_exists($hash, $this->cache)) { |
||||
204 | /** @var array<array<string, scalar|array<mixed>>> $cachedResponse */ |
||||
205 | $cachedResponse = $this->cache[$hash]; |
||||
206 | $body[$annotation->rel] = $cachedResponse; |
||||
207 | continue; |
||||
208 | } |
||||
209 | |||||
210 | $this->cache[$hash] = $body[$annotation->rel] = $this->getResponseBody($request); |
||||
211 | } |
||||
212 | } |
||||
213 | |||||
214 | /** @return array<mixed> */ |
||||
215 | private function getResponseBody(Request $request): array|null |
||||
216 | { |
||||
217 | $body = $this->invokeRecursive($request)->body; |
||||
218 | assert(is_array($body) || $body === null); |
||||
219 | |||||
220 | return $body; |
||||
221 | } |
||||
222 | |||||
223 | private function isList(mixed $value): bool |
||||
224 | { |
||||
225 | assert(is_array($value)); |
||||
226 | /** @var array<array<mixed>|string> $list */ |
||||
227 | $list = $value; |
||||
228 | /** @var array<mixed> $firstRow */ |
||||
229 | $firstRow = array_pop($list); |
||||
230 | /** @var array<string, mixed>|string $firstRow */ |
||||
231 | $keys = array_keys((array) $firstRow); |
||||
232 | $isMultiColumnMultiRowList = $this->isMultiColumnMultiRowList($keys, $list); |
||||
233 | $isMultiColumnList = $this->isMultiColumnList($value, $firstRow); |
||||
234 | $isSingleColumnList = $this->isSingleColumnList($value, $keys, $list); |
||||
235 | |||||
236 | return $isSingleColumnList || $isMultiColumnMultiRowList || $isMultiColumnList; |
||||
237 | } |
||||
238 | |||||
239 | /** |
||||
240 | * @param array<int, int|string> $keys |
||||
241 | * @param array<array<mixed>|string> $list |
||||
242 | */ |
||||
243 | private function isMultiColumnMultiRowList(array $keys, array $list): bool |
||||
244 | { |
||||
245 | if ($keys === [0 => 0]) { |
||||
246 | return false; |
||||
247 | } |
||||
248 | |||||
249 | foreach ($list as $item) { |
||||
250 | if ($keys !== array_keys((array) $item)) { |
||||
251 | return false; |
||||
252 | } |
||||
253 | } |
||||
254 | |||||
255 | return true; |
||||
256 | } |
||||
257 | |||||
258 | /** |
||||
259 | * @param array<int|string, mixed> $value |
||||
260 | * @psalm-param array<string, mixed>|scalar $firstRow |
||||
261 | */ |
||||
262 | private function isMultiColumnList(array $value, mixed $firstRow): bool |
||||
263 | { |
||||
264 | return is_array($firstRow) && array_filter(array_keys($value), 'is_numeric') === array_keys($value); |
||||
265 | } |
||||
266 | |||||
267 | /** |
||||
268 | * @param array<int|string, mixed> $value |
||||
269 | * @param list<array-key> $keys |
||||
0 ignored issues
–
show
The type
BEAR\Resource\list was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||||
270 | * @param array<mixed, mixed> $list |
||||
271 | */ |
||||
272 | private function isSingleColumnList(array $value, array $keys, array $list): bool |
||||
273 | { |
||||
274 | return (count($value) === 1) && $keys === array_keys($list); |
||||
275 | } |
||||
276 | } |
||||
277 |