Token.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 token.
  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 Token
  22. {
  23. public const TYPE_FILE_END = 'eof';
  24. public const TYPE_DELIMITER = 'delimiter';
  25. public const TYPE_WHITESPACE = 'whitespace';
  26. public const TYPE_IDENTIFIER = 'identifier';
  27. public const TYPE_HASH = 'hash';
  28. public const TYPE_NUMBER = 'number';
  29. public const TYPE_STRING = 'string';
  30. public function __construct(
  31. private ?string $type,
  32. private ?string $value,
  33. private ?int $position,
  34. ) {
  35. }
  36. public function getType(): ?int
  37. {
  38. return $this->type;
  39. }
  40. public function getValue(): ?string
  41. {
  42. return $this->value;
  43. }
  44. public function getPosition(): ?int
  45. {
  46. return $this->position;
  47. }
  48. public function isFileEnd(): bool
  49. {
  50. return self::TYPE_FILE_END === $this->type;
  51. }
  52. public function isDelimiter(array $values = []): bool
  53. {
  54. if (self::TYPE_DELIMITER !== $this->type) {
  55. return false;
  56. }
  57. if (!$values) {
  58. return true;
  59. }
  60. return \in_array($this->value, $values, true);
  61. }
  62. public function isWhitespace(): bool
  63. {
  64. return self::TYPE_WHITESPACE === $this->type;
  65. }
  66. public function isIdentifier(): bool
  67. {
  68. return self::TYPE_IDENTIFIER === $this->type;
  69. }
  70. public function isHash(): bool
  71. {
  72. return self::TYPE_HASH === $this->type;
  73. }
  74. public function isNumber(): bool
  75. {
  76. return self::TYPE_NUMBER === $this->type;
  77. }
  78. public function isString(): bool
  79. {
  80. return self::TYPE_STRING === $this->type;
  81. }
  82. public function __toString(): string
  83. {
  84. if ($this->value) {
  85. return \sprintf('<%s "%s" at %s>', $this->type, $this->value, $this->position);
  86. }
  87. return \sprintf('<%s at %s>', $this->type, $this->position);
  88. }
  89. }