Listener   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 0
dl 0
loc 153
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A startDocument() 0 8 1
A endDocument() 0 4 1
A startObject() 0 6 1
A endObject() 0 12 2
A startArray() 0 4 1
A startCommon() 0 8 2
A endArray() 0 4 1
A endCommon() 0 26 5
A key() 0 4 1
A value() 0 13 2
A whitespace() 0 3 1
1
<?php
2
3
namespace JsonCollectionParser;
4
5
use JsonStreamingParser\Listener\ListenerInterface;
6
7
class Listener implements ListenerInterface
8
{
9
    /**
10
     * @var array
11
     */
12
    protected $stack;
13
14
    /**
15
     * @var string
16
     */
17
    protected $key;
18
19
    /**
20
     * @var int
21
     */
22
    protected $level;
23
24
    /**
25
     * @var int
26
     */
27
    protected $objectLevel;
28
29
    /**
30
     * @var array
31
     */
32
    protected $objectKeys;
33
34
    /**
35
     * @var callback|callable
36
     */
37
    protected $callback;
38
39
    /**
40
     * @var bool
41
     */
42
    protected $assoc;
43
44
    /**
45
     * @param callback|callable $callback callback for parsed collection item
46
     * @param bool $assoc When true, returned objects will be converted into associative arrays
47
     */
48
    public function __construct($callback, $assoc = true)
49
    {
50
        $this->callback = $callback;
51
        $this->assoc = $assoc;
52
    }
53
54
    public function startDocument(): void
55
    {
56
        $this->stack = [];
57
        $this->key = null;
58
        $this->objectLevel = 0;
59
        $this->level = 0;
60
        $this->objectKeys = [];
61
    }
62
63
    public function endDocument(): void
64
    {
65
        $this->stack = [];
66
    }
67
68
    public function startObject(): void
69
    {
70
        $this->objectLevel++;
71
72
        $this->startCommon();
73
    }
74
75
    public function endObject(): void
76
    {
77
        $this->endCommon();
78
79
        $this->objectLevel--;
80
        if ($this->objectLevel === 0) {
81
            $obj = array_pop($this->stack);
82
            $obj = reset($obj);
83
84
            call_user_func($this->callback, $obj);
85
        }
86
    }
87
88
    public function startArray(): void
89
    {
90
        $this->startCommon();
91
    }
92
93
    public function startCommon(): void
94
    {
95
        $this->level++;
96
        $this->objectKeys[$this->level] = ($this->key) ? $this->key : null;
97
        $this->key = null;
98
99
        array_push($this->stack, []);
100
    }
101
102
    public function endArray(): void
103
    {
104
        $this->endCommon(false);
105
    }
106
107
    public function endCommon($isObject = true): void
108
    {
109
        $obj = array_pop($this->stack);
110
111
        if ($isObject && !$this->assoc) {
112
            $obj = (object)$obj;
113
        }
114
115
        if (!empty($this->stack)) {
116
            $parentObj = array_pop($this->stack);
117
118
            if ($this->objectKeys[$this->level]) {
119
                $objectKey = $this->objectKeys[$this->level];
120
                $parentObj[$objectKey] = $obj;
121
                unset($this->objectKeys[$this->level]);
122
            } else {
123
                $parentObj[] = $obj;
124
            }
125
        } else {
126
            $parentObj = [$obj];
127
        }
128
129
        array_push($this->stack, $parentObj);
130
131
        $this->level--;
132
    }
133
134
    public function key(string $key): void
135
    {
136
        $this->key = $key;
137
    }
138
139
    /**
140
     * @param mixed $value
141
     */
142
    public function value($value): void
143
    {
144
        $obj = array_pop($this->stack);
145
146
        if ($this->key) {
147
            $obj[$this->key] = $value;
148
            $this->key = null;
149
        } else {
150
            array_push($obj, $value);
151
        }
152
153
        array_push($this->stack, $obj);
154
    }
155
156
    public function whitespace(string $whitespace): void
157
    {
158
    }
159
}
160