1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Symfony\Component\Yaml\Exception;
13
14 15 16 17 18
19 class ParseException extends RuntimeException
20 {
21 private $parsedFile;
22 private $parsedLine;
23 private $snippet;
24 private $rawMessage;
25
26 27 28 29 30 31 32 33 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 49 50 51
52 public function getSnippet()
53 {
54 return $this->snippet;
55 }
56
57 58 59 60 61
62 public function setSnippet($snippet)
63 {
64 $this->snippet = $snippet;
65
66 $this->updateRepr();
67 }
68
69 70 71 72 73 74 75
76 public function getParsedFile()
77 {
78 return $this->parsedFile;
79 }
80
81 82 83 84 85
86 public function setParsedFile($parsedFile)
87 {
88 $this->parsedFile = $parsedFile;
89
90 $this->updateRepr();
91 }
92
93 94 95 96 97
98 public function getParsedLine()
99 {
100 return $this->parsedLine;
101 }
102
103 104 105 106 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 }
147