1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Jerowork\GraphqlAttributeSchema; |
||
6 | |||
7 | use Jerowork\GraphqlAttributeSchema\Node\AliasedNode; |
||
8 | use Jerowork\GraphqlAttributeSchema\Node\ArraySerializable; |
||
9 | use Jerowork\GraphqlAttributeSchema\Node\InterfaceTypeNode; |
||
10 | use Jerowork\GraphqlAttributeSchema\Node\Node; |
||
11 | use Jerowork\GraphqlAttributeSchema\Node\TypeNode; |
||
12 | |||
13 | /** |
||
14 | * @phpstan-type AstPayload array{ |
||
15 | * nodes: list<array{ |
||
16 | * node: class-string, |
||
17 | * payload: array<string, mixed> |
||
18 | * }> |
||
19 | * } |
||
20 | */ |
||
21 | final readonly class Ast implements ArraySerializable |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
22 | { |
||
23 | /** |
||
24 | * @var list<Node> |
||
25 | */ |
||
26 | private array $nodes; |
||
27 | |||
28 | 31 | public function __construct(Node ...$nodes) |
|
29 | { |
||
30 | 31 | $this->nodes = array_values($nodes); |
|
31 | } |
||
32 | |||
33 | /** |
||
34 | * @template T of Node |
||
35 | * |
||
36 | * @param class-string<T> $nodeType |
||
37 | * |
||
38 | * @return list<T> |
||
39 | */ |
||
40 | 5 | public function getNodesByNodeType(string $nodeType): array |
|
41 | { |
||
42 | 5 | return array_values(array_filter($this->nodes, fn($node) => $node instanceof $nodeType)); |
|
43 | } |
||
44 | |||
45 | /** |
||
46 | * @param class-string $className |
||
47 | */ |
||
48 | 23 | public function getNodeByClassName(string $className): ?Node |
|
49 | { |
||
50 | 23 | foreach ($this->nodes as $node) { |
|
51 | 22 | if ($node->getClassName() !== $className) { |
|
52 | 8 | continue; |
|
53 | } |
||
54 | |||
55 | 22 | return $node; |
|
56 | } |
||
57 | |||
58 | // Try to retrieve node by alias |
||
59 | 6 | foreach ($this->nodes as $node) { |
|
60 | 5 | if (!$node instanceof AliasedNode) { |
|
61 | 4 | continue; |
|
62 | } |
||
63 | |||
64 | 2 | if ($node->getAlias() !== $className) { |
|
65 | 2 | continue; |
|
66 | } |
||
67 | |||
68 | 1 | return $node; |
|
69 | } |
||
70 | |||
71 | 5 | return null; |
|
72 | } |
||
73 | |||
74 | /** |
||
75 | * @return list<TypeNode|InterfaceTypeNode> |
||
76 | */ |
||
77 | 2 | public function getNodesImplementingInterface(): array |
|
78 | { |
||
79 | 2 | $nodes = []; |
|
80 | |||
81 | 2 | foreach ($this->nodes as $node) { |
|
82 | 2 | if (!$node instanceof InterfaceTypeNode && !$node instanceof TypeNode) { |
|
83 | 2 | continue; |
|
84 | } |
||
85 | |||
86 | 2 | if ($node->implementsInterfaces === []) { |
|
87 | 1 | continue; |
|
88 | } |
||
89 | |||
90 | 2 | $nodes[] = $node; |
|
91 | } |
||
92 | |||
93 | 2 | return $nodes; |
|
94 | } |
||
95 | |||
96 | /** |
||
97 | * @return AstPayload |
||
98 | */ |
||
99 | 1 | public function toArray(): array |
|
100 | { |
||
101 | 1 | $nodes = []; |
|
102 | 1 | foreach ($this->nodes as $node) { |
|
103 | 1 | $nodes[] = [ |
|
104 | 1 | 'node' => $node::class, |
|
105 | 1 | 'payload' => $node->toArray(), |
|
106 | 1 | ]; |
|
107 | } |
||
108 | |||
109 | 1 | return [ |
|
110 | 1 | 'nodes' => $nodes, |
|
111 | 1 | ]; |
|
112 | } |
||
113 | |||
114 | /** |
||
115 | * @param AstPayload $payload |
||
116 | */ |
||
117 | 1 | public static function fromArray(array $payload): Ast |
|
118 | { |
||
119 | 1 | $nodes = []; |
|
120 | 1 | foreach ($payload['nodes'] as $nodePayload) { |
|
121 | /** @var Node $nodeClassName */ |
||
122 | 1 | $nodeClassName = $nodePayload['node']; |
|
123 | 1 | $nodes[] = $nodeClassName::fromArray($nodePayload['payload']); |
|
124 | } |
||
125 | |||
126 | /** @var list<Node> $nodes */ |
||
127 | 1 | return new self(...$nodes); |
|
128 | } |
||
129 | } |
||
130 |