TokenParser::parse()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 17
cts 17
cp 1
rs 8.439
c 0
b 0
f 0
cc 5
eloc 18
nc 5
nop 1
crap 5
1
<?php
2
/*
3
 * This file is part of the Twig Bufferized Template package, an RunOpenCode project.
4
 *
5
 * (c) 2017 RunOpenCode
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace RunOpenCode\Twig\BufferizedTemplate\Tag\Bufferize;
11
12
/**
13
 * Class TokenParser
14
 *
15
 * Delay and bufferize portion of Twig template. Usage examples:
16
 *
17
 * {% bufferize %}
18
 *      ... content ...
19
 * {% endbufferize %}
20
 *
21
 * or
22
 *
23
 * {% bufferize 25 %}
24
 *      ... content ...
25
 * {% endbufferize %}
26
 *
27
 * @package RunOpenCode\Twig\BufferizedTemplate\Tag\Bufferize
28
 *
29
 * @internal
30
 */
31
final class TokenParser extends \Twig_TokenParser
32
{
33
    /**
34
     * {@inheritdoc}
35
     */
36 7
    public function parse(\Twig_Token $token)
37
    {
38 7
        $lineno = $token->getLine();
39 7
        $stream = $this->parser->getStream();
40
41 7
        if ($stream->test(\Twig_Token::BLOCK_END_TYPE)) {
42 2
            $priority = null;
43 6
        } elseif ($stream->test(\Twig_Token::OPERATOR_TYPE)) {
44 2
            $operator = $stream->next()->getValue();
45
46 2
            if (!in_array($operator, array('-', '+'), true)) {
47 1
                throw new \Twig_Error_Syntax(sprintf('Priority can be given as positive and/or negative number, operator "%s" is not allowed.', $operator));
48
            }
49
50 1
            $priority = $stream->expect(\Twig_Token::NUMBER_TYPE)->getValue();
51
52 1
            if ($operator == '-') {
53 1
                $priority = -$priority;
54
            }
55
56
        } else {
57 4
            $priority = $stream->expect(\Twig_Token::NUMBER_TYPE)->getValue();
58
        }
59
60 6
        $stream->expect(\Twig_Token::BLOCK_END_TYPE);
61 6
        $body = $this->parser->subparse(array($this, 'decideBufferizeEnd'), true);
62 6
        $stream->expect(\Twig_Token::BLOCK_END_TYPE);
63
64 6
        return new Node(array('body' => $body), array('execution_priority' => $priority), $lineno, $this->getTag());
65
    }
66
67 6
    public function decideBufferizeEnd(\Twig_Token $token)
68
    {
69 6
        return $token->test('endbufferize');
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 9
    public function getTag()
76
    {
77 9
        return 'bufferize';
78
    }
79
}
80