ParseException.php (3589B)
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 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 int $parsedLine The line where the error occurred 33 * @param int $snippet The snippet of code near the problem 34 * @param string $parsedFile 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 $snippet The code snippet 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 int 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 int $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 if (PHP_VERSION_ID >= 50400) { 129 $jsonOptions = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE; 130 } else { 131 $jsonOptions = 0; 132 } 133 $this->message .= sprintf(' in %s', json_encode($this->parsedFile, $jsonOptions)); 134 } 135 136 if ($this->parsedLine >= 0) { 137 $this->message .= sprintf(' at line %d', $this->parsedLine); 138 } 139 140 if ($this->snippet) { 141 $this->message .= sprintf(' (near "%s")', $this->snippet); 142 } 143 144 if ($dot) { 145 $this->message .= '.'; 146 } 147 } 148 }