SyntaxError.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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\ExpressionLanguage;
  11. class SyntaxError extends \LogicException
  12. {
  13. public function __construct(string $message, int $cursor = 0, string $expression = '', string $subject = null, array $proposals = null)
  14. {
  15. $message = sprintf('%s around position %d', rtrim($message, '.'), $cursor);
  16. if ($expression) {
  17. $message = sprintf('%s for expression `%s`', $message, $expression);
  18. }
  19. $message .= '.';
  20. if (null !== $subject && null !== $proposals) {
  21. $minScore = \INF;
  22. foreach ($proposals as $proposal) {
  23. $distance = levenshtein($subject, $proposal);
  24. if ($distance < $minScore) {
  25. $guess = $proposal;
  26. $minScore = $distance;
  27. }
  28. }
  29. if (isset($guess) && $minScore < 3) {
  30. $message .= sprintf(' Did you mean "%s"?', $guess);
  31. }
  32. }
  33. parent::__construct($message);
  34. }
  35. }