Lines
94.87%
37 / 39
Functions and Methods
80.00%
8 / 10
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| GraphQLNode | 94.87% | 37 / 39 | 80.00% | 8 / 10 | 21.06 | 0.00% | 0 / 1 | |||
| addElement | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | |||||
| getLastKey | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | |||||
| getFirstElement | 75.00% | 3 / 4 | 0.00% | 0 / 1 | 2.06 | |||||
| getLastElement | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 1 | |||||
| addSubNode | 100.00% | 7 / 7 | 100.00% | 1 / 1 | 2 | |||||
| setParent | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | |||||
| hasParent | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 2 | |||||
| getParent | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 2 | |||||
| getRealRoot | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 3 | |||||
| getNodesFromIterable | 100.00% | 11 / 11 | 100.00% | 1 / 1 | 7 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | namespace BO\Zmsclient\GraphQL; | |
| 4 | ||
| 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 | // root node | |
| 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 | /** @psalm-api */ | |
| 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 | } |