Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
ArrayToItemSet
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
12
0.00% covered (danger)
0.00%
0 / 1
 convert
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2namespace Apie\StorageMetadata\Converters;
3
4use Apie\Core\Lists\ItemSet;
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>, ItemSet<T>>
15 */
16class ArrayToItemSet implements ConverterInterface
17{
18    /**
19     * @param array<int, T> $input
20     */
21    public function convert(array $input, ?ReflectionType $wantedType, ?TypeConverter $typeConverter = null): ItemSet
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}