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($propertyName): self
12    {
13        $this->propertyList[] = new GraphQLElement($propertyName);
14        return $this;
15    }
16
17    protected function getLastKey()
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        $first = (!$first) ? new GraphQLNode('first') : $first;
27        return $first;
28    }
29
30    public function getLastElement(): GraphQLElement
31    {
32        $lastkey = $this->getLastKey();
33        $element = $this->propertyList[$lastkey];
34        return $element;
35    }
36
37    public function addSubNode(): self
38    {
39        $lastkey = $this->getLastKey();
40        if (false === $lastkey) {
41            // root node
42            $node = new self();
43        } else {
44            $node = new self($this->getLastElement()->propertyName);
45        }
46        $node->setParent($this);
47        $this->propertyList[$lastkey] = $node;
48        return $node;
49    }
50
51    public function setParent(self $parent): self
52    {
53        $this->parent = $parent;
54        return $this;
55    }
56
57    public function hasParent(): bool
58    {
59        return ($this->parent) ? true : false;
60    }
61
62    public function getParent(): self
63    {
64        if (!$this->hasParent()) {
65            throw new GraphQLException("Curly bracket match problem, too many closing brackets");
66        }
67        return $this->parent;
68    }
69
70    public function getRealRoot(): self
71    {
72        if ($this->getFirstElement()->propertyName == '__root') {
73            return $this->getFirstElement()->getRealRoot();
74        }
75        return $this;
76    }
77
78    public function getNodesFromIterable($data): array
79    {
80        $reduced = [];
81        if (is_array($data) && array_values($data) === $data) {
82            foreach ($data as $item) {
83                $reduced[] = $this->getNodesFromIterable($item);
84            }
85        } else {
86            foreach ($this->propertyList as $element) {
87                $propertyName = $element->propertyName;
88                if (isset($data[$propertyName])) {
89                    if ($element instanceof self) {
90                        $reduced[$propertyName] = $element->getNodesFromIterable($data[$propertyName]);
91                    } else {
92                        $reduced[$propertyName] = $data[$propertyName];
93                    }
94                }
95            }
96        }
97        return $reduced;
98    }
99}