b74eb9132f513babfaf369568ed5c2bec77cc335
[yaffs-website] / vendor / symfony / yaml / Exception / ParseException.php
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 class ParseException extends RuntimeException
20 {
21     private $parsedFile;
22     private $parsedLine;
23     private $snippet;
24     private $rawMessage;
25
26     /**
27      * Constructor.
28      *
29      * @param string     $message    The error message
30      * @param int        $parsedLine The line where the error occurred
31      * @param int        $snippet    The snippet of code near the problem
32      * @param string     $parsedFile The file name where the error occurred
33      * @param \Exception $previous   The previous exception
34      */
35     public function __construct($message, $parsedLine = -1, $snippet = null, $parsedFile = null, \Exception $previous = null)
36     {
37         $this->parsedFile = $parsedFile;
38         $this->parsedLine = $parsedLine;
39         $this->snippet = $snippet;
40         $this->rawMessage = $message;
41
42         $this->updateRepr();
43
44         parent::__construct($this->message, 0, $previous);
45     }
46
47     /**
48      * Gets the snippet of code near the error.
49      *
50      * @return string The snippet of code
51      */
52     public function getSnippet()
53     {
54         return $this->snippet;
55     }
56
57     /**
58      * Sets the snippet of code near the error.
59      *
60      * @param string $snippet The code snippet
61      */
62     public function setSnippet($snippet)
63     {
64         $this->snippet = $snippet;
65
66         $this->updateRepr();
67     }
68
69     /**
70      * Gets the filename where the error occurred.
71      *
72      * This method returns null if a string is parsed.
73      *
74      * @return string The filename
75      */
76     public function getParsedFile()
77     {
78         return $this->parsedFile;
79     }
80
81     /**
82      * Sets the filename where the error occurred.
83      *
84      * @param string $parsedFile The filename
85      */
86     public function setParsedFile($parsedFile)
87     {
88         $this->parsedFile = $parsedFile;
89
90         $this->updateRepr();
91     }
92
93     /**
94      * Gets the line where the error occurred.
95      *
96      * @return int The file line
97      */
98     public function getParsedLine()
99     {
100         return $this->parsedLine;
101     }
102
103     /**
104      * Sets the line where the error occurred.
105      *
106      * @param int $parsedLine The file line
107      */
108     public function setParsedLine($parsedLine)
109     {
110         $this->parsedLine = $parsedLine;
111
112         $this->updateRepr();
113     }
114
115     private function updateRepr()
116     {
117         $this->message = $this->rawMessage;
118
119         $dot = false;
120         if ('.' === substr($this->message, -1)) {
121             $this->message = substr($this->message, 0, -1);
122             $dot = true;
123         }
124
125         if (null !== $this->parsedFile) {
126             if (PHP_VERSION_ID >= 50400) {
127                 $jsonOptions = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE;
128             } else {
129                 $jsonOptions = 0;
130             }
131             $this->message .= sprintf(' in %s', json_encode($this->parsedFile, $jsonOptions));
132         }
133
134         if ($this->parsedLine >= 0) {
135             $this->message .= sprintf(' at line %d', $this->parsedLine);
136         }
137
138         if ($this->snippet) {
139             $this->message .= sprintf(' (near "%s")', $this->snippet);
140         }
141
142         if ($dot) {
143             $this->message .= '.';
144         }
145     }
146 }