ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
Token.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of Twig.
5  *
6  * (c) Fabien Potencier
7  * (c) Armin Ronacher
8  *
9  * For the full copyright and license information, please view the LICENSE
10  * file that was distributed with this source code.
11  */
12 
21 {
22  protected $value;
23  protected $type;
24  protected $lineno;
25 
26  const EOF_TYPE = -1;
27  const TEXT_TYPE = 0;
28  const BLOCK_START_TYPE = 1;
29  const VAR_START_TYPE = 2;
30  const BLOCK_END_TYPE = 3;
31  const VAR_END_TYPE = 4;
32  const NAME_TYPE = 5;
33  const NUMBER_TYPE = 6;
34  const STRING_TYPE = 7;
35  const OPERATOR_TYPE = 8;
36  const PUNCTUATION_TYPE = 9;
39 
45  public function __construct($type, $value, $lineno)
46  {
47  $this->type = $type;
48  $this->value = $value;
49  $this->lineno = $lineno;
50  }
51 
52  public function __toString()
53  {
54  return sprintf('%s(%s)', self::typeToString($this->type, true), $this->value);
55  }
56 
70  public function test($type, $values = null)
71  {
72  if (null === $values && !is_int($type)) {
73  $values = $type;
74  $type = self::NAME_TYPE;
75  }
76 
77  return ($this->type === $type) && (
78  null === $values ||
79  (is_array($values) && in_array($this->value, $values)) ||
80  $this->value == $values
81  );
82  }
83 
87  public function getLine()
88  {
89  return $this->lineno;
90  }
91 
95  public function getType()
96  {
97  return $this->type;
98  }
99 
103  public function getValue()
104  {
105  return $this->value;
106  }
107 
116  public static function typeToString($type, $short = false)
117  {
118  switch ($type) {
119  case self::EOF_TYPE:
120  $name = 'EOF_TYPE';
121  break;
122  case self::TEXT_TYPE:
123  $name = 'TEXT_TYPE';
124  break;
125  case self::BLOCK_START_TYPE:
126  $name = 'BLOCK_START_TYPE';
127  break;
128  case self::VAR_START_TYPE:
129  $name = 'VAR_START_TYPE';
130  break;
131  case self::BLOCK_END_TYPE:
132  $name = 'BLOCK_END_TYPE';
133  break;
134  case self::VAR_END_TYPE:
135  $name = 'VAR_END_TYPE';
136  break;
137  case self::NAME_TYPE:
138  $name = 'NAME_TYPE';
139  break;
140  case self::NUMBER_TYPE:
141  $name = 'NUMBER_TYPE';
142  break;
143  case self::STRING_TYPE:
144  $name = 'STRING_TYPE';
145  break;
146  case self::OPERATOR_TYPE:
147  $name = 'OPERATOR_TYPE';
148  break;
149  case self::PUNCTUATION_TYPE:
150  $name = 'PUNCTUATION_TYPE';
151  break;
152  case self::INTERPOLATION_START_TYPE:
153  $name = 'INTERPOLATION_START_TYPE';
154  break;
155  case self::INTERPOLATION_END_TYPE:
156  $name = 'INTERPOLATION_END_TYPE';
157  break;
158  default:
159  throw new LogicException(sprintf('Token of type "%s" does not exist.', $type));
160  }
161 
162  return $short ? $name : 'Twig_Token::'.$name;
163  }
164 
172  public static function typeToEnglish($type)
173  {
174  switch ($type) {
175  case self::EOF_TYPE:
176  return 'end of template';
177  case self::TEXT_TYPE:
178  return 'text';
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:
188  return 'name';
189  case self::NUMBER_TYPE:
190  return 'number';
191  case self::STRING_TYPE:
192  return 'string';
193  case self::OPERATOR_TYPE:
194  return 'operator';
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';
201  default:
202  throw new LogicException(sprintf('Token of type "%s" does not exist.', $type));
203  }
204  }
205 }
206 
207 class_alias('Twig_Token', 'Twig\Token', false);
const PUNCTUATION_TYPE
Definition: Token.php:36
getType()
Definition: Token.php:95
test($type, $values=null)
Tests the current token for a type and/or a value.
Definition: Token.php:70
__toString()
Definition: Token.php:52
const STRING_TYPE
Definition: Token.php:34
const VAR_END_TYPE
Definition: Token.php:31
if($format !==null) $name
Definition: metadata.php:146
const BLOCK_START_TYPE
Definition: Token.php:28
const TEXT_TYPE
Definition: Token.php:27
static typeToString($type, $short=false)
Returns the constant representation (internal) of a given type.
Definition: Token.php:116
__construct($type, $value, $lineno)
Definition: Token.php:45
getValue()
Definition: Token.php:103
const INTERPOLATION_END_TYPE
Definition: Token.php:38
const EOF_TYPE
Definition: Token.php:26
const NUMBER_TYPE
Definition: Token.php:33
static typeToEnglish($type)
Returns the English representation of a given type.
Definition: Token.php:172
getLine()
Definition: Token.php:87
const OPERATOR_TYPE
Definition: Token.php:35
const VAR_START_TYPE
Definition: Token.php:29
const NAME_TYPE
Definition: Token.php:32
const INTERPOLATION_START_TYPE
Definition: Token.php:37
const BLOCK_END_TYPE
Definition: Token.php:30