diff --git a/src/Definition/Concerns/GraphQLDescribable.php b/src/Definition/Concerns/GraphQLDescribable.php new file mode 100644 index 0000000..60547af --- /dev/null +++ b/src/Definition/Concerns/GraphQLDescribable.php @@ -0,0 +1,40 @@ +getCase($this->name); + + $descriptionAttributes = $constReflection->getAttributes(Description::class); + + return match (count($descriptionAttributes)) { + 0 => self::friendlyDescription($this->name), + 1 => $descriptionAttributes[0]->newInstance()->description, + default => throw new Exception( + 'You cannot use more than 1 description attribute on ' . class_basename(static::class) . '::' . $this->name + ), + }; + } + + private function friendlyDescription(string $name): string + { + if (ctype_upper(preg_replace('/[^a-zA-Z]/', '', $name))) { + $name = strtolower($name); + } + + return ucfirst(str_replace('_', ' ', Str::snake($name))); + } +} diff --git a/tests/Unit/Definition/Concerns/GraphQLDescribable/GraphQLDescribableTest.php b/tests/Unit/Definition/Concerns/GraphQLDescribable/GraphQLDescribableTest.php new file mode 100644 index 0000000..8dc6310 --- /dev/null +++ b/tests/Unit/Definition/Concerns/GraphQLDescribable/GraphQLDescribableTest.php @@ -0,0 +1,31 @@ +description())->toBe($description); +})->with([ + [DummyEnum::PascalCase, 'PascalCase description'], + [DummyEnum::SCREAMING_SNAKE_CASE, 'SCREAMING_SNAKE_CASE description'], + [DummyEnum::snake_case, 'snake_case description'], + [DummyEnum::NoDescription, 'No description'], +]);