1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Swaggest\JsonCli\FilePosition; |
4
|
|
|
|
5
|
|
|
use JsonStreamingParser\Listener; |
6
|
|
|
use Swaggest\JsonDiff\JsonPointer; |
7
|
|
|
|
8
|
|
|
class PositionResolver implements Listener |
9
|
|
|
{ |
10
|
|
|
/** @var string[] map of pointer to file positions */ |
11
|
|
|
public $resolved; |
12
|
|
|
private $pathState; |
13
|
|
|
private $stack = []; |
14
|
|
|
|
15
|
1 |
|
public function __construct() |
16
|
|
|
{ |
17
|
1 |
|
$this->pathState = new PathState(); |
18
|
1 |
|
$this->pathState->path = ''; |
19
|
1 |
|
} |
20
|
|
|
|
21
|
1 |
|
public function startDocument() |
22
|
|
|
{ |
23
|
1 |
|
} |
24
|
|
|
|
25
|
1 |
|
public function endDocument() |
26
|
|
|
{ |
27
|
1 |
|
} |
28
|
|
|
|
29
|
1 |
|
public function startObject() |
30
|
|
|
{ |
31
|
1 |
|
$path = $this->pathState->path; |
32
|
1 |
|
if ($this->pathState->isArray) { |
33
|
1 |
|
$index = $this->pathState->arrayIndex++; |
34
|
1 |
|
$path = $this->pathState->path . '/' . $index; |
35
|
1 |
|
$this->resolved[$path] = $this->currentLine . ':' . $this->currentChar; |
36
|
|
|
} |
37
|
|
|
|
38
|
1 |
|
$this->stack[] = $this->pathState; |
39
|
1 |
|
$pathState = new PathState(); |
40
|
1 |
|
$pathState->path = $path; |
41
|
1 |
|
$this->pathState = $pathState; |
42
|
1 |
|
} |
43
|
|
|
|
44
|
1 |
|
public function endObject() |
45
|
|
|
{ |
46
|
1 |
|
$this->pathState = array_pop($this->stack); |
47
|
1 |
|
if ($this->pathState->isKey) { |
48
|
1 |
|
$this->pathState = array_pop($this->stack); |
49
|
|
|
} |
50
|
1 |
|
} |
51
|
|
|
|
52
|
|
|
|
53
|
1 |
|
public function startArray() |
54
|
|
|
{ |
55
|
1 |
|
$path = $this->pathState->path; |
56
|
1 |
|
if ($this->pathState->isArray) { |
57
|
|
|
$index = $this->pathState->arrayIndex++; |
58
|
|
|
$path = $this->pathState->path . '/' . $index; |
59
|
|
|
$this->resolved[$path] = $this->currentLine . ':' . $this->currentChar; |
60
|
|
|
} |
61
|
|
|
|
62
|
1 |
|
$this->stack[] = $this->pathState; |
63
|
1 |
|
$pathState = new PathState(); |
64
|
1 |
|
$pathState->path = $path; |
65
|
1 |
|
$pathState->isArray = true; |
66
|
1 |
|
$this->pathState = $pathState; |
67
|
1 |
|
} |
68
|
|
|
|
69
|
1 |
|
public function endArray() |
70
|
|
|
{ |
71
|
1 |
|
$this->pathState = array_pop($this->stack); |
72
|
1 |
|
if ($this->pathState->isKey) { |
73
|
1 |
|
$this->pathState = array_pop($this->stack); |
74
|
|
|
} |
75
|
1 |
|
} |
76
|
|
|
|
77
|
1 |
|
public function key($key) |
78
|
|
|
{ |
79
|
1 |
|
$path = $this->pathState->path . '/' . JsonPointer::escapeSegment($key); |
80
|
|
|
|
81
|
1 |
|
$this->stack[] = $this->pathState; |
82
|
1 |
|
$pathState = new PathState(); |
83
|
1 |
|
$pathState->path = $path; |
84
|
1 |
|
$pathState->isKey = true; |
85
|
1 |
|
$this->pathState = $pathState; |
86
|
|
|
|
87
|
|
|
|
88
|
1 |
|
$this->resolved[$path] = $this->currentLine . ':' . $this->currentChar; |
89
|
1 |
|
} |
90
|
|
|
|
91
|
1 |
|
public function value($value) |
92
|
|
|
{ |
93
|
1 |
|
if ($this->pathState->isArray) { |
94
|
1 |
|
$index = $this->pathState->arrayIndex++; |
95
|
1 |
|
$itemPath = $this->pathState->path . '/' . $index; |
96
|
1 |
|
$this->resolved[$itemPath] = $this->currentLine . ':' . $this->currentChar; |
97
|
1 |
|
} elseif ($this->pathState->isKey) { |
98
|
1 |
|
$this->pathState = array_pop($this->stack); |
99
|
|
|
} |
100
|
1 |
|
} |
101
|
|
|
|
102
|
|
|
public function whitespace($whitespace) |
103
|
|
|
{ |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
private $currentLine; |
107
|
|
|
private $currentChar; |
108
|
|
|
|
109
|
1 |
|
/** |
110
|
|
|
* @param int $line |
111
|
1 |
|
* @param int $char |
112
|
1 |
|
*/ |
113
|
1 |
|
public function filePosition($line, $char) |
114
|
|
|
{ |
115
|
|
|
$this->currentLine = $line; |
116
|
|
|
$this->currentChar = $char; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
} |