|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Digia\GraphQL\Language\ASTBuilder; |
|
4
|
|
|
|
|
5
|
|
|
use Digia\GraphQL\Error\SyntaxErrorException; |
|
6
|
|
|
use Digia\GraphQL\Language\LexerInterface; |
|
7
|
|
|
use Digia\GraphQL\Language\Node\NodeKindEnum; |
|
8
|
|
|
use Digia\GraphQL\Language\TokenKindEnum; |
|
9
|
|
|
|
|
10
|
|
|
class TypeReferenceASTBuilder extends AbstractASTBuilder |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @inheritdoc |
|
14
|
|
|
*/ |
|
15
|
|
|
public function supportsBuilder(string $kind): bool |
|
16
|
|
|
{ |
|
17
|
|
|
return $kind === ASTKindEnum::TYPE_REFERENCE; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @inheritdoc |
|
22
|
|
|
*/ |
|
23
|
|
|
public function build(LexerInterface $lexer, array $params): ?array |
|
24
|
|
|
{ |
|
25
|
|
|
return $this->parseTypeReference($lexer); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param LexerInterface $lexer |
|
30
|
|
|
* @return array |
|
31
|
|
|
* @throws SyntaxErrorException |
|
32
|
|
|
*/ |
|
33
|
|
|
protected function parseTypeReference(LexerInterface $lexer): array |
|
34
|
|
|
{ |
|
35
|
|
|
$start = $lexer->getToken(); |
|
36
|
|
|
|
|
37
|
|
|
if ($this->skip($lexer, TokenKindEnum::BRACKET_L)) { |
|
38
|
|
|
$type = $this->parseTypeReference($lexer); |
|
39
|
|
|
|
|
40
|
|
|
$this->expect($lexer, TokenKindEnum::BRACKET_R); |
|
41
|
|
|
|
|
42
|
|
|
$type = [ |
|
43
|
|
|
'kind' => NodeKindEnum::LIST_TYPE, |
|
44
|
|
|
'type' => $type, |
|
45
|
|
|
'loc' => $this->buildLocation($lexer, $start), |
|
46
|
|
|
]; |
|
47
|
|
|
} else { |
|
48
|
|
|
$type = $this->buildAST(ASTKindEnum::NAMED_TYPE, $lexer); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
if ($this->skip($lexer, TokenKindEnum::BANG)) { |
|
52
|
|
|
return [ |
|
53
|
|
|
'kind' => NodeKindEnum::NON_NULL_TYPE, |
|
54
|
|
|
'type' => $type, |
|
55
|
|
|
'loc' => $this->buildLocation($lexer, $start), |
|
56
|
|
|
]; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return $type; |
|
|
|
|
|
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|