1 | <?php |
---|
2 | |
---|
3 | /* |
---|
4 | * This file is part of Twig. |
---|
5 | * |
---|
6 | * (c) 2009 Fabien Potencier |
---|
7 | * |
---|
8 | * For the full copyright and license information, please view the LICENSE |
---|
9 | * file that was distributed with this source code. |
---|
10 | */ |
---|
11 | class Twig_TokenParser_Set extends Twig_TokenParser |
---|
12 | { |
---|
13 | /** |
---|
14 | * Parses a token and returns a node. |
---|
15 | * |
---|
16 | * @param Twig_Token $token A Twig_Token instance |
---|
17 | * |
---|
18 | * @return Twig_NodeInterface A Twig_NodeInterface instance |
---|
19 | */ |
---|
20 | public function parse(Twig_Token $token) |
---|
21 | { |
---|
22 | $lineno = $token->getLine(); |
---|
23 | $stream = $this->parser->getStream(); |
---|
24 | $names = $this->parser->getExpressionParser()->parseAssignmentExpression(); |
---|
25 | |
---|
26 | $capture = false; |
---|
27 | if ($stream->test(Twig_Token::OPERATOR_TYPE, '=')) { |
---|
28 | $stream->next(); |
---|
29 | $values = $this->parser->getExpressionParser()->parseMultitargetExpression(); |
---|
30 | |
---|
31 | $stream->expect(Twig_Token::BLOCK_END_TYPE); |
---|
32 | |
---|
33 | if (count($names) !== count($values)) { |
---|
34 | throw new Twig_Error_Syntax("When using set, you must have the same number of variables and assignements.", $lineno); |
---|
35 | } |
---|
36 | } else { |
---|
37 | $capture = true; |
---|
38 | |
---|
39 | if (count($names) > 1) { |
---|
40 | throw new Twig_Error_Syntax("When using set with a block, you cannot have a multi-target.", $lineno); |
---|
41 | } |
---|
42 | |
---|
43 | $stream->expect(Twig_Token::BLOCK_END_TYPE); |
---|
44 | |
---|
45 | $values = $this->parser->subparse(array($this, 'decideBlockEnd'), true); |
---|
46 | $stream->expect(Twig_Token::BLOCK_END_TYPE); |
---|
47 | } |
---|
48 | |
---|
49 | return new Twig_Node_Set($capture, $names, $values, $lineno, $this->getTag()); |
---|
50 | } |
---|
51 | |
---|
52 | public function decideBlockEnd(Twig_Token $token) |
---|
53 | { |
---|
54 | return $token->test('endset'); |
---|
55 | } |
---|
56 | |
---|
57 | /** |
---|
58 | * Gets the tag name associated with this token parser. |
---|
59 | * |
---|
60 | * @param string The tag name |
---|
61 | */ |
---|
62 | public function getTag() |
---|
63 | { |
---|
64 | return 'set'; |
---|
65 | } |
---|
66 | } |
---|