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