Decoder::endArray()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
declare(strict_types=1);
3
namespace JsonStreamParser;
4
5
use JsonStreamParser\Decoder\Element;
6
7
/**
8
 * @author  Stefan Pöhner <[email protected]>
9
 * @license MIT
10
 *
11
 * @package JsonStreamParser
12
 */
13
class Decoder
14
{
15
	/**
16
	 * @var mixed
17
	 */
18
	private $result;
19
20
	/**
21
	 * @var Element
22
	 */
23
	private $current;
24
25
	public function endOfStream()
26
	{
27
	}
28
29
	public function beginObject()
30
	{
31
		$this->createChild(Element::TYPE_OBJECT, new \stdClass());
32
	}
33
34
	public function endObject()
35
	{
36
		if ($this->current->type != Element::TYPE_OBJECT) {
37
			throw new \UnexpectedValueException('There is no object to end.');
38
		}
39
40
		$this->moveUp();
41
	}
42
43
	public function beginArray()
44
	{
45
		$this->createChild(Element::TYPE_ARRAY, []);
46
	}
47
48
	public function endArray()
49
	{
50
		if ($this->current->type != Element::TYPE_ARRAY) {
51
			throw new \UnexpectedValueException('There is no array to end.');
52
		}
53
54
		$this->moveUp();
55
	}
56
57
	/**
58
	 * @param mixed $value
59
	 */
60
	public function appendValue($value)
61
	{
62
		if ($this->current instanceof Element) {
63
			if ($this->current->type == Element::TYPE_ARRAY) {
64
				$this->current->value[] = $value;
65
			} elseif ($this->current->type == Element::TYPE_OBJECT) {
66
				$this->createChild(Element::TYPE_KEY, $value);
67
			} elseif ($this->current->type == Element::TYPE_KEY) {
68
				// get the key back
69
				$key = $this->current->value;
70
				// move up to object
71
				$this->current = $this->current->parent;
72
				// append key value pair
73
				$this->current->value->$key = $value;
74
			}
75
		} else {
76
			$this->result = $value;
77
		}
78
	}
79
80
	public function whitespace($char)
0 ignored issues
show
Unused Code introduced by
The parameter $char is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
81
	{
82
	}
83
84
	public function keyValueSeparator()
85
	{
86
		if (!$this->current instanceof Element || $this->current->type != Element::TYPE_KEY) {
87
			throw new \UnexpectedValueException('Not in object context.');
88
		}
89
	}
90
91
	public function arraySeparator()
92
	{
93
		if (!$this->current instanceof Element || !in_array($this->current->type, [Element::TYPE_ARRAY, Element::TYPE_OBJECT])) {
94
			throw new \UnexpectedValueException('Not in array or object context.');
95
		}
96
	}
97
98
	/**
99
	 * @return mixed
100
	 */
101
	public function getResult()
102
	{
103
		return $this->result;
104
	}
105
106
	/**
107
	 * Move up the tree.
108
	 */
109
	private function moveUp()
110
	{
111
		$value = $this->current->value;
112
		if ($this->current->parent) {
113
			$this->current = $this->current->parent;
114
		} else {
115
			$this->current = null;
116
		}
117
118
		$this->appendValue($value);
119
	}
120
121
	/**
122
	 * @param int   $type
123
	 * @param mixed $value
124
	 */
125
	private function createChild(int $type, $value)
126
	{
127
		$element         = new Element($type);
128
		$element->parent = $this->current;
129
		$element->value  = $value;
130
		$this->current   = $element;
131
	}
132
}
133