PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/vendors/Twig/Token.php

https://bitbucket.org/mrblackus/micro-muffin
PHP | 221 lines | 137 code | 13 blank | 71 comment | 7 complexity | 9ddf651934b2d7c8fad1539adf8e99a9 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) 2009 Fabien Potencier
  6. * (c) 2009 Armin Ronacher
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. /**
  12. * Represents a Token.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. class Twig_Token
  17. {
  18. protected $value;
  19. protected $type;
  20. protected $lineno;
  21. const EOF_TYPE = -1;
  22. const TEXT_TYPE = 0;
  23. const BLOCK_START_TYPE = 1;
  24. const VAR_START_TYPE = 2;
  25. const BLOCK_END_TYPE = 3;
  26. const VAR_END_TYPE = 4;
  27. const NAME_TYPE = 5;
  28. const NUMBER_TYPE = 6;
  29. const STRING_TYPE = 7;
  30. const OPERATOR_TYPE = 8;
  31. const PUNCTUATION_TYPE = 9;
  32. const INTERPOLATION_START_TYPE = 10;
  33. const INTERPOLATION_END_TYPE = 11;
  34. /**
  35. * Constructor.
  36. *
  37. * @param integer $type The type of the token
  38. * @param string $value The token value
  39. * @param integer $lineno The line position in the source
  40. */
  41. public function __construct($type, $value, $lineno)
  42. {
  43. $this->type = $type;
  44. $this->value = $value;
  45. $this->lineno = $lineno;
  46. }
  47. /**
  48. * Returns a string representation of the token.
  49. *
  50. * @return string A string representation of the token
  51. */
  52. public function __toString()
  53. {
  54. return sprintf('%s(%s)', self::typeToString($this->type, true, $this->lineno), $this->value);
  55. }
  56. /**
  57. * Tests the current token for a type and/or a value.
  58. *
  59. * Parameters may be:
  60. * * just type
  61. * * type and value (or array of possible values)
  62. * * just value (or array of possible values) (NAME_TYPE is used as type)
  63. *
  64. * @param array|integer $type The type to test
  65. * @param array|string|null $values The token value
  66. *
  67. * @return Boolean
  68. */
  69. public function test($type, $values = null)
  70. {
  71. if (null === $values && !is_int($type))
  72. {
  73. $values = $type;
  74. $type = self::NAME_TYPE;
  75. }
  76. return ($this->type === $type) && (
  77. null === $values ||
  78. (is_array($values) && in_array($this->value, $values)) ||
  79. $this->value == $values
  80. );
  81. }
  82. /**
  83. * Gets the line.
  84. *
  85. * @return integer The source line
  86. */
  87. public function getLine()
  88. {
  89. return $this->lineno;
  90. }
  91. /**
  92. * Gets the token type.
  93. *
  94. * @return integer The token type
  95. */
  96. public function getType()
  97. {
  98. return $this->type;
  99. }
  100. /**
  101. * Gets the token value.
  102. *
  103. * @return string The token value
  104. */
  105. public function getValue()
  106. {
  107. return $this->value;
  108. }
  109. /**
  110. * Returns the constant representation (internal) of a given type.
  111. *
  112. * @param integer $type The type as an integer
  113. * @param Boolean $short Whether to return a short representation or not
  114. * @param integer $line The code line
  115. *
  116. * @return string The string representation
  117. */
  118. public static function typeToString($type, $short = false, $line = -1)
  119. {
  120. switch ($type)
  121. {
  122. case self::EOF_TYPE:
  123. $name = 'EOF_TYPE';
  124. break;
  125. case self::TEXT_TYPE:
  126. $name = 'TEXT_TYPE';
  127. break;
  128. case self::BLOCK_START_TYPE:
  129. $name = 'BLOCK_START_TYPE';
  130. break;
  131. case self::VAR_START_TYPE:
  132. $name = 'VAR_START_TYPE';
  133. break;
  134. case self::BLOCK_END_TYPE:
  135. $name = 'BLOCK_END_TYPE';
  136. break;
  137. case self::VAR_END_TYPE:
  138. $name = 'VAR_END_TYPE';
  139. break;
  140. case self::NAME_TYPE:
  141. $name = 'NAME_TYPE';
  142. break;
  143. case self::NUMBER_TYPE:
  144. $name = 'NUMBER_TYPE';
  145. break;
  146. case self::STRING_TYPE:
  147. $name = 'STRING_TYPE';
  148. break;
  149. case self::OPERATOR_TYPE:
  150. $name = 'OPERATOR_TYPE';
  151. break;
  152. case self::PUNCTUATION_TYPE:
  153. $name = 'PUNCTUATION_TYPE';
  154. break;
  155. case self::INTERPOLATION_START_TYPE:
  156. $name = 'INTERPOLATION_START_TYPE';
  157. break;
  158. case self::INTERPOLATION_END_TYPE:
  159. $name = 'INTERPOLATION_END_TYPE';
  160. break;
  161. default:
  162. throw new LogicException(sprintf('Token of type "%s" does not exist.', $type));
  163. }
  164. return $short ? $name : 'Twig_Token::' . $name;
  165. }
  166. /**
  167. * Returns the english representation of a given type.
  168. *
  169. * @param integer $type The type as an integer
  170. * @param integer $line The code line
  171. *
  172. * @return string The string representation
  173. */
  174. public static function typeToEnglish($type, $line = -1)
  175. {
  176. switch ($type)
  177. {
  178. case self::EOF_TYPE:
  179. return 'end of template';
  180. case self::TEXT_TYPE:
  181. return 'text';
  182. case self::BLOCK_START_TYPE:
  183. return 'begin of statement block';
  184. case self::VAR_START_TYPE:
  185. return 'begin of print statement';
  186. case self::BLOCK_END_TYPE:
  187. return 'end of statement block';
  188. case self::VAR_END_TYPE:
  189. return 'end of print statement';
  190. case self::NAME_TYPE:
  191. return 'name';
  192. case self::NUMBER_TYPE:
  193. return 'number';
  194. case self::STRING_TYPE:
  195. return 'string';
  196. case self::OPERATOR_TYPE:
  197. return 'operator';
  198. case self::PUNCTUATION_TYPE:
  199. return 'punctuation';
  200. case self::INTERPOLATION_START_TYPE:
  201. return 'begin of string interpolation';
  202. case self::INTERPOLATION_END_TYPE:
  203. return 'end of string interpolation';
  204. default:
  205. throw new LogicException(sprintf('Token of type "%s" does not exist.', $type));
  206. }
  207. }
  208. }