shop.balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

Token.php (6254B)


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