|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Andi\GraphQL; |
|
6
|
|
|
|
|
7
|
|
|
use Andi\GraphQL\Exception\NotFoundException; |
|
8
|
|
|
use GraphQL\Type\Definition as Webonyx; |
|
9
|
|
|
|
|
10
|
|
|
class TypeRegistry implements TypeRegistryInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var array<string, string> |
|
14
|
|
|
*/ |
|
15
|
|
|
protected array $aliases; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var array<string, Webonyx\Type&Webonyx\NamedType> |
|
19
|
|
|
*/ |
|
20
|
|
|
protected array $registry; |
|
21
|
|
|
|
|
22
|
29 |
|
public function __construct() |
|
23
|
|
|
{ |
|
24
|
29 |
|
$this->aliases = [ |
|
25
|
29 |
|
Webonyx\IntType::class => Webonyx\Type::INT, |
|
26
|
29 |
|
Webonyx\FloatType::class => Webonyx\Type::FLOAT, |
|
27
|
29 |
|
Webonyx\StringType::class => Webonyx\Type::STRING, |
|
28
|
29 |
|
Webonyx\BooleanType::class => Webonyx\Type::BOOLEAN, |
|
29
|
29 |
|
Webonyx\IDType::class => Webonyx\Type::ID, |
|
30
|
29 |
|
]; |
|
31
|
|
|
|
|
32
|
29 |
|
$this->registry = [ |
|
33
|
29 |
|
Webonyx\Type::INT => Webonyx\Type::int(), |
|
34
|
29 |
|
Webonyx\Type::FLOAT => Webonyx\Type::float(), |
|
35
|
29 |
|
Webonyx\Type::STRING => Webonyx\Type::string(), |
|
36
|
29 |
|
Webonyx\Type::BOOLEAN => Webonyx\Type::boolean(), |
|
37
|
29 |
|
Webonyx\Type::ID => Webonyx\Type::id(), |
|
38
|
29 |
|
]; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
14 |
|
public function has(string $type): bool |
|
42
|
|
|
{ |
|
43
|
14 |
|
return isset($this->aliases[$type]) || isset($this->registry[$type]); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
14 |
|
public function get(string $type): Webonyx\Type&Webonyx\NamedType |
|
47
|
|
|
{ |
|
48
|
14 |
|
return $this->registry[$this->aliases[$type] ?? $type] |
|
49
|
14 |
|
?? throw NotFoundException::create($type); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
29 |
|
public function register(Webonyx\Type&Webonyx\NamedType $type, string ...$aliases): void |
|
53
|
|
|
{ |
|
54
|
29 |
|
$name = (string) $type; |
|
55
|
|
|
|
|
56
|
29 |
|
$this->registry[$name] = $type; |
|
57
|
|
|
|
|
58
|
29 |
|
foreach ($aliases as $alias) { |
|
59
|
29 |
|
$this->aliases[$alias] = $name; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return iterable<Webonyx\ObjectType> |
|
65
|
|
|
*/ |
|
66
|
1 |
|
public function getTypes(): iterable |
|
67
|
|
|
{ |
|
68
|
1 |
|
foreach ($this->registry as $type) { |
|
69
|
1 |
|
if ($type instanceof Webonyx\ObjectType && ! empty($type->getInterfaces())) { |
|
70
|
1 |
|
yield $type; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|