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