vendor/symfony/src/Symfony/Component/CssSelector/TokenStream.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * This file is part of the Symfony package.
       
     5  *
       
     6  * (c) Fabien Potencier <fabien@symfony.com>
       
     7  *
       
     8  * For the full copyright and license information, please view the LICENSE
       
     9  * file that was distributed with this source code.
       
    10  */
       
    11 
       
    12 namespace Symfony\Component\CssSelector;
       
    13 
       
    14 /**
       
    15  * TokenStream represents a stream of CSS Selector tokens.
       
    16  *
       
    17  * This component is a port of the Python lxml library,
       
    18  * which is copyright Infrae and distributed under the BSD license.
       
    19  *
       
    20  * @author Fabien Potencier <fabien@symfony.com>
       
    21  */
       
    22 class TokenStream
       
    23 {
       
    24     private $used;
       
    25     private $tokens;
       
    26     private $source;
       
    27     private $peeked;
       
    28     private $peeking;
       
    29 
       
    30     /**
       
    31      * Constructor.
       
    32      *
       
    33      * @param array $tokens The tokens that make the stream.
       
    34      * @param mixed $source The source of the stream.
       
    35      */
       
    36     public function __construct($tokens, $source = null)
       
    37     {
       
    38         $this->used = array();
       
    39         $this->tokens = $tokens;
       
    40         $this->source = $source;
       
    41         $this->peeked = null;
       
    42         $this->peeking = false;
       
    43     }
       
    44 
       
    45     /**
       
    46      * Gets the tokens that have already been visited in this stream.
       
    47      *
       
    48      * @return array
       
    49      */
       
    50     public function getUsed()
       
    51     {
       
    52         return $this->used;
       
    53     }
       
    54 
       
    55     /**
       
    56      * Gets the next token in the stream or null if there is none.
       
    57      * Note that if this stream was set to be peeking its behavior
       
    58      * will be restored to not peeking after this operation.
       
    59      *
       
    60      * @return mixed
       
    61      */
       
    62     public function next()
       
    63     {
       
    64         if ($this->peeking) {
       
    65             $this->peeking = false;
       
    66             $this->used[] = $this->peeked;
       
    67 
       
    68             return $this->peeked;
       
    69         }
       
    70 
       
    71         if (!count($this->tokens)) {
       
    72             return null;
       
    73         }
       
    74 
       
    75         $next = array_shift($this->tokens);
       
    76         $this->used[] = $next;
       
    77 
       
    78         return $next;
       
    79     }
       
    80 
       
    81     /**
       
    82      * Peeks for the next token in this stream. This means that the next token
       
    83      * will be returned but it won't be considered as used (visited) until the
       
    84      * next() method is invoked.
       
    85      * If there are no remaining tokens null will be returned.
       
    86      *
       
    87      * @see next()
       
    88      *
       
    89      * @return mixed
       
    90      */
       
    91     public function peek()
       
    92     {
       
    93         if (!$this->peeking) {
       
    94             if (!count($this->tokens)) {
       
    95                 return null;
       
    96             }
       
    97 
       
    98             $this->peeked = array_shift($this->tokens);
       
    99 
       
   100             $this->peeking = true;
       
   101         }
       
   102 
       
   103         return $this->peeked;
       
   104     }
       
   105 }