1 | <?php |
||
17 | final class Linker implements LinkerInterface |
||
18 | { |
||
19 | /** |
||
20 | * @var Reader |
||
21 | */ |
||
22 | private $reader; |
||
23 | |||
24 | /** |
||
25 | * @var InvokerInterface |
||
26 | */ |
||
27 | private $invoker; |
||
28 | |||
29 | /** |
||
30 | * @var FactoryInterface |
||
31 | */ |
||
32 | private $factory; |
||
33 | |||
34 | /** |
||
35 | * memory cache for linker |
||
36 | * |
||
37 | * @var array<string, mixed> |
||
38 | */ |
||
39 | private $cache = []; |
||
40 | 54 | ||
41 | public function __construct( |
||
42 | Reader $reader, |
||
43 | InvokerInterface $invoker, |
||
44 | FactoryInterface $factory |
||
45 | 54 | ) { |
|
46 | 54 | $this->reader = $reader; |
|
47 | 54 | $this->invoker = $invoker; |
|
48 | 54 | $this->factory = $factory; |
|
49 | } |
||
50 | |||
51 | /** |
||
52 | * {@inheritdoc} |
||
53 | * |
||
54 | * @throws LinkQueryException |
||
55 | * @throws \BEAR\Resource\Exception\LinkRelException |
||
56 | 10 | */ |
|
57 | public function invoke(AbstractRequest $request) |
||
58 | 10 | { |
|
59 | 10 | $this->invoker->invoke($request); |
|
60 | 10 | $current = clone $request->resourceObject; |
|
61 | if ($current->code >= Code::BAD_REQUEST) { |
||
62 | 10 | return $current; |
|
63 | 8 | } |
|
64 | |||
65 | foreach ($request->links as $link) { |
||
66 | 8 | /** @var array<mixed> $nextBody */ |
|
67 | $nextBody = $this->annotationLink($link, $current, $request)->body; |
||
68 | $current = $this->nextLink($link, $current, $nextBody); |
||
69 | } |
||
70 | |||
71 | return $current; |
||
72 | 8 | } |
|
73 | |||
74 | 8 | /** |
|
75 | * How next linked resource treated (add ? replace ?) |
||
76 | 8 | * |
|
77 | 2 | * @param mixed|ResourceObject $nextResource |
|
78 | */ |
||
79 | 2 | private function nextLink(LinkType $link, ResourceObject $ro, $nextResource) : ResourceObject |
|
80 | { |
||
81 | /** @var array<mixed> $nextBody */ |
||
82 | 6 | $nextBody = $nextResource instanceof ResourceObject ? $nextResource->body : $nextResource; |
|
83 | 2 | ||
84 | if ($link->type === LinkType::SELF_LINK) { |
||
85 | 2 | $ro->body = $nextBody; |
|
86 | |||
87 | return $ro; |
||
88 | } |
||
89 | 4 | ||
90 | if ($link->type === LinkType::NEW_LINK) { |
||
91 | assert(is_array($ro->body) || $ro->body === null); |
||
92 | $ro->body[$link->key] = $nextBody; |
||
93 | |||
94 | return $ro; |
||
95 | } |
||
96 | |||
97 | // crawl |
||
98 | return $ro; |
||
99 | } |
||
100 | |||
101 | 10 | /** |
|
102 | * Annotation link |
||
103 | 10 | * |
|
104 | 1 | * @throws \BEAR\Resource\Exception\MethodException |
|
105 | * @throws \BEAR\Resource\Exception\LinkRelException |
||
106 | 9 | * @throws Exception\LinkQueryException |
|
107 | 9 | */ |
|
108 | 9 | private function annotationLink(LinkType $link, ResourceObject $current, AbstractRequest $request) : ResourceObject |
|
123 | |||
124 | /** |
||
125 | * Annotation link (new, self) |
||
126 | 5 | * |
|
127 | * @param \BEAR\Resource\Annotation\Link[] $annotations |
||
128 | * |
||
129 | 5 | * @throws \BEAR\Resource\Exception\UriException |
|
130 | 5 | * @throws \BEAR\Resource\Exception\MethodException |
|
131 | 1 | * @throws Exception\LinkQueryException |
|
132 | * @throws Exception\LinkRelException |
||
133 | 4 | */ |
|
134 | 4 | private function annotationRel(array $annotations, LinkType $link, ResourceObject $current) : ResourceObject |
|
152 | 4 | ||
153 | 4 | /** |
|
154 | * Link annotation crawl |
||
155 | 4 | * |
|
156 | * @param array<object> $annotations |
||
157 | 4 | * |
|
158 | 4 | * @throws \BEAR\Resource\Exception\MethodException |
|
159 | */ |
||
160 | 4 | private function annotationCrawl(array $annotations, LinkType $link, ResourceObject $current) : ResourceObject |
|
174 | 3 | ||
175 | /** |
||
176 | 4 | * @param array<object> $annotations |
|
177 | 4 | * @param array<string, mixed> $body |
|
178 | * |
||
179 | 4 | * @param-out array $body |
|
180 | 4 | * |
|
181 | 4 | * @throws \BEAR\Resource\Exception\LinkQueryException |
|
182 | 3 | * @throws \BEAR\Resource\Exception\MethodException |
|
183 | * @throws \BEAR\Resource\Exception\LinkRelException |
||
184 | 3 | * @throws \BEAR\Resource\Exception\UriException |
|
185 | */ |
||
186 | 4 | private function crawl(array $annotations, LinkType $link, array &$body) : void |
|
208 | 4 | ||
209 | 4 | /** |
|
210 | 4 | * @return array<mixed> |
|
211 | */ |
||
212 | private function getResponseBody(Request $request) : ?array |
||
219 | 4 | ||
220 | /** |
||
221 | * @param mixed $value |
||
222 | 4 | */ |
|
223 | private function isList($value) : bool |
||
239 | |||
240 | /** |
||
241 | * @param array<int, int|string> $keys |
||
242 | * @param array<array> $list |
||
243 | */ |
||
244 | private function isMultiColumnMultiRowList(array $keys, array $list) : bool |
||
257 | |||
258 | /** |
||
259 | * @param array<int|string, mixed> $value |
||
260 | * @param mixed $firstRow |
||
261 | */ |
||
262 | private function isMultiColumnList(array $value, $firstRow) : bool |
||
266 | |||
267 | /** |
||
268 | * @param array<int|string, mixed> $value |
||
269 | * @param list<array-key> $keys |
||
270 | * @param array<mixed, mixed> $list |
||
271 | */ |
||
272 | private function isSingleColumnList(array $value, array $keys, array $list) : bool |
||
276 | } |
||
277 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.