bearsunday /
BEAR.Resource
| 1 | <?php |
||
| 2 | |||
| 3 | namespace BEAR\Resource; |
||
| 4 | |||
| 5 | use BackedEnum; |
||
| 6 | use BEAR\Resource\Exception\ParameterEnumTypeException; |
||
| 7 | use BEAR\Resource\Exception\ParameterException; |
||
| 8 | use BEAR\Resource\Exception\ParameterInvalidEnumException; |
||
| 9 | use Override; |
||
| 10 | use Ray\Di\InjectorInterface; |
||
| 11 | use ReflectionClass; |
||
| 12 | use ReflectionEnum; |
||
| 13 | use ReflectionNamedType; |
||
| 14 | use ReflectionParameter; |
||
| 15 | use UnitEnum; |
||
| 16 | |||
| 17 | use function assert; |
||
| 18 | use function class_exists; |
||
| 19 | use function enum_exists; |
||
| 20 | use function is_a; |
||
| 21 | use function is_int; |
||
| 22 | use function is_iterable; |
||
| 23 | use function is_string; |
||
| 24 | use function ltrim; |
||
| 25 | use function preg_replace; |
||
| 26 | use function strtolower; |
||
| 27 | |||
| 28 | /** @psalm-import-type Query from Types */ |
||
| 29 | final class ClassParam implements ParamInterface |
||
| 30 | { |
||
| 31 | private readonly string $type; |
||
| 32 | private readonly bool $isDefaultAvailable; |
||
| 33 | private readonly mixed $defaultValue; // @phpstan-ignore-line |
||
| 34 | |||
| 35 | public function __construct( |
||
| 36 | ReflectionNamedType $type, |
||
| 37 | ReflectionParameter $parameter, |
||
| 38 | ) { |
||
| 39 | $this->type = $type->getName(); |
||
| 40 | $this->isDefaultAvailable = $parameter->isDefaultValueAvailable(); |
||
| 41 | if (! $this->isDefaultAvailable) { |
||
| 42 | return; |
||
| 43 | } |
||
| 44 | |||
| 45 | $this->defaultValue = $parameter->getDefaultValue(); |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * {@inheritDoc} |
||
| 50 | */ |
||
| 51 | #[Override] |
||
| 52 | public function __invoke(string $varName, array $query, InjectorInterface $injector) |
||
| 53 | { |
||
| 54 | try { |
||
| 55 | /** @psalm-suppress MixedAssignment */ |
||
| 56 | $props = $this->getProps($varName, $query, $injector); |
||
| 57 | } catch (ParameterException $e) { |
||
| 58 | if ($this->isDefaultAvailable) { |
||
| 59 | return $this->defaultValue; |
||
| 60 | } |
||
| 61 | |||
| 62 | throw $e; |
||
| 63 | } |
||
| 64 | |||
| 65 | assert(class_exists($this->type)); |
||
| 66 | $refClass = new ReflectionClass($this->type); |
||
| 67 | |||
| 68 | if ($refClass->isEnum()) { |
||
| 69 | return $this->enum($this->type, $props, $varName); |
||
| 70 | } |
||
| 71 | |||
| 72 | assert(is_iterable($props)); |
||
| 73 | |||
| 74 | $hasConstructor = (bool) $refClass->getConstructor(); |
||
| 75 | if ($hasConstructor) { |
||
| 76 | /** @psalm-suppress MixedMethodCall */ |
||
| 77 | return new $this->type(...$props); |
||
| 78 | } |
||
| 79 | |||
| 80 | /** @psalm-suppress MixedMethodCall */ |
||
| 81 | $obj = new $this->type(); |
||
| 82 | /** @psalm-suppress MixedAssignment */ |
||
| 83 | foreach ($props as $propName => $propValue) { |
||
| 84 | $obj->{$propName} = $propValue; |
||
| 85 | } |
||
| 86 | |||
| 87 | return $obj; |
||
| 88 | } |
||
| 89 | |||
| 90 | /** @param Query $query */ |
||
| 91 | private function getProps(string $varName, array $query, InjectorInterface $injector): mixed |
||
| 92 | { |
||
| 93 | if (isset($query[$varName])) { |
||
| 94 | return $query[$varName]; |
||
| 95 | } |
||
| 96 | |||
| 97 | // try camelCase variable name |
||
| 98 | $snakeName = ltrim(strtolower((string) preg_replace('/[A-Z]/', '_\0', $varName)), '_'); |
||
| 99 | if (isset($query[$snakeName])) { |
||
| 100 | return $query[$snakeName]; |
||
| 101 | } |
||
| 102 | |||
| 103 | unset($injector); |
||
| 104 | |||
| 105 | throw new ParameterException($varName); |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @param class-string $type |
||
|
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||
| 110 | * |
||
| 111 | * @psalm-suppress MixedArgument |
||
| 112 | */ |
||
| 113 | private function enum(string $type, mixed $props, string $varName): mixed |
||
| 114 | { |
||
| 115 | /** @var class-string<UnitEnum> $type */ |
||
| 116 | $refEnum = new ReflectionEnum($type); |
||
| 117 | assert(enum_exists($type)); |
||
| 118 | |||
| 119 | if (! $refEnum->isBacked()) { |
||
| 120 | throw new NotBackedEnumException($type); |
||
| 121 | } |
||
| 122 | |||
| 123 | assert(is_a($type, BackedEnum::class, true)); |
||
| 124 | if (! (is_int($props) || is_string($props))) { |
||
| 125 | throw new ParameterEnumTypeException($varName); |
||
| 126 | } |
||
| 127 | |||
| 128 | /** @psalm-suppress MixedAssignment */ |
||
| 129 | $value = $type::tryFrom($props); |
||
| 130 | if ($value === null) { |
||
| 131 | throw new ParameterInvalidEnumException($varName); |
||
| 132 | } |
||
| 133 | |||
| 134 | return $value; |
||
| 135 | } |
||
| 136 | } |
||
| 137 |