1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Digia\GraphQL\Language\Visitor; |
4
|
|
|
|
5
|
|
|
use Digia\GraphQL\Language\Node\NodeInterface; |
6
|
|
|
|
7
|
|
|
class VisitorInfo |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var VisitorInterface |
11
|
|
|
*/ |
12
|
|
|
protected $visitor; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var string|int|null |
16
|
|
|
*/ |
17
|
|
|
protected $key; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var NodeInterface|null |
21
|
|
|
*/ |
22
|
|
|
protected $parent; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $path; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $ancestors; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* VisitorInfo constructor. |
36
|
|
|
* @param VisitorInterface $visitor |
37
|
|
|
* @param int|null|string $key |
38
|
|
|
* @param NodeInterface|null $parent |
39
|
|
|
* @param array $path |
40
|
|
|
* @param array $ancestors |
41
|
|
|
*/ |
42
|
|
|
public function __construct( |
43
|
|
|
VisitorInterface $visitor, |
44
|
|
|
$key = null, |
45
|
|
|
?NodeInterface $parent = null, |
46
|
|
|
array $path = [], |
47
|
|
|
array $ancestors = [] |
48
|
|
|
) { |
49
|
|
|
$this->visitor = $visitor; |
50
|
|
|
$this->key = $key; |
51
|
|
|
$this->parent = $parent; |
52
|
|
|
$this->path = $path; |
53
|
|
|
$this->ancestors = $ancestors; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Appends a key to the path. |
58
|
|
|
* @param string $key |
59
|
|
|
*/ |
60
|
|
|
public function addOneToPath(string $key) |
61
|
|
|
{ |
62
|
|
|
$this->path[] = $key; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Removes the last item from the path. |
67
|
|
|
*/ |
68
|
|
|
public function removeOneFromPath() |
69
|
|
|
{ |
70
|
|
|
$this->path = \array_slice($this->path, 0, -1); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Adds an ancestor. |
75
|
|
|
* @param NodeInterface $node |
76
|
|
|
*/ |
77
|
|
|
public function addAncestor(NodeInterface $node) |
78
|
|
|
{ |
79
|
|
|
$this->ancestors[] = $node; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Removes the last ancestor. |
84
|
|
|
*/ |
85
|
|
|
public function removeAncestor() |
86
|
|
|
{ |
87
|
|
|
$this->ancestors = \array_slice($this->ancestors, 0, -1); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @inheritdoc |
92
|
|
|
*/ |
93
|
|
|
public function getAncestor(int $depth = 1): ?NodeInterface |
94
|
|
|
{ |
95
|
|
|
if (empty($this->ancestors)) { |
96
|
|
|
return null; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$index = \count($this->ancestors) - $depth; |
100
|
|
|
|
101
|
|
|
return $this->ancestors[$index] ?? null; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return VisitorInterface |
106
|
|
|
*/ |
107
|
|
|
public function getVisitor(): VisitorInterface |
108
|
|
|
{ |
109
|
|
|
return $this->visitor; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return int|null|string |
114
|
|
|
*/ |
115
|
|
|
public function getKey() |
116
|
|
|
{ |
117
|
|
|
return $this->key; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return NodeInterface|null |
122
|
|
|
*/ |
123
|
|
|
public function getParent(): ?NodeInterface |
124
|
|
|
{ |
125
|
|
|
return $this->parent; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return array |
130
|
|
|
*/ |
131
|
|
|
public function getPath(): array |
132
|
|
|
{ |
133
|
|
|
return $this->path; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return array |
138
|
|
|
*/ |
139
|
|
|
public function getAncestors(): array |
140
|
|
|
{ |
141
|
|
|
return $this->ancestors; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|