[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/twig/twig/src/ -> Token.php (source)

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


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1