bearsunday /
BEAR.Resource
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace BEAR\Resource; |
||
| 6 | |||
| 7 | use BEAR\Resource\Exception\ParameterException; |
||
| 8 | use InvalidArgumentException; |
||
| 9 | use Override; |
||
| 10 | use Ray\Di\InjectorInterface; |
||
| 11 | |||
| 12 | final readonly class NamedParameter implements NamedParameterInterface |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 13 | { |
||
| 14 | public function __construct( |
||
| 15 | private NamedParamMetasInterface $paramMetas, |
||
| 16 | private InjectorInterface $injector, |
||
| 17 | ) { |
||
| 18 | } |
||
| 19 | |||
| 20 | /** |
||
| 21 | * {@inheritDoc} |
||
| 22 | * |
||
| 23 | * @throws ParameterException Thrown when the query parameter is invalid. |
||
| 24 | */ |
||
| 25 | #[Override] |
||
| 26 | public function getParameters(callable $callable, array $query): array |
||
| 27 | { |
||
| 28 | $metas = ($this->paramMetas)($callable); |
||
| 29 | $parameters = []; |
||
| 30 | foreach ($metas as $varName => $param) { |
||
| 31 | |||
| 32 | /** @psalm-suppress MixedAssignment */ |
||
| 33 | try { |
||
| 34 | $parameters[$varName] = $param($varName, $query, $this->injector); |
||
| 35 | } catch (InvalidArgumentException $e) { |
||
| 36 | // handle missing query parameter in Ray.InputQuery |
||
| 37 | throw new ParameterException($varName, $e->getCode(), $e); |
||
| 38 | } |
||
| 39 | } |
||
| 40 | |||
| 41 | return $parameters; |
||
| 42 | } |
||
| 43 | } |
||
| 44 |