aboutsummaryrefslogtreecommitdiffhomepage
path: root/inc/Twig/Extensions/Grammar/Tag.php
blob: 727f26107f3f0b39a43c61df0be59e88dd637f58 (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
<?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_Tag 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 implode(' ', $repr);
    }

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

    public function parse(Twig_Token $token)
    {
        $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;
            }
        }

        $this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);

        return $elements;
    }
}