Token.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. /**
  31. * @var int
  32. */
  33. private $type;
  34. /**
  35. * @var string
  36. */
  37. private $value;
  38. /**
  39. * @var int
  40. */
  41. private $position;
  42. /**
  43. * @param int $type
  44. * @param string $value
  45. * @param int $position
  46. */
  47. public function __construct($type, $value, $position)
  48. {
  49. $this->type = $type;
  50. $this->value = $value;
  51. $this->position = $position;
  52. }
  53. /**
  54. * @return int
  55. */
  56. public function getType()
  57. {
  58. return $this->type;
  59. }
  60. /**
  61. * @return string
  62. */
  63. public function getValue()
  64. {
  65. return $this->value;
  66. }
  67. /**
  68. * @return int
  69. */
  70. public function getPosition()
  71. {
  72. return $this->position;
  73. }
  74. /**
  75. * @return bool
  76. */
  77. public function isFileEnd()
  78. {
  79. return self::TYPE_FILE_END === $this->type;
  80. }
  81. /**
  82. * @param array $values
  83. *
  84. * @return bool
  85. */
  86. public function isDelimiter(array $values = array())
  87. {
  88. if (self::TYPE_DELIMITER !== $this->type) {
  89. return false;
  90. }
  91. if (empty($values)) {
  92. return true;
  93. }
  94. return in_array($this->value, $values);
  95. }
  96. /**
  97. * @return bool
  98. */
  99. public function isWhitespace()
  100. {
  101. return self::TYPE_WHITESPACE === $this->type;
  102. }
  103. /**
  104. * @return bool
  105. */
  106. public function isIdentifier()
  107. {
  108. return self::TYPE_IDENTIFIER === $this->type;
  109. }
  110. /**
  111. * @return bool
  112. */
  113. public function isHash()
  114. {
  115. return self::TYPE_HASH === $this->type;
  116. }
  117. /**
  118. * @return bool
  119. */
  120. public function isNumber()
  121. {
  122. return self::TYPE_NUMBER === $this->type;
  123. }
  124. /**
  125. * @return bool
  126. */
  127. public function isString()
  128. {
  129. return self::TYPE_STRING === $this->type;
  130. }
  131. /**
  132. * @return string
  133. */
  134. public function __toString()
  135. {
  136. if ($this->value) {
  137. return sprintf('<%s "%s" at %s>', $this->type, $this->value, $this->position);
  138. }
  139. return sprintf('<%s at %s>', $this->type, $this->position);
  140. }
  141. }