fractalzombie /
frzb-request-mapper
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace FRZB\Component\RequestMapper\Extractor; |
||
| 6 | |||
| 7 | use FRZB\Component\DependencyInjection\Attribute\AsService; |
||
| 8 | use FRZB\Component\RequestMapper\ClassMapper\ClassMapperInterface as ClassMapper; |
||
| 9 | use FRZB\Component\RequestMapper\Helper\ClassHelper; |
||
| 10 | use JetBrains\PhpStorm\Pure; |
||
| 11 | |||
| 12 | #[AsService] |
||
| 13 | class ParametersExtractor |
||
| 14 | { |
||
| 15 | public function __construct( |
||
| 16 | private readonly ClassMapper $classMapper, |
||
| 17 | 19 | ) { |
|
| 18 | } |
||
| 19 | |||
| 20 | public function extract(string $className, array $payload): array |
||
| 21 | { |
||
| 22 | 19 | return [...$payload, ...$this->mapProperties($this->classMapper->map($className, $payload), $payload)]; |
|
| 23 | } |
||
| 24 | 19 | ||
| 25 | private function mapProperties(array $properties, array $parameters): array |
||
| 26 | { |
||
| 27 | 19 | $mapping = []; |
|
| 28 | |||
| 29 | 19 | foreach ($properties as $propertyName => $propertyType) { |
|
| 30 | 19 | $propertyValue = $parameters[$propertyName] ?? null; |
|
| 31 | 19 | ||
| 32 | 19 | $mapping[$propertyName] = match (true) { |
|
| 33 | 19 | \is_array($propertyType) => array_map(fn (array $parameters) => $this->extract(current($propertyType), $parameters), $propertyValue ?? []), |
|
| 34 | 19 | ClassHelper::isNotBuiltinAndExists($propertyType) => $this->extract($propertyType, $propertyValue ?? []), |
|
| 35 | 19 | ClassHelper::isEnum($propertyType) => $this->mapEnum($propertyType, $propertyValue) ?? $propertyValue, |
|
| 36 | default => $propertyValue, |
||
| 37 | 19 | }; |
|
| 38 | 19 | } |
|
| 39 | |||
| 40 | return $mapping; |
||
| 41 | } |
||
| 42 | |||
| 43 | #[Pure] |
||
| 44 | private function mapEnum(string $enumClassName, mixed $value = null): ?\BackedEnum |
||
|
0 ignored issues
–
show
|
|||
| 45 | { |
||
| 46 | return match (true) { |
||
| 47 | is_subclass_of($enumClassName, \IntBackedEnum::class) && \is_int($value) => $enumClassName::tryFrom($value), |
||
|
0 ignored issues
–
show
The type
IntBackedEnum 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 Loading history...
|
|||
| 48 | is_subclass_of($enumClassName, \StringBackedEnum::class) && \is_string($value) => $enumClassName::tryFrom($value), |
||
|
0 ignored issues
–
show
The type
StringBackedEnum 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 Loading history...
|
|||
| 49 | default => null, |
||
| 50 | }; |
||
| 51 | } |
||
| 52 | } |
||
| 53 |
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