1 | <?php |
---|
2 | |
---|
3 | /* |
---|
4 | * This file is part of the Symfony package. |
---|
5 | * |
---|
6 | * (c) Fabien Potencier <fabien@symfony.com> |
---|
7 | * |
---|
8 | * For the full copyright and license information, please view the LICENSE |
---|
9 | * file that was distributed with this source code. |
---|
10 | */ |
---|
11 | |
---|
12 | namespace Symfony\Component\Yaml\Exception; |
---|
13 | |
---|
14 | /** |
---|
15 | * Exception class thrown when an error occurs during parsing. |
---|
16 | * |
---|
17 | * @author Fabien Potencier <fabien@symfony.com> |
---|
18 | * |
---|
19 | * @api |
---|
20 | */ |
---|
21 | class ParseException extends \RuntimeException implements ExceptionInterface |
---|
22 | { |
---|
23 | private $parsedFile; |
---|
24 | private $parsedLine; |
---|
25 | private $snippet; |
---|
26 | private $rawMessage; |
---|
27 | |
---|
28 | /** |
---|
29 | * Constructor. |
---|
30 | * |
---|
31 | * @param string $message The error message |
---|
32 | * @param integer $lineno The line where the error occurred |
---|
33 | * @param integer $snippet The snippet of code near the problem |
---|
34 | * @param string $filename The file name where the error occurred |
---|
35 | * @param Exception $previous The previous exception |
---|
36 | */ |
---|
37 | public function __construct($message, $parsedLine = -1, $snippet = null, $parsedFile = null, Exception $previous = null) |
---|
38 | { |
---|
39 | $this->parsedFile = $parsedFile; |
---|
40 | $this->parsedLine = $parsedLine; |
---|
41 | $this->snippet = $snippet; |
---|
42 | $this->rawMessage = $message; |
---|
43 | |
---|
44 | $this->updateRepr(); |
---|
45 | |
---|
46 | parent::__construct($this->message, 0, $previous); |
---|
47 | } |
---|
48 | |
---|
49 | /** |
---|
50 | * Gets the snippet of code near the error. |
---|
51 | * |
---|
52 | * @return string The snippet of code |
---|
53 | */ |
---|
54 | public function getSnippet() |
---|
55 | { |
---|
56 | return $this->snippet; |
---|
57 | } |
---|
58 | |
---|
59 | /** |
---|
60 | * Sets the snippet of code near the error. |
---|
61 | * |
---|
62 | * @param string $parsedFile The filename |
---|
63 | */ |
---|
64 | public function setSnippet($snippet) |
---|
65 | { |
---|
66 | $this->snippet = $snippet; |
---|
67 | |
---|
68 | $this->updateRepr(); |
---|
69 | } |
---|
70 | |
---|
71 | /** |
---|
72 | * Gets the filename where the error occurred. |
---|
73 | * |
---|
74 | * This method returns null if a string is parsed. |
---|
75 | * |
---|
76 | * @return string The filename |
---|
77 | */ |
---|
78 | public function getParsedFile() |
---|
79 | { |
---|
80 | return $this->parsedFile; |
---|
81 | } |
---|
82 | |
---|
83 | /** |
---|
84 | * Sets the filename where the error occurred. |
---|
85 | * |
---|
86 | * @param string $parsedFile The filename |
---|
87 | */ |
---|
88 | public function setParsedFile($parsedFile) |
---|
89 | { |
---|
90 | $this->parsedFile = $parsedFile; |
---|
91 | |
---|
92 | $this->updateRepr(); |
---|
93 | } |
---|
94 | |
---|
95 | /** |
---|
96 | * Gets the line where the error occurred. |
---|
97 | * |
---|
98 | * @return integer The file line |
---|
99 | */ |
---|
100 | public function getParsedLine() |
---|
101 | { |
---|
102 | return $this->parsedLine; |
---|
103 | } |
---|
104 | |
---|
105 | /** |
---|
106 | * Sets the line where the error occurred. |
---|
107 | * |
---|
108 | * @param integer $parsedLine The file line |
---|
109 | */ |
---|
110 | public function setParsedLine($parsedLine) |
---|
111 | { |
---|
112 | $this->parsedLine = $parsedLine; |
---|
113 | |
---|
114 | $this->updateRepr(); |
---|
115 | } |
---|
116 | |
---|
117 | private function updateRepr() |
---|
118 | { |
---|
119 | $this->message = $this->rawMessage; |
---|
120 | |
---|
121 | $dot = false; |
---|
122 | if ('.' === substr($this->message, -1)) { |
---|
123 | $this->message = substr($this->message, 0, -1); |
---|
124 | $dot = true; |
---|
125 | } |
---|
126 | |
---|
127 | if (null !== $this->parsedFile) { |
---|
128 | $this->message .= sprintf(' in %s', json_encode($this->parsedFile)); |
---|
129 | } |
---|
130 | |
---|
131 | if ($this->parsedLine >= 0) { |
---|
132 | $this->message .= sprintf(' at line %d', $this->parsedLine); |
---|
133 | } |
---|
134 | |
---|
135 | if ($this->snippet) { |
---|
136 | $this->message .= sprintf(' (near "%s")', $this->snippet); |
---|
137 | } |
---|
138 | |
---|
139 | if ($dot) { |
---|
140 | $this->message .= '.'; |
---|
141 | } |
---|
142 | } |
---|
143 | } |
---|