| 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 |
namespace WindPressDeps\Symfony\Component\Yaml\Exception; |
| 12 |
|
| 13 |
/** |
| 14 |
* Exception class thrown when an error occurs during parsing. |
| 15 |
* |
| 16 |
* @author Fabien Potencier <fabien@symfony.com> |
| 17 |
*/ |
| 18 |
class ParseException extends RuntimeException |
| 19 |
{ |
| 20 |
private $parsedFile; |
| 21 |
private $parsedLine; |
| 22 |
private $snippet; |
| 23 |
private $rawMessage; |
| 24 |
/** |
| 25 |
* @param string $message The error message |
| 26 |
* @param int $parsedLine The line where the error occurred |
| 27 |
* @param string|null $snippet The snippet of code near the problem |
| 28 |
* @param string|null $parsedFile The file name where the error occurred |
| 29 |
*/ |
| 30 |
public function __construct(string $message, int $parsedLine = -1, ?string $snippet = null, ?string $parsedFile = null, ?\Throwable $previous = null) |
| 31 |
{ |
| 32 |
$this->parsedFile = $parsedFile; |
| 33 |
$this->parsedLine = $parsedLine; |
| 34 |
$this->snippet = $snippet; |
| 35 |
$this->rawMessage = $message; |
| 36 |
$this->updateRepr(); |
| 37 |
parent::__construct($this->message, 0, $previous); |
| 38 |
} |
| 39 |
/** |
| 40 |
* Gets the snippet of code near the error. |
| 41 |
* |
| 42 |
* @return string |
| 43 |
*/ |
| 44 |
public function getSnippet() |
| 45 |
{ |
| 46 |
return $this->snippet; |
| 47 |
} |
| 48 |
/** |
| 49 |
* Sets the snippet of code near the error. |
| 50 |
*/ |
| 51 |
public function setSnippet(string $snippet) |
| 52 |
{ |
| 53 |
$this->snippet = $snippet; |
| 54 |
$this->updateRepr(); |
| 55 |
} |
| 56 |
/** |
| 57 |
* Gets the filename where the error occurred. |
| 58 |
* |
| 59 |
* This method returns null if a string is parsed. |
| 60 |
* |
| 61 |
* @return string |
| 62 |
*/ |
| 63 |
public function getParsedFile() |
| 64 |
{ |
| 65 |
return $this->parsedFile; |
| 66 |
} |
| 67 |
/** |
| 68 |
* Sets the filename where the error occurred. |
| 69 |
*/ |
| 70 |
public function setParsedFile(string $parsedFile) |
| 71 |
{ |
| 72 |
$this->parsedFile = $parsedFile; |
| 73 |
$this->updateRepr(); |
| 74 |
} |
| 75 |
/** |
| 76 |
* Gets the line where the error occurred. |
| 77 |
* |
| 78 |
* @return int |
| 79 |
*/ |
| 80 |
public function getParsedLine() |
| 81 |
{ |
| 82 |
return $this->parsedLine; |
| 83 |
} |
| 84 |
/** |
| 85 |
* Sets the line where the error occurred. |
| 86 |
*/ |
| 87 |
public function setParsedLine(int $parsedLine) |
| 88 |
{ |
| 89 |
$this->parsedLine = $parsedLine; |
| 90 |
$this->updateRepr(); |
| 91 |
} |
| 92 |
private function updateRepr() |
| 93 |
{ |
| 94 |
$this->message = $this->rawMessage; |
| 95 |
$dot = \false; |
| 96 |
if ('.' === substr($this->message, -1)) { |
| 97 |
$this->message = substr($this->message, 0, -1); |
| 98 |
$dot = \true; |
| 99 |
} |
| 100 |
if (null !== $this->parsedFile) { |
| 101 |
$this->message .= sprintf(' in %s', json_encode($this->parsedFile, \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE)); |
| 102 |
} |
| 103 |
if ($this->parsedLine >= 0) { |
| 104 |
$this->message .= sprintf(' at line %d', $this->parsedLine); |
| 105 |
} |
| 106 |
if ($this->snippet) { |
| 107 |
$this->message .= sprintf(' (near "%s")', $this->snippet); |
| 108 |
} |
| 109 |
if ($dot) { |
| 110 |
$this->message .= '.'; |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
|