ParseException.php 3.3 KB

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