|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Overblog\GraphQLBundle\Definition\Type; |
| 6 | + |
| 7 | +use Exception; |
| 8 | +use GraphQL\Error\Error; |
| 9 | +use GraphQL\Error\SerializationError; |
| 10 | +use GraphQL\Language\AST\EnumTypeDefinitionNode; |
| 11 | +use GraphQL\Language\AST\EnumTypeExtensionNode; |
| 12 | +use GraphQL\Language\AST\EnumValueNode; |
| 13 | +use GraphQL\Language\AST\Node; |
| 14 | +use GraphQL\Type\Definition\EnumType; |
| 15 | +use GraphQL\Utils\Utils; |
| 16 | +use ReflectionEnum; |
| 17 | +use UnitEnum; |
| 18 | + |
| 19 | +/** |
| 20 | + * @phpstan-import-type EnumValues from EnumType |
| 21 | + * |
| 22 | + * @phpstan-type PhpEnumTypeConfig array{ |
| 23 | + * name?: string|null, |
| 24 | + * description?: string|null, |
| 25 | + * enumClass?: class-string<\UnitEnum>|null, |
| 26 | + * values?: EnumValues|callable(): EnumValues, |
| 27 | + * astNode?: EnumTypeDefinitionNode|null, |
| 28 | + * extensionASTNodes?: array<int, EnumTypeExtensionNode>|null |
| 29 | + * } |
| 30 | + */ |
| 31 | +class PhpEnumType extends EnumType |
| 32 | +{ |
| 33 | + /** @var class-string<UnitEnum>|null */ |
| 34 | + protected ?string $enumClass = null; |
| 35 | + |
| 36 | + /** |
| 37 | + * @phpstan-param PhpEnumTypeConfig $config |
| 38 | + */ |
| 39 | + public function __construct(array $config) |
| 40 | + { |
| 41 | + if (isset($config['enumClass'])) { |
| 42 | + $this->enumClass = $config['enumClass']; |
| 43 | + if (!enum_exists($this->enumClass)) { |
| 44 | + throw new Error(sprintf('Enum class "%s" does not exist.', $this->enumClass)); |
| 45 | + } |
| 46 | + unset($config['enumClass']); |
| 47 | + } |
| 48 | + |
| 49 | + if (!isset($config['values'])) { |
| 50 | + $config['values'] = []; |
| 51 | + } |
| 52 | + |
| 53 | + parent::__construct($config); |
| 54 | + if ($this->enumClass) { |
| 55 | + $configValues = $this->config['values'] ?? []; |
| 56 | + if (is_callable($configValues)) { |
| 57 | + $configValues = $configValues(); |
| 58 | + } |
| 59 | + $reflection = new ReflectionEnum($this->enumClass); |
| 60 | + |
| 61 | + $enumDefinitions = []; |
| 62 | + foreach ($reflection->getCases() as $case) { |
| 63 | + $enumDefinitions[$case->getName()] = ['value' => $case->getName()]; |
| 64 | + } |
| 65 | + |
| 66 | + foreach ($configValues as $name => $config) { |
| 67 | + if (!isset($enumDefinitions[$name])) { |
| 68 | + throw new Error("Enum value {$name} is not defined in {$this->enumClass}"); |
| 69 | + } |
| 70 | + $enumDefinitions[$name]['description'] = $config['description'] ?? null; |
| 71 | + $enumDefinitions[$name]['deprecationReason'] = $config['deprecationReason'] ?? null; |
| 72 | + } |
| 73 | + |
| 74 | + $this->config['values'] = $enumDefinitions; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + public function parseValue($value): mixed |
| 79 | + { |
| 80 | + if ($this->enumClass) { |
| 81 | + try { |
| 82 | + return (new ReflectionEnum($this->enumClass))->getCase($value)->getValue(); |
| 83 | + } catch (Exception $e) { |
| 84 | + throw new Error("Cannot represent enum of class {$this->enumClass} from value {$value}: ".$e->getMessage()); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + return parent::parseValue($value); |
| 89 | + } |
| 90 | + |
| 91 | + public function parseLiteral(Node $valueNode, ?array $variables = null): mixed |
| 92 | + { |
| 93 | + if ($this->enumClass) { |
| 94 | + if (!$valueNode instanceof EnumValueNode) { |
| 95 | + throw new Error("Cannot represent enum of class {$this->enumClass} from node: {$valueNode->__toString()} is not an enum value"); |
| 96 | + } |
| 97 | + try { |
| 98 | + return (new ReflectionEnum($this->enumClass))->getCase($valueNode->value)->getValue(); |
| 99 | + } catch (Exception $e) { |
| 100 | + throw new Error("Cannot represent enum of class {$this->enumClass} from literal {$valueNode->value}: ".$e->getMessage()); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + return parent::parseLiteral($valueNode, $variables); |
| 105 | + } |
| 106 | + |
| 107 | + public function serialize($value): mixed |
| 108 | + { |
| 109 | + if ($this->enumClass) { |
| 110 | + if (!$value instanceof $this->enumClass) { |
| 111 | + $valueStr = Utils::printSafe($value); |
| 112 | + throw new SerializationError("Cannot serialize value {$valueStr} as it must be an instance of enum {$this->enumClass}."); |
| 113 | + } |
| 114 | + |
| 115 | + return $value->name; |
| 116 | + } |
| 117 | + |
| 118 | + return parent::serialize($value); |
| 119 | + } |
| 120 | +} |
0 commit comments