Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
37 / 37
100.00% covered (success)
100.00%
10 / 10
CRAP
100.00% covered (success)
100.00%
1 / 1
GraphQLNode
100.00% covered (success)
100.00%
37 / 37
100.00% covered (success)
100.00%
10 / 10
21
100.00% covered (success)
100.00%
1 / 1
 addElement
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getLastKey
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getFirstElement
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getLastElement
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 addSubNode
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 setParent
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 hasParent
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 getParent
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getRealRoot
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getNodesFromIterable
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
7
1<?php
2
3namespace BO\Zmsclient\GraphQL;
4
5class GraphQLNode extends GraphQLElement
6{
7    public $propertyList = [];
8
9    public $parent;
10
11    public function addElement(string $propertyName): self
12    {
13        $this->propertyList[] = new GraphQLElement($propertyName);
14        return $this;
15    }
16
17    /**
18     * @return false|key-of<TArray>
19     */
20    protected function getLastKey()
21    {
22        $keys = array_keys($this->propertyList);
23        return end($keys);
24    }
25
26    public function getFirstElement(): GraphQLElement
27    {
28        $first = reset($this->propertyList);
29        $first = (!$first) ? new GraphQLNode('first') : $first;
30        return $first;
31    }
32
33    public function getLastElement(): GraphQLElement
34    {
35        $lastkey = $this->getLastKey();
36        $element = $this->propertyList[$lastkey];
37        return $element;
38    }
39
40    public function addSubNode(): self
41    {
42        $lastkey = $this->getLastKey();
43        if (false === $lastkey) {
44            // root node
45            $node = new self();
46        } else {
47            $node = new self($this->getLastElement()->propertyName);
48        }
49        $node->setParent($this);
50        $this->propertyList[$lastkey] = $node;
51        return $node;
52    }
53
54    public function setParent(self $parent): self
55    {
56        $this->parent = $parent;
57        return $this;
58    }
59
60    public function hasParent(): bool
61    {
62        return ($this->parent) ? true : false;
63    }
64
65    public function getParent(): self
66    {
67        if (!$this->hasParent()) {
68            throw new GraphQLException("Curly bracket match problem, too many closing brackets");
69        }
70        return $this->parent;
71    }
72
73    public function getRealRoot(): self
74    {
75        if ($this->getFirstElement()->propertyName == '__root') {
76            return $this->getFirstElement()->getRealRoot();
77        }
78        return $this;
79    }
80
81    /** @psalm-api */
82    public function getNodesFromIterable($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->propertyName;
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}