| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * @author Gerard van Helden <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @copyright Zicht Online <http://zicht.nl> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | namespace Zicht\Bundle\FrameworkExtraBundle\Twig\ControlStructures; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Twig_Error_Syntax; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Twig_TokenParser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Twig_Token; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Twig_Node; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * Class SwitchTokenParser | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * @package Zicht\Bundle\FrameworkExtraBundle\Twig\ControlStructures | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | class SwitchTokenParser extends Twig_TokenParser | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * @{inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     public function getTag() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         return 'switch'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @{inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     public function parse(Twig_Token $token) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $lineno = $token->getLine(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $switchExpr = $this->parser->getExpressionParser()->parseExpression(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         /** @var $stream Twig_TokenStream */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $stream = $this->parser->getStream(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $stream->expect(Twig_Token::BLOCK_END_TYPE); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         // skip whitespace between switch and first case | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         while ($stream->test(Twig_Token::TEXT_TYPE)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             if (trim($stream->getCurrent()->getValue()) != '') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |                 $content = $stream->getCurrent()->getValue(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                 throw new Twig_Error_Syntax("Can not render content '$content' directly after switch", $stream->getCurrent()->getLine()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             $stream->next(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $stream->expect(Twig_Token::BLOCK_START_TYPE); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $tests = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         while (!$stream->test('endswitch')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             $token = $stream->expect(Twig_Token::NAME_TYPE, array('case', 'default')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             switch ($token->getValue()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                 case 'case': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                     $caseExpr   = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                     $caseExpr[] = $this->parser->getExpressionParser()->parseExpression(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                     while ($stream->test(Twig_Token::OPERATOR_TYPE, ',')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                         $stream->next(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                         $caseExpr[] = $this->parser->getExpressionParser()->parseExpression(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                 case 'default': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                     $caseExpr = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             $fallthrough = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             if ($stream->test('fallthrough')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                 $stream->next(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                 $fallthrough = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             $stream->expect(Twig_Token::BLOCK_END_TYPE); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             $body = $this->parser->subparse(array($this, 'decideSwitchFork')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             $tests[] = new SwitchCaseNode( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                 $body, | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                 $caseExpr, | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                 $fallthrough, | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                 $token->getLine(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                 $token->getValue() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         $stream->expect('endswitch'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         $stream->expect(Twig_Token::BLOCK_END_TYPE); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         return new SwitchNode(new Twig_Node($tests), $switchExpr, $lineno); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * Checks if the token is part of the current control structure. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * @param Twig_Token $token | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * @return mixed | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 99 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |     public function decideSwitchFork($token) | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |         return $token->test(array('case', 'default', 'endswitch')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 104 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 105 |  |  |  |