]>
git.immae.eu Git - github/wallabag/wallabag.git/blob - vendor/twig/twig/lib/Twig/Token.php
4 * This file is part of Twig.
6 * (c) 2009 Fabien Potencier
7 * (c) 2009 Armin Ronacher
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>
26 const BLOCK_START_TYPE
= 1;
27 const VAR_START_TYPE
= 2;
28 const BLOCK_END_TYPE
= 3;
29 const VAR_END_TYPE
= 4;
31 const NUMBER_TYPE
= 6;
32 const STRING_TYPE
= 7;
33 const OPERATOR_TYPE
= 8;
34 const PUNCTUATION_TYPE
= 9;
35 const INTERPOLATION_START_TYPE
= 10;
36 const INTERPOLATION_END_TYPE
= 11;
41 * @param integer $type The type of the token
42 * @param string $value The token value
43 * @param integer $lineno The line position in the source
45 public function __construct($type, $value, $lineno)
48 $this->value
= $value;
49 $this->lineno
= $lineno;
53 * Returns a string representation of the token.
55 * @return string A string representation of the token
57 public function __toString()
59 return sprintf('%s(%s)', self
::typeToString($this->type
, true, $this->lineno
), $this->value
);
63 * Tests the current token for a type and/or a value.
67 * * type and value (or array of possible values)
68 * * just value (or array of possible values) (NAME_TYPE is used as type)
70 * @param array|integer $type The type to test
71 * @param array|string|null $values The token value
75 public function test($type, $values = null)
77 if (null === $values && !is_int($type)) {
79 $type = self
::NAME_TYPE
;
82 return ($this->type
=== $type) && (
84 (is_array($values) && in_array($this->value
, $values)) ||
85 $this->value
== $values
92 * @return integer The source line
94 public function getLine()
100 * Gets the token type.
102 * @return integer The token type
104 public function getType()
110 * Gets the token value.
112 * @return string The token value
114 public function getValue()
120 * Returns the constant representation (internal) of a given type.
122 * @param integer $type The type as an integer
123 * @param Boolean $short Whether to return a short representation or not
124 * @param integer $line The code line
126 * @return string The string representation
128 public static function typeToString($type, $short = false, $line = -1)
134 case self
::TEXT_TYPE
:
137 case self
::BLOCK_START_TYPE
:
138 $name = 'BLOCK_START_TYPE';
140 case self
::VAR_START_TYPE
:
141 $name = 'VAR_START_TYPE';
143 case self
::BLOCK_END_TYPE
:
144 $name = 'BLOCK_END_TYPE';
146 case self
::VAR_END_TYPE
:
147 $name = 'VAR_END_TYPE';
149 case self
::NAME_TYPE
:
152 case self
::NUMBER_TYPE
:
153 $name = 'NUMBER_TYPE';
155 case self
::STRING_TYPE
:
156 $name = 'STRING_TYPE';
158 case self
::OPERATOR_TYPE
:
159 $name = 'OPERATOR_TYPE';
161 case self
::PUNCTUATION_TYPE
:
162 $name = 'PUNCTUATION_TYPE';
164 case self
::INTERPOLATION_START_TYPE
:
165 $name = 'INTERPOLATION_START_TYPE';
167 case self
::INTERPOLATION_END_TYPE
:
168 $name = 'INTERPOLATION_END_TYPE';
171 throw new LogicException(sprintf('Token of type "%s" does not exist.', $type));
174 return $short ? $name : 'Twig_Token::'.$name;
178 * Returns the english representation of a given type.
180 * @param integer $type The type as an integer
181 * @param integer $line The code line
183 * @return string The string representation
185 public static function typeToEnglish($type, $line = -1)
189 return 'end of template';
190 case self
::TEXT_TYPE
:
192 case self
::BLOCK_START_TYPE
:
193 return 'begin of statement block';
194 case self
::VAR_START_TYPE
:
195 return 'begin of print statement';
196 case self
::BLOCK_END_TYPE
:
197 return 'end of statement block';
198 case self
::VAR_END_TYPE
:
199 return 'end of print statement';
200 case self
::NAME_TYPE
:
202 case self
::NUMBER_TYPE
:
204 case self
::STRING_TYPE
:
206 case self
::OPERATOR_TYPE
:
208 case self
::PUNCTUATION_TYPE
:
209 return 'punctuation';
210 case self
::INTERPOLATION_START_TYPE
:
211 return 'begin of string interpolation';
212 case self
::INTERPOLATION_END_TYPE
:
213 return 'end of string interpolation';
215 throw new LogicException(sprintf('Token of type "%s" does not exist.', $type));