Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
35.00% covered (danger)
35.00%
7 / 20
33.33% covered (danger)
33.33%
2 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
MapOfType
35.00% covered (danger)
35.00%
7 / 20
33.33% covered (danger)
33.33%
2 / 6
31.24
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 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
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getInnermostType
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
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 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        $named = Type::getNamedType($type);
36        parent::__construct([
37            'name' => 'MapOf' . $named->name,
38            'description' => 'A map/dictionary/hashmap where the keys are strings and the values are of type ' . $named->name,
39        ]);
40    }
41
42    public function serialize($value): mixed
43    {
44        return $value;
45    }
46
47    public function parseValue($value): mixed
48    {
49        if (!is_iterable($value)) {
50            throw new InvariantViolation('Could not parse value as MapOfType, value is not iterable.');
51        }
52        $res = [];
53        foreach ($value as $key => $item) {
54            $res[$key] = $this->getWrappedType()->parseValue($item);
55        }
56        return $res;
57    }
58
59    public function parseLiteral($valueNode, array $variables = null): mixed
60    {
61        return $valueNode->value;
62    }
63
64    /** @phpstan-return OfType */
65    public function getWrappedType(): Type
66    {
67        return Schema::resolveType($this->wrappedType);
68    }
69
70    public function getInnermostType(): NamedType
71    {
72        $type = $this->getWrappedType();
73        while ($type instanceof WrappingType) {
74            $type = $type->getWrappedType();
75        }
76
77        assert($type instanceof NamedType, 'known because we unwrapped all the way down');
78
79        return $type;
80    }
81}