|
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>> |
|
|
|
|
|
|
27
|
|
|
*/ |
|
28
|
|
|
protected function createValues(): array |
|
29
|
|
|
{ |
|
30
|
|
|
$values = []; |
|
31
|
|
|
|
|
32
|
|
|
foreach (static::getEnumClass()::values() as $value) { |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
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.