Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
92.86% |
13 / 14 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| ArrayToItemHashmap | |
92.86% |
13 / 14 |
|
0.00% |
0 / 1 |
3.00 | |
0.00% |
0 / 1 |
| convert | |
92.86% |
13 / 14 |
|
0.00% |
0 / 1 |
3.00 | |||
| 1 | <?php |
| 2 | namespace Apie\StorageMetadata\Converters; |
| 3 | |
| 4 | use Apie\Core\Lists\ItemHashmap; |
| 5 | use Apie\Core\TypeUtils; |
| 6 | use Apie\Core\Utils\ConverterUtils; |
| 7 | use Apie\Core\Utils\HashmapUtils; |
| 8 | use Apie\TypeConverter\ConverterInterface; |
| 9 | use Apie\TypeConverter\TypeConverter; |
| 10 | use ReflectionType; |
| 11 | |
| 12 | /** |
| 13 | * @template T |
| 14 | * @implements ConverterInterface<array<string, T>, ItemHashmap<T>> |
| 15 | */ |
| 16 | class ArrayToItemHashmap implements ConverterInterface |
| 17 | { |
| 18 | /** |
| 19 | * @param array<string, T> $input |
| 20 | */ |
| 21 | public function convert(array $input, ?ReflectionType $wantedType, ?TypeConverter $typeConverter = null): ItemHashmap |
| 22 | { |
| 23 | $class = ConverterUtils::toReflectionClass($wantedType); |
| 24 | assert(null !== $class); |
| 25 | $className = $class->name; |
| 26 | $arrayType = HashmapUtils::getArrayType($class); |
| 27 | if ($typeConverter !== null) { |
| 28 | $input = array_map( |
| 29 | function ($val) use ($typeConverter, $arrayType) { |
| 30 | if (TypeUtils::matchesType($arrayType, $val)) { |
| 31 | return $val; |
| 32 | } |
| 33 | return $typeConverter->convertTo($val, $arrayType); |
| 34 | }, |
| 35 | $input |
| 36 | ); |
| 37 | } |
| 38 | return new $className($input); |
| 39 | } |
| 40 | } |