1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Scabbia2 Yaml Component |
4
|
|
|
* https://github.com/eserozvataf/scabbia2 |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
* |
9
|
|
|
* @link https://github.com/eserozvataf/scabbia2-yaml for the canonical source repository |
10
|
|
|
* @copyright 2010-2016 Eser Ozvataf. (http://eser.ozvataf.com/) |
11
|
|
|
* @license http://www.apache.org/licenses/LICENSE-2.0 - Apache License, Version 2.0 |
12
|
|
|
* |
13
|
|
|
* ------------------------- |
14
|
|
|
* Portions of this code are from Symfony YAML Component under the MIT license. |
15
|
|
|
* |
16
|
|
|
* (c) Fabien Potencier <[email protected]> |
17
|
|
|
* |
18
|
|
|
* For the full copyright and license information, please view the LICENSE-MIT |
19
|
|
|
* file that was distributed with this source code. |
20
|
|
|
* |
21
|
|
|
* Modifications made: |
22
|
|
|
* - Scabbia Framework code styles applied. |
23
|
|
|
* - All dump methods are moved under Dumper class. |
24
|
|
|
* - Redundant classes removed. |
25
|
|
|
* - Namespace changed. |
26
|
|
|
* - Tests ported to Scabbia2. |
27
|
|
|
* - Encoding checks removed. |
28
|
|
|
*/ |
29
|
|
|
|
30
|
|
|
namespace Scabbia\Yaml; |
31
|
|
|
|
32
|
|
|
use RuntimeException; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Exception class thrown when an error occurs during parsing |
36
|
|
|
* |
37
|
|
|
* @package Scabbia\Yaml |
38
|
|
|
* @author Fabien Potencier <[email protected]> |
39
|
|
|
* @author Eser Ozvataf <[email protected]> |
40
|
|
|
* @since 2.0.0 |
41
|
|
|
*/ |
42
|
|
|
class ParseException extends RuntimeException |
43
|
|
|
{ |
44
|
|
|
/** @type null|string $parsedFile The file name where the error occurred */ |
45
|
|
|
protected $parsedFile; |
46
|
|
|
/** @type int $parsedLine The line where the error occurred */ |
47
|
|
|
protected $parsedLine; |
48
|
|
|
/** @type int|null $snippet The snippet of code near the problem */ |
49
|
|
|
protected $snippet; |
50
|
|
|
/** @type string $rawMessage The error message */ |
51
|
|
|
protected $rawMessage; |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Constructor |
56
|
|
|
* |
57
|
|
|
* @param string $message The error message |
58
|
|
|
* @param int $parsedLine The line where the error occurred |
59
|
|
|
* @param int $snippet The snippet of code near the problem |
60
|
|
|
* @param string $parsedFile The file name where the error occurred |
61
|
|
|
* @param \Exception $previous The previous exception |
62
|
|
|
* |
63
|
|
|
* @return ParseException |
|
|
|
|
64
|
|
|
*/ |
65
|
|
|
public function __construct( |
66
|
|
|
$message, |
67
|
|
|
$parsedLine = -1, |
68
|
|
|
$snippet = null, |
69
|
|
|
$parsedFile = null, |
70
|
|
|
\Exception $previous = null |
71
|
|
|
) { |
72
|
|
|
$this->parsedFile = $parsedFile; |
73
|
|
|
$this->parsedLine = $parsedLine; |
74
|
|
|
$this->snippet = $snippet; |
75
|
|
|
$this->rawMessage = $message; |
76
|
|
|
|
77
|
|
|
$this->updateRepr(); |
78
|
|
|
|
79
|
|
|
parent::__construct($this->message, 0, $previous); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Gets the snippet of code near the error |
84
|
|
|
* |
85
|
|
|
* @return string The snippet of code |
86
|
|
|
*/ |
87
|
|
|
public function getSnippet() |
88
|
|
|
{ |
89
|
|
|
return $this->snippet; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Sets the snippet of code near the error |
94
|
|
|
* |
95
|
|
|
* @param string $snippet The code snippet |
96
|
|
|
* |
97
|
|
|
* @return void |
98
|
|
|
*/ |
99
|
|
|
public function setSnippet($snippet) |
100
|
|
|
{ |
101
|
|
|
$this->snippet = $snippet; |
|
|
|
|
102
|
|
|
|
103
|
|
|
$this->updateRepr(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Gets the filename where the error occurred |
108
|
|
|
* |
109
|
|
|
* This method returns null if a string is parsed. |
110
|
|
|
* |
111
|
|
|
* @return string The filename |
112
|
|
|
*/ |
113
|
|
|
public function getParsedFile() |
114
|
|
|
{ |
115
|
|
|
return $this->parsedFile; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Sets the filename where the error occurred |
120
|
|
|
* |
121
|
|
|
* @param string $parsedFile The filename |
122
|
|
|
* |
123
|
|
|
* @return void |
124
|
|
|
*/ |
125
|
|
|
public function setParsedFile($parsedFile) |
126
|
|
|
{ |
127
|
|
|
$this->parsedFile = $parsedFile; |
128
|
|
|
|
129
|
|
|
$this->updateRepr(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Gets the line where the error occurred |
134
|
|
|
* |
135
|
|
|
* @return int The file line |
136
|
|
|
*/ |
137
|
|
|
public function getParsedLine() |
138
|
|
|
{ |
139
|
|
|
return $this->parsedLine; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Sets the line where the error occurred |
144
|
|
|
* |
145
|
|
|
* @param int $parsedLine The file line |
146
|
|
|
*/ |
147
|
|
|
public function setParsedLine($parsedLine) |
148
|
|
|
{ |
149
|
|
|
$this->parsedLine = $parsedLine; |
150
|
|
|
|
151
|
|
|
$this->updateRepr(); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Updates the generated message |
156
|
|
|
* |
157
|
|
|
* @return void |
158
|
|
|
*/ |
159
|
|
|
protected function updateRepr() |
160
|
|
|
{ |
161
|
|
|
$this->message = $this->rawMessage; |
162
|
|
|
|
163
|
|
|
$dot = false; |
164
|
|
|
if (substr($this->message, -1) === ".") { |
165
|
|
|
$this->message = substr($this->message, 0, -1); |
166
|
|
|
$dot = true; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
if ($this->parsedFile !== null) { |
170
|
|
|
$this->message .= sprintf( |
171
|
|
|
" in %s", |
172
|
|
|
json_encode($this->parsedFile, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) |
173
|
|
|
); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
if ($this->parsedLine >= 0) { |
177
|
|
|
$this->message .= sprintf(" at line %d", $this->parsedLine); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
if ($this->snippet) { |
|
|
|
|
181
|
|
|
$this->message .= sprintf(" (near \"%s\")", $this->snippet); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
if ($dot) { |
185
|
|
|
$this->message .= "."; |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.