1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace GraphQL\Language\AST; |
6
|
|
|
|
7
|
|
|
use ArrayAccess; |
8
|
|
|
use Countable; |
9
|
|
|
use Generator; |
10
|
|
|
use GraphQL\Utils\AST; |
11
|
|
|
use IteratorAggregate; |
12
|
|
|
use function array_merge; |
13
|
|
|
use function array_splice; |
14
|
|
|
use function count; |
15
|
|
|
use function is_array; |
16
|
|
|
|
17
|
|
|
class NodeList implements ArrayAccess, IteratorAggregate, Countable |
18
|
|
|
{ |
19
|
|
|
/** @var Node[]|mixed[] */ |
20
|
|
|
private $nodes; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param Node[]|mixed[] $nodes |
24
|
|
|
* |
25
|
|
|
* @return static |
26
|
|
|
*/ |
27
|
1 |
|
public static function create(array $nodes) |
28
|
|
|
{ |
29
|
1 |
|
return new static($nodes); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param Node[]|mixed[] $nodes |
34
|
|
|
*/ |
35
|
915 |
|
public function __construct(array $nodes) |
36
|
|
|
{ |
37
|
915 |
|
$this->nodes = $nodes; |
38
|
915 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param mixed $offset |
42
|
|
|
* |
43
|
|
|
* @return bool |
44
|
|
|
*/ |
45
|
|
|
public function offsetExists($offset) |
46
|
|
|
{ |
47
|
|
|
return isset($this->nodes[$offset]); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param mixed $offset |
52
|
|
|
* |
53
|
|
|
* @return mixed |
54
|
|
|
*/ |
55
|
913 |
|
public function offsetGet($offset) |
56
|
|
|
{ |
57
|
913 |
|
$item = $this->nodes[$offset]; |
58
|
|
|
|
59
|
913 |
|
if (is_array($item) && isset($item['kind'])) { |
60
|
2 |
|
$this->nodes[$offset] = $item = AST::fromArray($item); |
61
|
|
|
} |
62
|
|
|
|
63
|
913 |
|
return $item; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param mixed $offset |
68
|
|
|
* @param mixed $value |
69
|
|
|
*/ |
70
|
133 |
|
public function offsetSet($offset, $value) |
71
|
|
|
{ |
72
|
133 |
|
if (is_array($value) && isset($value['kind'])) { |
73
|
|
|
$value = AST::fromArray($value); |
74
|
|
|
} |
75
|
133 |
|
$this->nodes[$offset] = $value; |
76
|
133 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param mixed $offset |
80
|
|
|
*/ |
81
|
|
|
public function offsetUnset($offset) |
82
|
|
|
{ |
83
|
|
|
unset($this->nodes[$offset]); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param int $offset |
88
|
|
|
* @param int $length |
89
|
|
|
* @param mixed $replacement |
90
|
|
|
* |
91
|
|
|
* @return NodeList |
92
|
|
|
*/ |
93
|
4 |
|
public function splice($offset, $length, $replacement = null) |
94
|
|
|
{ |
95
|
4 |
|
return new NodeList(array_splice($this->nodes, $offset, $length, $replacement)); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param NodeList|Node[] $list |
100
|
|
|
* |
101
|
|
|
* @return NodeList |
102
|
|
|
*/ |
103
|
1 |
|
public function merge($list) |
104
|
|
|
{ |
105
|
1 |
|
if ($list instanceof self) { |
106
|
1 |
|
$list = $list->nodes; |
107
|
|
|
} |
108
|
|
|
|
109
|
1 |
|
return new NodeList(array_merge($this->nodes, $list)); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return Generator |
114
|
|
|
*/ |
115
|
705 |
|
public function getIterator() |
116
|
|
|
{ |
117
|
705 |
|
foreach ($this->nodes as $key => $_) { |
118
|
699 |
|
yield $this->offsetGet($key); |
119
|
|
|
} |
120
|
701 |
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return int |
124
|
|
|
*/ |
125
|
610 |
|
public function count() |
126
|
|
|
{ |
127
|
610 |
|
return count($this->nodes); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|