Completed
Pull Request — master (#125)
by Mathieu
01:39
created

EnumType::createValues()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php declare(strict_types=1);
2
3
namespace Elao\Enum\Bridge\Overblog\GraphQL;
4
5
use Elao\Enum\EnumInterface;
6
use GraphQL\Type\Definition;
7
8
abstract class EnumType extends Definition\EnumType
9
{
10
    /**
11
     * {@inheritdoc}
12
     *
13
     * @param array<string, mixed> $config
14
     */
15
    public function __construct(array $config = [])
16
    {
17
        parent::__construct([
18
            'values' => $this->createValues(),
19
            'description' => $this->description,
20
        ] + $config);
21
    }
22
23
    /**
24
     * Override to customize how enum values are created.
25
     *
26
     * @return array<string, array<string>>
0 ignored issues
show
Documentation introduced by
The doc-type array<string, could not be parsed: Expected ">" at position 5, but found "end of type". (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
27
     */
28
    protected function createValues(): array
29
    {
30
        $values = [];
31
32
        foreach (static::getEnumClass()::values() as $value) {
0 ignored issues
show
Bug introduced by
The method values cannot be called on static::getEnumClass() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
33
            $values[$this->getEnumValueName($value)] = ['value' => $value];
34
        }
35
36
        return $values;
37
    }
38
39
    /**
40
     * @param int|string $value One of the EnumInterface implementation enumerated value
41
     *
42
     * @return string|int
43
     */
44
    protected function getEnumValueName($value)
45
    {
46
        return $value;
47
    }
48
49
    /**
50
     * @return string The enum FQCN for which we should create a type.
51
     */
52
    abstract protected static function getEnumClass(): string;
53
54
    /**
55
     * {@inheritdoc}
56
     *
57
     * @param EnumInterface|null $value
58
     */
59
    public function serialize($value)
60
    {
61
        return $value ? $value->getValue() : null;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function parseValue($value)
68
    {
69
        if ($value === null) {
70
            return null;
71
        }
72
73
        return static::getEnumClass()::get(parent::parseValue($value) ?? $value);
0 ignored issues
show
Bug introduced by
The method get cannot be called on static::getEnumClass() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     *
79
     * @param array<string, mixed>|null $variables
80
     */
81
    public function parseLiteral($valueNode, ?array $variables = null)
82
    {
83
        if ($valueNode === null) {
84
            return null;
85
        }
86
87
        return static::getEnumClass()::get(parent::parseLiteral($valueNode) ?? $valueNode);
0 ignored issues
show
Bug introduced by
The method get cannot be called on static::getEnumClass() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
88
    }
89
}
90