Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.86% covered (success)
92.86%
13 / 14
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
ArrayToItemList
92.86% covered (success)
92.86%
13 / 14
0.00% covered (danger)
0.00%
0 / 1
3.00
0.00% covered (danger)
0.00%
0 / 1
 convert
92.86% covered (success)
92.86%
13 / 14
0.00% covered (danger)
0.00%
0 / 1
3.00
1<?php
2namespace Apie\StorageMetadata\Converters;
3
4use Apie\Core\Lists\ItemList;
5use Apie\Core\TypeUtils;
6use Apie\Core\Utils\ConverterUtils;
7use Apie\Core\Utils\HashmapUtils;
8use Apie\TypeConverter\ConverterInterface;
9use Apie\TypeConverter\TypeConverter;
10use ReflectionType;
11
12/**
13 * @template T
14 * @implements ConverterInterface<array<int, T>, ItemList<T>>
15 */
16class ArrayToItemList implements ConverterInterface
17{
18    /**
19     * @param array<int, T> $input
20     */
21    public function convert(array $input, ?ReflectionType $wantedType, ?TypeConverter $typeConverter = null): ItemList
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}