ParseException.php 3.2 KB

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