4 * This file is part of Twig.
9 * For the full copyright and license information, please view the LICENSE
10 * file that was distributed with this source code.
16 * @author Fabien Potencier <fabien@symfony.com>
28 const BLOCK_START_TYPE = 1;
29 const VAR_START_TYPE = 2;
30 const BLOCK_END_TYPE = 3;
31 const VAR_END_TYPE = 4;
33 const NUMBER_TYPE = 6;
34 const STRING_TYPE = 7;
35 const OPERATOR_TYPE = 8;
36 const PUNCTUATION_TYPE = 9;
37 const INTERPOLATION_START_TYPE = 10;
38 const INTERPOLATION_END_TYPE = 11;
41 * @param int $type The type of the token
42 * @param string $value The token value
43 * @param int $lineno The line position in the source
45 public function __construct($type, $value, $lineno)
48 $this->value = $value;
49 $this->lineno = $lineno;
52 public function __toString()
54 return sprintf('%s(%s)', self::typeToString($this->type, true), $this->value);
58 * Tests the current token for a type and/or a value.
62 * * type and value (or array of possible values)
63 * * just value (or array of possible values) (NAME_TYPE is used as type)
65 * @param array|string|int $type The type to test
66 * @param array|string|null $values The token value
70 public function test($type, $values = null)
72 if (null === $values && !is_int($type)) {
74 $type = self::NAME_TYPE;
77 return ($this->type === $type) && (
79 (is_array($values) && in_array($this->value, $values)) ||
80 $this->value == $values
87 public function getLine()
95 public function getType()
103 public function getValue()
109 * Returns the constant representation (internal) of a given type.
111 * @param int $type The type as an integer
112 * @param bool $short Whether to return a short representation or not
114 * @return string The string representation
116 public static function typeToString($type, $short = false)
122 case self::TEXT_TYPE:
125 case self::BLOCK_START_TYPE:
126 $name = 'BLOCK_START_TYPE';
128 case self::VAR_START_TYPE:
129 $name = 'VAR_START_TYPE';
131 case self::BLOCK_END_TYPE:
132 $name = 'BLOCK_END_TYPE';
134 case self::VAR_END_TYPE:
135 $name = 'VAR_END_TYPE';
137 case self::NAME_TYPE:
140 case self::NUMBER_TYPE:
141 $name = 'NUMBER_TYPE';
143 case self::STRING_TYPE:
144 $name = 'STRING_TYPE';
146 case self::OPERATOR_TYPE:
147 $name = 'OPERATOR_TYPE';
149 case self::PUNCTUATION_TYPE:
150 $name = 'PUNCTUATION_TYPE';
152 case self::INTERPOLATION_START_TYPE:
153 $name = 'INTERPOLATION_START_TYPE';
155 case self::INTERPOLATION_END_TYPE:
156 $name = 'INTERPOLATION_END_TYPE';
159 throw new LogicException(sprintf('Token of type "%s" does not exist.', $type));
162 return $short ? $name : 'Twig_Token::'.$name;
166 * Returns the English representation of a given type.
168 * @param int $type The type as an integer
170 * @return string The string representation
172 public static function typeToEnglish($type)
176 return 'end of template';
177 case self::TEXT_TYPE:
179 case self::BLOCK_START_TYPE:
180 return 'begin of statement block';
181 case self::VAR_START_TYPE:
182 return 'begin of print statement';
183 case self::BLOCK_END_TYPE:
184 return 'end of statement block';
185 case self::VAR_END_TYPE:
186 return 'end of print statement';
187 case self::NAME_TYPE:
189 case self::NUMBER_TYPE:
191 case self::STRING_TYPE:
193 case self::OPERATOR_TYPE:
195 case self::PUNCTUATION_TYPE:
196 return 'punctuation';
197 case self::INTERPOLATION_START_TYPE:
198 return 'begin of string interpolation';
199 case self::INTERPOLATION_END_TYPE:
200 return 'end of string interpolation';
202 throw new LogicException(sprintf('Token of type "%s" does not exist.', $type));
207 class_alias('Twig_Token', 'Twig\Token', false);