]>
Commit | Line | Data |
---|---|---|
a4565e88 NL |
1 | <?php |
2 | ||
3 | /* | |
4 | * This file is part of Twig. | |
5 | * | |
6 | * (c) 2010 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 | class Twig_Extensions_Grammar_Tag extends Twig_Extensions_Grammar | |
12 | { | |
13 | protected $grammar; | |
14 | ||
15 | public function __construct() | |
16 | { | |
17 | $this->grammar = array(); | |
18 | foreach (func_get_args() as $grammar) { | |
19 | $this->addGrammar($grammar); | |
20 | } | |
21 | } | |
22 | ||
23 | public function __toString() | |
24 | { | |
25 | $repr = array(); | |
26 | foreach ($this->grammar as $grammar) { | |
27 | $repr[] = (string) $grammar; | |
28 | } | |
29 | ||
30 | return implode(' ', $repr); | |
31 | } | |
32 | ||
33 | public function addGrammar(Twig_Extensions_GrammarInterface $grammar) | |
34 | { | |
35 | $this->grammar[] = $grammar; | |
36 | } | |
37 | ||
38 | public function parse(Twig_Token $token) | |
39 | { | |
40 | $elements = array(); | |
41 | foreach ($this->grammar as $grammar) { | |
42 | $grammar->setParser($this->parser); | |
43 | ||
44 | $element = $grammar->parse($token); | |
45 | if (is_array($element)) { | |
46 | $elements = array_merge($elements, $element); | |
47 | } else { | |
48 | $elements[$grammar->getName()] = $element; | |
49 | } | |
50 | } | |
51 | ||
52 | $this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE); | |
53 | ||
54 | return $elements; | |
55 | } | |
56 | } |