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
12namespace Twig\TokenParser;
13
14use Twig\Node\BlockNode;
15use Twig\Node\Expression\BlockReferenceExpression;
16use Twig\Node\Expression\ConstantExpression;
17use Twig\Node\PrintNode;
18use Twig\Token;
19
20/**
21 * Filters a section of a template by applying filters.
22 *
23 *   {% filter upper %}
24 *      This text becomes uppercase
25 *   {% endfilter %}
26 *
27 * @final
28 */
29class FilterTokenParser extends AbstractTokenParser
30{
31    public function parse(Token $token)
32    {
33        $name = $this->parser->getVarName();
34        $ref = new BlockReferenceExpression(new ConstantExpression($name, $token->getLine()), null, $token->getLine(), $this->getTag());
35
36        $filter = $this->parser->getExpressionParser()->parseFilterExpressionRaw($ref, $this->getTag());
37        $this->parser->getStream()->expect(Token::BLOCK_END_TYPE);
38
39        $body = $this->parser->subparse([$this, 'decideBlockEnd'], true);
40        $this->parser->getStream()->expect(Token::BLOCK_END_TYPE);
41
42        $block = new BlockNode($name, $body, $token->getLine());
43        $this->parser->setBlock($name, $block);
44
45        return new PrintNode($filter, $token->getLine(), $this->getTag());
46    }
47
48    public function decideBlockEnd(Token $token)
49    {
50        return $token->test('endfilter');
51    }
52
53    public function getTag()
54    {
55        return 'filter';
56    }
57}
58
59class_alias('Twig\TokenParser\FilterTokenParser', 'Twig_TokenParser_Filter');
60