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