1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Digia\GraphQL\Language\AST\Node\Builder; |
4
|
|
|
|
5
|
|
|
use Digia\GraphQL\Language\AST\Node\Builder\Contract\BuilderInterface; |
6
|
|
|
use Digia\GraphQL\Language\AST\Node\Builder\Contract\DirectorInterface; |
7
|
|
|
use Digia\GraphQL\Language\Location; |
8
|
|
|
|
9
|
|
|
abstract class AbstractBuilder implements BuilderInterface |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var DirectorInterface |
14
|
|
|
*/ |
15
|
|
|
protected $director; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param DirectorInterface $director |
19
|
|
|
* @return $this |
20
|
|
|
*/ |
21
|
|
|
public function setDirector(DirectorInterface $director) |
22
|
|
|
{ |
23
|
|
|
$this->director = $director; |
24
|
|
|
return $this; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Creates a location object. |
29
|
|
|
* |
30
|
|
|
* @param array $ast |
31
|
|
|
* @return Location |
32
|
|
|
*/ |
33
|
|
|
protected function createLocation(array $ast): Location |
34
|
|
|
{ |
35
|
|
|
return isset($ast['loc']['start'], $ast['loc']['end']) |
|
|
|
|
36
|
|
|
? new Location($ast['loc']['start'], $ast['loc']['end']) |
37
|
|
|
: null; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Returns the value of a single property in the given AST. |
42
|
|
|
* |
43
|
|
|
* @param array $ast |
44
|
|
|
* @param string $propertyName |
45
|
|
|
* @param null $defaultValue |
|
|
|
|
46
|
|
|
* @return mixed|null |
47
|
|
|
*/ |
48
|
|
|
protected function get(array $ast, string $propertyName, $defaultValue = null) |
49
|
|
|
{ |
50
|
|
|
return $ast[$propertyName] ?? $defaultValue; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Builds a single item from the given AST. |
55
|
|
|
* |
56
|
|
|
* @param array $ast |
57
|
|
|
* @param string $propertyName |
58
|
|
|
* @return mixed|null |
59
|
|
|
*/ |
60
|
|
|
protected function buildOne(array $ast, string $propertyName) |
61
|
|
|
{ |
62
|
|
|
return isset($ast[$propertyName]) ? $this->director->build($ast[$propertyName]) : null; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Builds many items from the given AST. |
67
|
|
|
* |
68
|
|
|
* @param array $ast |
69
|
|
|
* @param string $propertyName |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
|
|
protected function buildMany(array $ast, string $propertyName): array |
73
|
|
|
{ |
74
|
|
|
$array = []; |
75
|
|
|
|
76
|
|
|
if (isset($ast[$propertyName])) { |
77
|
|
|
foreach ($ast[$propertyName] as $subAst) { |
78
|
|
|
$array[] = $this->director->build($subAst); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $array; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|