aboutsummaryrefslogtreecommitdiffhomepage
path: root/inc/3rdparty/Twig/Extensions/Grammar/Optional.php
blob: da42748549e2744df16359d959649f62244744eb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php

/*
 * This file is part of Twig.
 *
 * (c) 2010 Fabien Potencier
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
class Twig_Extensions_Grammar_Optional extends Twig_Extensions_Grammar
{
    protected $grammar;

    public function __construct()
    {
        $this->grammar = array();
        foreach (func_get_args() as $grammar) {
            $this->addGrammar($grammar);
        }
    }

    public function __toString()
    {
        $repr = array();
        foreach ($this->grammar as $grammar) {
            $repr[] = (string) $grammar;
        }

        return sprintf('[%s]', implode(' ', $repr));
    }

    public function addGrammar(Twig_Extensions_GrammarInterface $grammar)
    {
        $this->grammar[] = $grammar;
    }

    public function parse(Twig_Token $token)
    {
        // test if we have the optional element before consuming it
        if ($this->grammar[0] instanceof Twig_Extensions_Grammar_Constant) {
            if (!$this->parser->getStream()->test($this->grammar[0]->getType(), $this->grammar[0]->getName())) {
                return array();
            }
        } elseif ($this->grammar[0] instanceof Twig_Extensions_Grammar_Name) {
            if (!$this->parser->getStream()->test(Twig_Token::NAME_TYPE)) {
                return array();
            }
        } elseif ($this->parser->getStream()->test(Twig_Token::BLOCK_END_TYPE)) {
            // if this is not a Constant or a Name, it must be the last element of the tag

            return array();
        }

        $elements = array();
        foreach ($this->grammar as $grammar) {
            $grammar->setParser($this->parser);

            $element = $grammar->parse($token);
            if (is_array($element)) {
                $elements = array_merge($elements, $element);
            } else {
                $elements[$grammar->getName()] = $element;
            }
        }

        return $elements;
    }
}