| 5 | | class GraphQLNode extends GraphQLElement |
| 6 | | { |
| 7 | | public array $propertyList = []; |
| 8 | | |
| 9 | | public ?self $parent = null; |
| 10 | | |
| 11 | | public function addElement(string $propertyName): self |
| 12 | | { |
| 13 | | $this->propertyList[] = new GraphQLElement($propertyName); |
| 14 | | return $this; |
| 15 | | } |
| 16 | | |
| 17 | | protected function getLastKey(): int|string|false |
| 18 | | { |
| 19 | | $keys = array_keys($this->propertyList); |
| 20 | | return end($keys); |
| 21 | | } |
| 22 | | |
| 23 | | public function getFirstElement(): GraphQLElement |
| 24 | | { |
| 25 | | $first = reset($this->propertyList); |
| 26 | | if ($first === false) { |
| 27 | | $first = new GraphQLNode('first'); |
| 28 | | } |
| 29 | | return $first; |
| 30 | | } |
| 31 | | |
| 32 | | public function getLastElement(): GraphQLElement |
| 33 | | { |
| 34 | | $lastkey = $this->getLastKey(); |
| 35 | | $element = $this->propertyList[$lastkey]; |
| 36 | | return $element; |
| 37 | | } |
| 38 | | |
| 39 | | public function addSubNode(): self |
| 40 | | { |
| 41 | | $lastkey = $this->getLastKey(); |
| 42 | | if (false === $lastkey) { |
| 43 | | |
| 44 | | $node = new self(); |
| 45 | | } else { |
| 46 | | $node = new self($this->getLastElement()->getPropertyName()); |
| 47 | | } |
| 48 | | $node->setParent($this); |
| 49 | | $this->propertyList[$lastkey] = $node; |
| 50 | | return $node; |
| 51 | | } |
| 52 | | |
| 53 | | public function setParent(self $parent): self |
| 54 | | { |
| 55 | | $this->parent = $parent; |
| 56 | | return $this; |
| 57 | | } |
| 58 | | |
| 59 | | public function hasParent(): bool |
| 60 | | { |
| 61 | | return $this->parent instanceof self; |
| 62 | | } |
| 63 | | |
| 64 | | public function getParent(): self |
| 65 | | { |
| 66 | | if (!$this->parent instanceof self) { |
| 67 | | throw new GraphQLException("Curly bracket match problem, too many closing brackets"); |
| 68 | | } |
| 69 | | return $this->parent; |
| 70 | | } |
| 71 | | |
| 72 | | public function getRealRoot(): self |
| 73 | | { |
| 74 | | $first = $this->getFirstElement(); |
| 75 | | if ($first->getPropertyName() == '__root' && $first instanceof self) { |
| 76 | | return $first->getRealRoot(); |
| 77 | | } |
| 78 | | return $this; |
| 79 | | } |
| 80 | | |
| 81 | | |
| 82 | | public function getNodesFromIterable(mixed $data): array |
| 83 | | { |
| 84 | | $reduced = []; |
| 85 | | if (is_array($data) && array_values($data) === $data) { |
| 86 | | foreach ($data as $item) { |
| 87 | | $reduced[] = $this->getNodesFromIterable($item); |
| 88 | | } |
| 89 | | } else { |
| 90 | | foreach ($this->propertyList as $element) { |
| 91 | | $propertyName = $element->getPropertyName(); |
| 92 | | if (isset($data[$propertyName])) { |
| 93 | | if ($element instanceof self) { |
| 94 | | $reduced[$propertyName] = $element->getNodesFromIterable($data[$propertyName]); |
| 95 | | } else { |
| 96 | | $reduced[$propertyName] = $data[$propertyName]; |
| 97 | | } |
| 98 | | } |
| 99 | | } |
| 100 | | } |
| 101 | | return $reduced; |
| 102 | | } |
| 103 | | } |