Nessuna descrizione

Reader.php 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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\CssSelector\Parser;
  11. /**
  12. * CSS selector reader.
  13. *
  14. * This component is a port of the Python cssselect library,
  15. * which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
  16. *
  17. * @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
  18. *
  19. * @internal
  20. */
  21. class Reader
  22. {
  23. private $source;
  24. private $length;
  25. private $position = 0;
  26. public function __construct(string $source)
  27. {
  28. $this->source = $source;
  29. $this->length = \strlen($source);
  30. }
  31. public function isEOF(): bool
  32. {
  33. return $this->position >= $this->length;
  34. }
  35. public function getPosition(): int
  36. {
  37. return $this->position;
  38. }
  39. public function getRemainingLength(): int
  40. {
  41. return $this->length - $this->position;
  42. }
  43. public function getSubstring(int $length, int $offset = 0): string
  44. {
  45. return substr($this->source, $this->position + $offset, $length);
  46. }
  47. public function getOffset(string $string)
  48. {
  49. $position = strpos($this->source, $string, $this->position);
  50. return false === $position ? false : $position - $this->position;
  51. }
  52. /**
  53. * @return array|false
  54. */
  55. public function findPattern(string $pattern)
  56. {
  57. $source = substr($this->source, $this->position);
  58. if (preg_match($pattern, $source, $matches)) {
  59. return $matches;
  60. }
  61. return false;
  62. }
  63. public function moveForward(int $length)
  64. {
  65. $this->position += $length;
  66. }
  67. public function moveToEnd()
  68. {
  69. $this->position = $this->length;
  70. }
  71. }