Yaffs site version 1.1
[yaffs-website] / vendor / twig / twig / lib / Twig / Token.php
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
13 /**
14  * Represents a Token.
15  *
16  * @author Fabien Potencier <fabien@symfony.com>
17  *
18  * @final
19  */
20 class Twig_Token
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;
37     const INTERPOLATION_START_TYPE = 10;
38     const INTERPOLATION_END_TYPE = 11;
39
40     /**
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
44      */
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
57     /**
58      * Tests the current token for a type and/or a value.
59      *
60      * Parameters may be:
61      *  * just type
62      *  * type and value (or array of possible values)
63      *  * just value (or array of possible values) (NAME_TYPE is used as type)
64      *
65      * @param array|int         $type   The type to test
66      * @param array|string|null $values The token value
67      *
68      * @return bool
69      */
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
84     /**
85      * @return int
86      */
87     public function getLine()
88     {
89         return $this->lineno;
90     }
91
92     /**
93      * @return int
94      */
95     public function getType()
96     {
97         return $this->type;
98     }
99
100     /**
101      * @return string
102      */
103     public function getValue()
104     {
105         return $this->value;
106     }
107
108     /**
109      * Returns the constant representation (internal) of a given type.
110      *
111      * @param int  $type  The type as an integer
112      * @param bool $short Whether to return a short representation or not
113      *
114      * @return string The string representation
115      */
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
165     /**
166      * Returns the English representation of a given type.
167      *
168      * @param int $type The type as an integer
169      *
170      * @return string The string representation
171      */
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);