-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphQLDescribable.php
More file actions
46 lines (38 loc) · 1.21 KB
/
GraphQLDescribable.php
File metadata and controls
46 lines (38 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
declare(strict_types=1);
namespace Worksome\GraphQLHelpers\Definition\Concerns;
use Exception;
use GraphQL\Type\Definition\Description;
use Illuminate\Support\Str;
use ReflectionEnum;
use UnitEnum;
/**
* @mixin UnitEnum
*
* @phpstan-ignore trait.unused
*/
trait GraphQLDescribable
{
public function description(): string
{
$reflection = new ReflectionEnum(static::class);
$constReflection = $reflection->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)));
}
}