Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
32 / 32
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
GraphQLInterpreter
100.00% covered (success)
100.00%
32 / 32
100.00% covered (success)
100.00%
6 / 6
16
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getGraphInterpretation
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
8
 reduceData
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
3
 setJson
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 jsonSerialize
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __toString
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace BO\Zmsclient\GraphQL;
4
5class GraphQLInterpreter implements \JsonSerializable
6{
7    protected string $gqlString;
8    protected mixed $data = null;
9
10    public function __construct(string $gqlString)
11    {
12        $this->gqlString = $gqlString;
13    }
14
15    protected function getGraphInterpretation(): GraphQLNode
16    {
17        $matchCount = preg_match_all('#\w+|[{}]#', $this->gqlString, $parts);
18        if ($matchCount === false || $matchCount === 0) {
19            throw new GraphQLException("No content for graph");
20        }
21        $parts = $parts[0];
22        if ($parts[0] != '{' || end($parts) != '}') {
23            throw new GraphQLException("No valid graphql");
24        }
25        $node = new GraphQLNode();
26        foreach ($parts as $part) {
27            if ($part == '{') {
28                $node = $node->addSubNode();
29            } elseif ($part == '}') {
30                $node = $node->getParent();
31            } else {
32                $node = $node->addElement($part);
33            }
34        }
35        $node = $node->getRealRoot();
36        return $node;
37    }
38
39    protected function reduceData(): self
40    {
41        $graph = $this->getGraphInterpretation();
42        $reducedData = $graph->getNodesFromIterable($this->data);
43        if (isset($this->data[0]['$schema'])) {
44            $schema = $this->data[0]['$schema'];
45            $reducedData[0]['$schema'] = $schema;
46        } elseif (isset($this->data['$schema'])) {
47            $schema = $this->data['$schema'];
48            $reducedData['$schema'] = $schema;
49        }
50        $this->data = $reducedData;
51
52        return $this;
53    }
54
55    public function setJson(string $json): self
56    {
57        $this->data = json_decode($json, true);
58        $this->reduceData();
59        return $this;
60    }
61
62    #[\Override]
63    public function jsonSerialize(): mixed
64    {
65        return $this->data;
66    }
67
68    public function __toString(): string
69    {
70        $encoded = json_encode($this);
71        return $encoded === false ? '' : $encoded;
72    }
73}