1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Digia\GraphQL\Language\ASTBuilder; |
4
|
|
|
|
5
|
|
|
use Digia\GraphQL\Error\SyntaxErrorException; |
6
|
|
|
use Digia\GraphQL\Language\DirectiveLocationEnum; |
7
|
|
|
use Digia\GraphQL\Language\KeywordEnum; |
8
|
|
|
use Digia\GraphQL\Language\LexerInterface; |
9
|
|
|
use Digia\GraphQL\Language\Node\NodeKindEnum; |
10
|
|
|
use Digia\GraphQL\Language\TokenKindEnum; |
11
|
|
|
|
12
|
|
|
class DirectiveDefinitionASTBuilder extends AbstractASTBuilder |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @inheritdoc |
16
|
|
|
*/ |
17
|
|
|
public function supportsBuilder(string $kind): bool |
18
|
|
|
{ |
19
|
|
|
return $kind === ASTKindEnum::DIRECTIVE_DEFINITION; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @inheritdoc |
24
|
|
|
*/ |
25
|
|
|
public function build(LexerInterface $lexer, array $params): ?array |
26
|
|
|
{ |
27
|
|
|
$start = $lexer->getToken(); |
28
|
|
|
|
29
|
|
|
$description = $this->buildAST(ASTKindEnum::DESCRIPTION, $lexer); |
30
|
|
|
|
31
|
|
|
$this->expectKeyword($lexer, KeywordEnum::DIRECTIVE); |
32
|
|
|
$this->expect($lexer, TokenKindEnum::AT); |
33
|
|
|
|
34
|
|
|
$name = $this->buildAST(ASTKindEnum::NAME, $lexer); |
35
|
|
|
$arguments = $this->buildAST(ASTKindEnum::ARGUMENTS_DEFINITION, $lexer); |
36
|
|
|
|
37
|
|
|
$this->expectKeyword($lexer, KeywordEnum::ON); |
38
|
|
|
|
39
|
|
|
$locations = $this->parseDirectiveLocations($lexer); |
40
|
|
|
|
41
|
|
|
return [ |
42
|
|
|
'kind' => NodeKindEnum::DIRECTIVE_DEFINITION, |
43
|
|
|
'description' => $description, |
44
|
|
|
'name' => $name, |
45
|
|
|
'arguments' => $arguments, |
46
|
|
|
'locations' => $locations, |
47
|
|
|
'loc' => $this->buildLocation($lexer, $start), |
48
|
|
|
]; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param LexerInterface $lexer |
53
|
|
|
* @return array |
54
|
|
|
* @throws SyntaxErrorException |
55
|
|
|
* @throws \ReflectionException |
56
|
|
|
*/ |
57
|
|
|
protected function parseDirectiveLocations(LexerInterface $lexer): array |
58
|
|
|
{ |
59
|
|
|
$this->skip($lexer, TokenKindEnum::PIPE); |
60
|
|
|
|
61
|
|
|
$locations = []; |
62
|
|
|
|
63
|
|
|
do { |
64
|
|
|
$locations[] = $this->parseDirectiveLocation($lexer); |
65
|
|
|
} while ($this->skip($lexer, TokenKindEnum::PIPE)); |
66
|
|
|
|
67
|
|
|
return $locations; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param LexerInterface $lexer |
72
|
|
|
* @return array |
73
|
|
|
* @throws SyntaxErrorException |
74
|
|
|
* @throws \ReflectionException |
75
|
|
|
*/ |
76
|
|
|
protected function parseDirectiveLocation(LexerInterface $lexer): array |
77
|
|
|
{ |
78
|
|
|
$start = $lexer->getToken(); |
79
|
|
|
|
80
|
|
|
$name = $this->buildAST(ASTKindEnum::NAME, $lexer); |
81
|
|
|
|
82
|
|
|
if (\in_array($name['value'], DirectiveLocationEnum::values(), true)) { |
83
|
|
|
return $name; |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
throw $this->unexpected($lexer, $start); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|