Completed
Push — master ( bf375b...cc5661 )
by Rafał
07:07
created

GimmeListTokenParser::parse()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 37
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 37
rs 8.439
c 0
b 0
f 0
cc 5
eloc 24
nc 16
nop 1
1
<?php
2
3
/**
4
 * This file is part of the Superdesk Web Publisher Templates System.
5
 *
6
 * Copyright 2015 Sourcefabric z.ú. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2015 Sourcefabric z.ú
12
 * @license http://www.superdesk.org/license
13
 */
14
15
namespace SWP\Component\TemplatesSystem\Twig\TokenParser;
16
17
use SWP\Component\TemplatesSystem\Twig\Node\GimmeListNode;
18
19
/**
20
 * Parser for gimme/endgimme blocks.
21
 */
22
class GimmeListTokenParser extends \Twig_TokenParser
23
{
24
    /**
25
     * @param \Twig_Token $token
26
     *
27
     * @return bool
28
     */
29
    public function decideGimmeListEnd(\Twig_Token $token)
30
    {
31
        return $token->test('endgimmelist');
32
    }
33
34
    public function decideGimmeListFork(\Twig_Token $token)
35
    {
36
        return $token->test(['else', 'endgimmelist']);
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function getTag()
43
    {
44
        return 'gimmelist';
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function parse(\Twig_Token $token)
51
    {
52
        $lineno = $token->getLine();
53
        $stream = $this->parser->getStream();
54
55
        $variable = $this->parser->getExpressionParser()->parseAssignmentExpression();
56
        $stream->expect(\Twig_Token::NAME_TYPE, 'from');
57
        $collectionType = $this->parser->getExpressionParser()->parseAssignmentExpression();
58
59
        $collectionFilters = null;
60
        if ($stream->test(\Twig_Token::PUNCTUATION_TYPE, '|')) {
61
            $collectionFilters = $this->parser->getExpressionParser()->parsePostfixExpression($collectionType);
62
        }
63
64
        $parameters = null;
65
        if ($stream->nextIf(\Twig_Token::NAME_TYPE, 'with')) {
66
            $parameters = $this->parser->getExpressionParser()->parseExpression();
67
        }
68
69
        $ifExpression = null;
70
        if ($stream->nextIf(\Twig_Token::NAME_TYPE, 'if')) {
71
            $ifExpression = $this->parser->getExpressionParser()->parseExpression();
72
        }
73
74
        $stream->expect(\Twig_Token::BLOCK_END_TYPE);
75
        $body = $this->parser->subparse([$this, 'decideGimmeListFork']);
76
        if ($stream->next()->getValue() == 'else') {
77
            $stream->expect(\Twig_Token::BLOCK_END_TYPE);
78
            $else = $this->parser->subparse([$this, 'decideGimmeListEnd'], true);
79
        } else {
80
            $else = null;
81
        }
82
83
        $stream->expect(\Twig_Token::BLOCK_END_TYPE);
84
85
        return new GimmeListNode($variable, $collectionType, $collectionFilters, $parameters, $ifExpression, $else, $body, $lineno, $this->getTag());
86
    }
87
}
88