FormatExceptionContext.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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\Dotenv\Exception;
  11. /**
  12. * @author Fabien Potencier <fabien@symfony.com>
  13. */
  14. final class FormatExceptionContext
  15. {
  16. private $data;
  17. private $path;
  18. private $lineno;
  19. private $cursor;
  20. public function __construct(string $data, string $path, int $lineno, int $cursor)
  21. {
  22. $this->data = $data;
  23. $this->path = $path;
  24. $this->lineno = $lineno;
  25. $this->cursor = $cursor;
  26. }
  27. public function getPath(): string
  28. {
  29. return $this->path;
  30. }
  31. public function getLineno(): int
  32. {
  33. return $this->lineno;
  34. }
  35. public function getDetails(): string
  36. {
  37. $before = str_replace("\n", '\n', substr($this->data, max(0, $this->cursor - 20), min(20, $this->cursor)));
  38. $after = str_replace("\n", '\n', substr($this->data, $this->cursor, 20));
  39. return '...'.$before.$after."...\n".str_repeat(' ', \strlen($before) + 2).'^ line '.$this->lineno.' offset '.$this->cursor;
  40. }
  41. }