1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace BEAR\Resource; |
||
6 | |||
7 | use ArrayAccess; |
||
8 | use ArrayIterator; |
||
9 | use BEAR\Resource\Exception\IlligalAccessException; |
||
10 | use Countable; |
||
11 | use IteratorAggregate; |
||
12 | use JsonSerializable; |
||
13 | use Override; |
||
14 | use Ray\Di\Di\Inject; |
||
15 | use ReturnTypeWillChange; |
||
0 ignored issues
–
show
|
|||
16 | use Stringable; |
||
17 | use Throwable; |
||
18 | |||
19 | use function asort; |
||
20 | use function assert; |
||
21 | use function count; |
||
22 | use function is_array; |
||
23 | use function is_countable; |
||
24 | use function is_iterable; |
||
25 | use function is_string; |
||
26 | use function ksort; |
||
27 | use function sprintf; |
||
28 | use function trigger_error; |
||
29 | |||
30 | use const E_USER_WARNING; |
||
31 | |||
32 | /** |
||
33 | * @psalm-import-type Headers from Types |
||
34 | * @phpstan-implements ArrayAccess<string, mixed> |
||
35 | * @phpstan-implements IteratorAggregate<(int|string), mixed> |
||
36 | */ |
||
37 | abstract class ResourceObject implements AcceptTransferInterface, ArrayAccess, Countable, IteratorAggregate, JsonSerializable, Stringable, InvokeRequestInterface |
||
38 | { |
||
39 | /** |
||
40 | * @var AbstractUri |
||
41 | * @psalm-suppress PropertyNotSetInConstructor |
||
42 | */ |
||
43 | public $uri; |
||
44 | |||
45 | /** @var int */ |
||
46 | public $code = 200; |
||
47 | |||
48 | /** @var Headers */ |
||
0 ignored issues
–
show
The type
BEAR\Resource\Headers 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 ![]() |
|||
49 | public $headers = []; |
||
50 | |||
51 | /** |
||
52 | * Resource representation |
||
53 | * |
||
54 | * @var ?string |
||
55 | */ |
||
56 | public $view; |
||
57 | |||
58 | /** @var mixed */ |
||
59 | public $body; |
||
60 | |||
61 | /** @var ?RenderInterface */ |
||
62 | protected $renderer; |
||
63 | |||
64 | /** |
||
65 | * Return representational string |
||
66 | * |
||
67 | * Return object hash if representation renderer is not set. |
||
68 | * |
||
69 | * @return string |
||
70 | */ |
||
71 | #[Override] |
||
72 | public function __toString() |
||
73 | { |
||
74 | try { |
||
75 | $view = $this->toString(); |
||
76 | } catch (Throwable $e) { |
||
77 | $msg = sprintf("%s(%s)\n%s", $e::class, $e->getMessage(), $e->getTraceAsString()); |
||
78 | trigger_error($msg, E_USER_WARNING); |
||
79 | |||
80 | return ''; |
||
81 | } |
||
82 | |||
83 | return $view; |
||
84 | } |
||
85 | |||
86 | /** @return list<string> */ |
||
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 ![]() |
|||
87 | public function __sleep() |
||
88 | { |
||
89 | if (is_array($this->body)) { |
||
90 | /** @psalm-suppress MixedAssignment */ |
||
91 | foreach ($this->body as &$item) { |
||
92 | if (! ($item instanceof RequestInterface)) { |
||
93 | continue; |
||
94 | } |
||
95 | |||
96 | $item = ($item)(); |
||
97 | } |
||
98 | } |
||
99 | |||
100 | return ['uri', 'code', 'headers', 'body', 'view']; |
||
0 ignored issues
–
show
|
|||
101 | } |
||
102 | |||
103 | /** |
||
104 | * Returns the body value at the specified index |
||
105 | * |
||
106 | * @param int|string $offset offset |
||
107 | * |
||
108 | * @return mixed |
||
109 | */ |
||
110 | #[Override] |
||
111 | #[ReturnTypeWillChange] |
||
112 | public function offsetGet($offset) |
||
113 | { |
||
114 | if (is_array($this->body)) { |
||
115 | return $this->body[$offset]; |
||
116 | } |
||
117 | |||
118 | throw new IlligalAccessException((string) $offset); |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * Sets the body value at the specified index to renew |
||
123 | * |
||
124 | * @param array-key $offset offset |
||
0 ignored issues
–
show
|
|||
125 | * @param mixed $value value |
||
126 | * |
||
127 | * @return void |
||
128 | */ |
||
129 | #[Override] |
||
130 | #[ReturnTypeWillChange] |
||
131 | public function offsetSet($offset, mixed $value) |
||
132 | { |
||
133 | if ($this->body === null) { |
||
134 | $this->body = []; |
||
135 | } |
||
136 | |||
137 | if (! is_array($this->body)) { |
||
138 | throw new IlligalAccessException((string) $offset); |
||
139 | } |
||
140 | |||
141 | $this->body[$offset] = $value; |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Returns whether the requested index in body exists |
||
146 | * |
||
147 | * @param array-key $offset offset |
||
0 ignored issues
–
show
|
|||
148 | * |
||
149 | * @return bool |
||
150 | */ |
||
151 | #[Override] |
||
152 | #[ReturnTypeWillChange] |
||
153 | public function offsetExists($offset) |
||
154 | { |
||
155 | if (is_array($this->body)) { |
||
156 | return isset($this->body[$offset]); |
||
157 | } |
||
158 | |||
159 | return false; |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * Set the value at the specified index |
||
164 | * |
||
165 | * @param int|string $offset offset |
||
166 | */ |
||
167 | #[Override] |
||
168 | #[ReturnTypeWillChange] |
||
169 | public function offsetUnset($offset): void |
||
170 | { |
||
171 | assert(is_array($this->body)); |
||
172 | unset($this->body[$offset]); |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * Get the number of public properties in the ArrayObject |
||
177 | */ |
||
178 | #[Override] |
||
179 | public function count(): int |
||
180 | { |
||
181 | if (is_countable($this->body)) { |
||
182 | return count($this->body); |
||
183 | } |
||
184 | |||
185 | throw new IlligalAccessException(); |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * Sort the entries by key |
||
190 | */ |
||
191 | public function ksort(): void |
||
192 | { |
||
193 | if (! is_array($this->body)) { |
||
194 | return; |
||
195 | } |
||
196 | |||
197 | ksort($this->body); |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * Sort the entries by key |
||
202 | */ |
||
203 | public function asort(): void |
||
204 | { |
||
205 | if (! is_array($this->body)) { |
||
206 | return; |
||
207 | } |
||
208 | |||
209 | asort($this->body); |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * @return ArrayIterator<int|string, mixed> |
||
214 | * @psalm-return ArrayIterator<empty, empty>|ArrayIterator<int|string, mixed> |
||
215 | * @phpstan-return ArrayIterator<int|string, mixed> |
||
216 | */ |
||
217 | #[Override] |
||
218 | public function getIterator(): ArrayIterator |
||
219 | { |
||
220 | return is_array($this->body) ? new ArrayIterator($this->body) : new ArrayIterator([]); |
||
221 | } |
||
222 | |||
223 | /** @return self */ |
||
224 | #[Inject(optional: true)] |
||
225 | public function setRenderer(RenderInterface $renderer) |
||
226 | { |
||
227 | $this->renderer = $renderer; |
||
228 | |||
229 | return $this; |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * {@inheritDoc} |
||
234 | */ |
||
235 | public function toString(): string |
||
236 | { |
||
237 | if (is_string($this->view)) { |
||
238 | return $this->view; |
||
239 | } |
||
240 | |||
241 | if (! $this->renderer instanceof RenderInterface) { |
||
242 | $this->renderer = new JsonRenderer(); |
||
243 | } |
||
244 | |||
245 | return $this->renderer->render($this); |
||
246 | } |
||
247 | |||
248 | /** @return array<int|string, mixed> */ |
||
249 | #[Override] |
||
250 | public function jsonSerialize(): array |
||
251 | { |
||
252 | /** @psalm-suppress MixedAssignment */ |
||
253 | if (! is_iterable($this->body)) { |
||
254 | return ['value' => $this->body]; |
||
255 | } |
||
256 | |||
257 | assert(is_array($this->body)); |
||
258 | |||
259 | return $this->body; |
||
260 | } |
||
261 | |||
262 | /** |
||
263 | * {@inheritDoc} |
||
264 | */ |
||
265 | #[Override] |
||
266 | public function transfer(TransferInterface $responder, array $server) |
||
267 | { |
||
268 | $responder($this, $server); |
||
269 | } |
||
270 | |||
271 | public function __clone() |
||
272 | { |
||
273 | $this->uri = clone $this->uri; |
||
274 | } |
||
275 | |||
276 | /** |
||
277 | * {@inheritDoc} |
||
278 | * |
||
279 | * @SuppressWarnings(PHPMD.CamelCaseMethodName) |
||
280 | */ |
||
281 | #[Override] |
||
282 | public function _invokeRequest(InvokerInterface $invoker, AbstractRequest $request): ResourceObject |
||
283 | { |
||
284 | return $invoker->invoke($request); |
||
285 | } |
||
286 | } |
||
287 |
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.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths