| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of Twig. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier |
| 7 |
* (c) 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 |
namespace ElementorDeps\Twig\TokenParser; |
| 13 |
|
| 14 |
use ElementorDeps\Twig\Error\SyntaxError; |
| 15 |
use ElementorDeps\Twig\Node\IfNode; |
| 16 |
use ElementorDeps\Twig\Node\Node; |
| 17 |
use ElementorDeps\Twig\Token; |
| 18 |
/** |
| 19 |
* Tests a condition. |
| 20 |
* |
| 21 |
* {% if users %} |
| 22 |
* <ul> |
| 23 |
* {% for user in users %} |
| 24 |
* <li>{{ user.username|e }}</li> |
| 25 |
* {% endfor %} |
| 26 |
* </ul> |
| 27 |
* {% endif %} |
| 28 |
* |
| 29 |
* @internal |
| 30 |
*/ |
| 31 |
final class IfTokenParser extends AbstractTokenParser |
| 32 |
{ |
| 33 |
public function parse(Token $token) : Node |
| 34 |
{ |
| 35 |
$lineno = $token->getLine(); |
| 36 |
$expr = $this->parser->getExpressionParser()->parseExpression(); |
| 37 |
$stream = $this->parser->getStream(); |
| 38 |
$stream->expect( |
| 39 |
/* Token::BLOCK_END_TYPE */ |
| 40 |
3 |
| 41 |
); |
| 42 |
$body = $this->parser->subparse([$this, 'decideIfFork']); |
| 43 |
$tests = [$expr, $body]; |
| 44 |
$else = null; |
| 45 |
$end = \false; |
| 46 |
while (!$end) { |
| 47 |
switch ($stream->next()->getValue()) { |
| 48 |
case 'else': |
| 49 |
$stream->expect( |
| 50 |
/* Token::BLOCK_END_TYPE */ |
| 51 |
3 |
| 52 |
); |
| 53 |
$else = $this->parser->subparse([$this, 'decideIfEnd']); |
| 54 |
break; |
| 55 |
case 'elseif': |
| 56 |
$expr = $this->parser->getExpressionParser()->parseExpression(); |
| 57 |
$stream->expect( |
| 58 |
/* Token::BLOCK_END_TYPE */ |
| 59 |
3 |
| 60 |
); |
| 61 |
$body = $this->parser->subparse([$this, 'decideIfFork']); |
| 62 |
$tests[] = $expr; |
| 63 |
$tests[] = $body; |
| 64 |
break; |
| 65 |
case 'endif': |
| 66 |
$end = \true; |
| 67 |
break; |
| 68 |
default: |
| 69 |
throw new SyntaxError(\sprintf('Unexpected end of template. Twig was looking for the following tags "else", "elseif", or "endif" to close the "if" block started at line %d).', $lineno), $stream->getCurrent()->getLine(), $stream->getSourceContext()); |
| 70 |
} |
| 71 |
} |
| 72 |
$stream->expect( |
| 73 |
/* Token::BLOCK_END_TYPE */ |
| 74 |
3 |
| 75 |
); |
| 76 |
return new IfNode(new Node($tests), $else, $lineno, $this->getTag()); |
| 77 |
} |
| 78 |
public function decideIfFork(Token $token) : bool |
| 79 |
{ |
| 80 |
return $token->test(['elseif', 'else', 'endif']); |
| 81 |
} |
| 82 |
public function decideIfEnd(Token $token) : bool |
| 83 |
{ |
| 84 |
return $token->test(['endif']); |
| 85 |
} |
| 86 |
public function getTag() : string |
| 87 |
{ |
| 88 |
return 'if'; |
| 89 |
} |
| 90 |
} |
| 91 |
|