菜谱项目

Token.php 2.8KB

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