Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
63.16% covered (warning)
63.16%
12 / 19
66.67% covered (warning)
66.67%
4 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
MapOfType
63.16% covered (warning)
63.16%
12 / 19
66.67% covered (warning)
66.67%
4 / 6
17.05
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 serialize
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 parseValue
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 parseLiteral
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getWrappedType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getInnermostType
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
1<?php declare(strict_types=1);
2
3namespace Apie\Graphql\Types;
4
5use GraphQL\Error\InvariantViolation;
6use GraphQL\Type\Definition\InputType;
7use GraphQL\Type\Definition\NamedType;
8use GraphQL\Type\Definition\NullableType;
9use GraphQL\Type\Definition\OutputType;
10use GraphQL\Type\Definition\ScalarType;
11use GraphQL\Type\Definition\Type;
12use GraphQL\Type\Definition\WrappingType;
13use GraphQL\Type\Schema;
14
15/**
16 * Graphql mapping for ItemHashmap.
17 */
18class MapOfType extends ScalarType implements WrappingType, InputType, OutputType, NullableType
19{
20    /**
21     * @var Type|callable
22     *
23     * @phpstan-var OfType|callable(): OfType
24     */
25    private $wrappedType;
26
27    /**
28     * @param Type|callable $type
29     *
30     * @phpstan-param OfType|callable(): OfType $type
31     */
32    public function __construct($type)
33    {
34        $this->wrappedType = $type;
35        parent::__construct([
36            'name' => 'MapOf' . (is_callable($type) ? md5(static::class) : $type->toString()),
37            'description' => 'A map/dictionary/hashmap where the keys are strings and the values are of type ' . (is_callable($type) ? '<dynamic>' : $type->toString()),
38        ]);
39    }
40
41    public function serialize($value): mixed
42    {
43        return $value;
44    }
45
46    public function parseValue($value): mixed
47    {
48        if (!is_iterable($value)) {
49            throw new InvariantViolation('Could not parse value as MapOfType, value is not iterable.');
50        }
51        $res = [];
52        foreach ($value as $key => $item) {
53            $res[$key] = $this->getWrappedType()->parseValue($item);
54        }
55        return $res;
56    }
57
58    public function parseLiteral($valueNode, array $variables = null): mixed
59    {
60        return $valueNode->value;
61    }
62
63    /** @phpstan-return OfType */
64    public function getWrappedType(): Type
65    {
66        return Schema::resolveType($this->wrappedType);
67    }
68
69    public function getInnermostType(): NamedType
70    {
71        $type = $this->getWrappedType();
72        while ($type instanceof WrappingType) {
73            $type = $type->getWrappedType();
74        }
75
76        assert($type instanceof NamedType, 'known because we unwrapped all the way down');
77
78        return $type;
79    }
80}