Token.php 2.4 KB

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