ParseException.php 2.9 KB

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