jerowork /
graphql-attribute-schema
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Jerowork\GraphqlAttributeSchema\NodeParser; |
||
| 6 | |||
| 7 | use BackedEnum; |
||
| 8 | use Generator; |
||
| 9 | use Jerowork\GraphqlAttributeSchema\Attribute\Enum; |
||
| 10 | use Jerowork\GraphqlAttributeSchema\Attribute\EnumValue; |
||
| 11 | use Jerowork\GraphqlAttributeSchema\Node\Child\EnumValueNode; |
||
| 12 | use Jerowork\GraphqlAttributeSchema\Node\EnumNode; |
||
| 13 | use Override; |
||
| 14 | use ReflectionClass; |
||
| 15 | use ReflectionEnum; |
||
| 16 | use ReflectionException; |
||
| 17 | use ReflectionMethod; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @internal |
||
| 21 | */ |
||
| 22 | final readonly class EnumNodeParser implements NodeParser |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 23 | { |
||
| 24 | use RetrieveNameForTypeTrait; |
||
| 25 | use GetAttributeTrait; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @throws ParseException |
||
| 29 | * @throws ReflectionException |
||
| 30 | */ |
||
| 31 | 6 | #[Override] |
|
| 32 | public function parse(string $attribute, ReflectionClass $class, ?ReflectionMethod $method): Generator |
||
| 33 | { |
||
| 34 | 6 | if ($attribute !== Enum::class) { |
|
| 35 | 3 | return; |
|
| 36 | } |
||
| 37 | |||
| 38 | 5 | $className = $class->getName(); |
|
| 39 | |||
| 40 | 5 | if (!$class->isEnum()) { |
|
| 41 | 1 | throw ParseException::notAnEnumClass($className); |
|
| 42 | } |
||
| 43 | |||
| 44 | 4 | if (!is_subclass_of($className, BackedEnum::class)) { |
|
| 45 | 1 | throw ParseException::notABackedEnumClass($className); |
|
| 46 | } |
||
| 47 | |||
| 48 | 3 | $attribute = $this->getAttribute($class, Enum::class); |
|
| 49 | |||
| 50 | 3 | $name = $this->retrieveNameForType($class, $attribute); |
|
| 51 | |||
| 52 | 3 | yield new EnumNode( |
|
| 53 | 3 | $className, |
|
| 54 | 3 | $name, |
|
| 55 | 3 | $attribute->description, |
|
| 56 | 3 | $this->getValues($class), |
|
| 57 | 3 | ); |
|
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @throws ReflectionException |
||
| 62 | * |
||
| 63 | * @return list<EnumValueNode> |
||
| 64 | */ |
||
| 65 | 3 | private function getValues(ReflectionClass $class): array |
|
| 66 | { |
||
| 67 | 3 | $cases = []; |
|
| 68 | /** @var class-string<BackedEnum> $enumClassName */ |
||
| 69 | 3 | $enumClassName = $class->getName(); |
|
| 70 | 3 | foreach ((new ReflectionEnum($enumClassName))->getCases() as $case) { |
|
| 71 | 3 | $enumAttributes = $case->getAttributes(EnumValue::class); |
|
| 72 | |||
| 73 | /** @var null|EnumValue $enumAttribute */ |
||
| 74 | 3 | $enumAttribute = $enumAttributes !== [] ? array_pop($enumAttributes)->newInstance() : null; |
|
| 75 | |||
| 76 | /** @var BackedEnum $value */ |
||
| 77 | 3 | $value = $case->getValue(); |
|
| 78 | |||
| 79 | 3 | $cases[] = new EnumValueNode( |
|
| 80 | 3 | (string) $value->value, |
|
| 81 | 3 | $enumAttribute?->description, |
|
| 82 | 3 | $enumAttribute?->deprecationReason, |
|
| 83 | 3 | ); |
|
| 84 | } |
||
| 85 | |||
| 86 | 3 | return $cases; |
|
| 87 | } |
||
| 88 | } |
||
| 89 |