| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of Twig. |
| 5 |
* |
| 6 |
* (c) 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 |
namespace ElementorDeps\Twig\TokenParser; |
| 12 |
|
| 13 |
use ElementorDeps\Twig\Error\SyntaxError; |
| 14 |
use ElementorDeps\Twig\Node\Node; |
| 15 |
use ElementorDeps\Twig\Node\SetNode; |
| 16 |
use ElementorDeps\Twig\Token; |
| 17 |
/** |
| 18 |
* Defines a variable. |
| 19 |
* |
| 20 |
* {% set foo = 'foo' %} |
| 21 |
* {% set foo = [1, 2] %} |
| 22 |
* {% set foo = {'foo': 'bar'} %} |
| 23 |
* {% set foo = 'foo' ~ 'bar' %} |
| 24 |
* {% set foo, bar = 'foo', 'bar' %} |
| 25 |
* {% set foo %}Some content{% endset %} |
| 26 |
* |
| 27 |
* @internal |
| 28 |
*/ |
| 29 |
final class SetTokenParser extends AbstractTokenParser |
| 30 |
{ |
| 31 |
public function parse(Token $token) : Node |
| 32 |
{ |
| 33 |
$lineno = $token->getLine(); |
| 34 |
$stream = $this->parser->getStream(); |
| 35 |
$names = $this->parser->getExpressionParser()->parseAssignmentExpression(); |
| 36 |
$capture = \false; |
| 37 |
if ($stream->nextIf( |
| 38 |
/* Token::OPERATOR_TYPE */ |
| 39 |
8, |
| 40 |
'=' |
| 41 |
)) { |
| 42 |
$values = $this->parser->getExpressionParser()->parseMultitargetExpression(); |
| 43 |
$stream->expect( |
| 44 |
/* Token::BLOCK_END_TYPE */ |
| 45 |
3 |
| 46 |
); |
| 47 |
if (\count($names) !== \count($values)) { |
| 48 |
throw new SyntaxError('When using set, you must have the same number of variables and assignments.', $stream->getCurrent()->getLine(), $stream->getSourceContext()); |
| 49 |
} |
| 50 |
} else { |
| 51 |
$capture = \true; |
| 52 |
if (\count($names) > 1) { |
| 53 |
throw new SyntaxError('When using set with a block, you cannot have a multi-target.', $stream->getCurrent()->getLine(), $stream->getSourceContext()); |
| 54 |
} |
| 55 |
$stream->expect( |
| 56 |
/* Token::BLOCK_END_TYPE */ |
| 57 |
3 |
| 58 |
); |
| 59 |
$values = $this->parser->subparse([$this, 'decideBlockEnd'], \true); |
| 60 |
$stream->expect( |
| 61 |
/* Token::BLOCK_END_TYPE */ |
| 62 |
3 |
| 63 |
); |
| 64 |
} |
| 65 |
return new SetNode($capture, $names, $values, $lineno, $this->getTag()); |
| 66 |
} |
| 67 |
public function decideBlockEnd(Token $token) : bool |
| 68 |
{ |
| 69 |
return $token->test('endset'); |
| 70 |
} |
| 71 |
public function getTag() : string |
| 72 |
{ |
| 73 |
return 'set'; |
| 74 |
} |
| 75 |
} |
| 76 |
|