Token.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. /**
  31. * @param self::TYPE_*|null $type
  32. */
  33. public function __construct(
  34. private ?string $type,
  35. private ?string $value,
  36. private ?int $position,
  37. ) {
  38. }
  39. /**
  40. * @return self::TYPE_*|null
  41. */
  42. public function getType(): ?string
  43. {
  44. return $this->type;
  45. }
  46. public function getValue(): ?string
  47. {
  48. return $this->value;
  49. }
  50. public function getPosition(): ?int
  51. {
  52. return $this->position;
  53. }
  54. public function isFileEnd(): bool
  55. {
  56. return self::TYPE_FILE_END === $this->type;
  57. }
  58. public function isDelimiter(array $values = []): bool
  59. {
  60. if (self::TYPE_DELIMITER !== $this->type) {
  61. return false;
  62. }
  63. if (!$values) {
  64. return true;
  65. }
  66. return \in_array($this->value, $values, true);
  67. }
  68. public function isWhitespace(): bool
  69. {
  70. return self::TYPE_WHITESPACE === $this->type;
  71. }
  72. public function isIdentifier(): bool
  73. {
  74. return self::TYPE_IDENTIFIER === $this->type;
  75. }
  76. public function isHash(): bool
  77. {
  78. return self::TYPE_HASH === $this->type;
  79. }
  80. public function isNumber(): bool
  81. {
  82. return self::TYPE_NUMBER === $this->type;
  83. }
  84. public function isString(): bool
  85. {
  86. return self::TYPE_STRING === $this->type;
  87. }
  88. public function __toString(): string
  89. {
  90. if ($this->value) {
  91. return \sprintf('<%s "%s" at %s>', $this->type, $this->value, $this->position);
  92. }
  93. return \sprintf('<%s at %s>', $this->type, $this->position);
  94. }
  95. }