Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Yaml\Exception; Chris@0: Chris@0: /** Chris@0: * Exception class thrown when an error occurs during parsing. Chris@0: * Chris@0: * @author Fabien Potencier Chris@0: */ Chris@0: class ParseException extends RuntimeException Chris@0: { Chris@0: private $parsedFile; Chris@0: private $parsedLine; Chris@0: private $snippet; Chris@0: private $rawMessage; Chris@0: Chris@0: /** Chris@0: * @param string $message The error message Chris@0: * @param int $parsedLine The line where the error occurred Chris@0: * @param string|null $snippet The snippet of code near the problem Chris@0: * @param string|null $parsedFile The file name where the error occurred Chris@0: * @param \Exception|null $previous The previous exception Chris@0: */ Chris@0: public function __construct($message, $parsedLine = -1, $snippet = null, $parsedFile = null, \Exception $previous = null) Chris@0: { Chris@0: $this->parsedFile = $parsedFile; Chris@0: $this->parsedLine = $parsedLine; Chris@0: $this->snippet = $snippet; Chris@0: $this->rawMessage = $message; Chris@0: Chris@0: $this->updateRepr(); Chris@0: Chris@0: parent::__construct($this->message, 0, $previous); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the snippet of code near the error. Chris@0: * Chris@0: * @return string The snippet of code Chris@0: */ Chris@0: public function getSnippet() Chris@0: { Chris@0: return $this->snippet; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the snippet of code near the error. Chris@0: * Chris@0: * @param string $snippet The code snippet Chris@0: */ Chris@0: public function setSnippet($snippet) Chris@0: { Chris@0: $this->snippet = $snippet; Chris@0: Chris@0: $this->updateRepr(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the filename where the error occurred. Chris@0: * Chris@0: * This method returns null if a string is parsed. Chris@0: * Chris@0: * @return string The filename Chris@0: */ Chris@0: public function getParsedFile() Chris@0: { Chris@0: return $this->parsedFile; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the filename where the error occurred. Chris@0: * Chris@0: * @param string $parsedFile The filename Chris@0: */ Chris@0: public function setParsedFile($parsedFile) Chris@0: { Chris@0: $this->parsedFile = $parsedFile; Chris@0: Chris@0: $this->updateRepr(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the line where the error occurred. Chris@0: * Chris@0: * @return int The file line Chris@0: */ Chris@0: public function getParsedLine() Chris@0: { Chris@0: return $this->parsedLine; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the line where the error occurred. Chris@0: * Chris@0: * @param int $parsedLine The file line Chris@0: */ Chris@0: public function setParsedLine($parsedLine) Chris@0: { Chris@0: $this->parsedLine = $parsedLine; Chris@0: Chris@0: $this->updateRepr(); Chris@0: } Chris@0: Chris@0: private function updateRepr() Chris@0: { Chris@0: $this->message = $this->rawMessage; Chris@0: Chris@0: $dot = false; Chris@0: if ('.' === substr($this->message, -1)) { Chris@0: $this->message = substr($this->message, 0, -1); Chris@0: $dot = true; Chris@0: } Chris@0: Chris@0: if (null !== $this->parsedFile) { Chris@0: $this->message .= sprintf(' in %s', json_encode($this->parsedFile, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)); Chris@0: } Chris@0: Chris@0: if ($this->parsedLine >= 0) { Chris@0: $this->message .= sprintf(' at line %d', $this->parsedLine); Chris@0: } Chris@0: Chris@0: if ($this->snippet) { Chris@0: $this->message .= sprintf(' (near "%s")', $this->snippet); Chris@0: } Chris@0: Chris@0: if ($dot) { Chris@0: $this->message .= '.'; Chris@0: } Chris@0: } Chris@0: }