prolab-api/vendor/symfony/yaml/Exception/ParseException.php

133 lines
3.1 KiB
PHP
Raw Normal View History

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