Project

General

Profile

1
<?php
2

    
3
/*
4
 * This file is part of Twig.
5
 *
6
 * (c) 2009 Fabien Potencier
7
 * (c) 2009 Armin Ronacher
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12

    
13
/**
14
 * Represents an if node.
15
 *
16
 * @package    twig
17
 * @author     Fabien Potencier <fabien@symfony.com>
18
 */
19
class Twig_Node_If extends Twig_Node
20
{
21
    public function __construct(Twig_NodeInterface $tests, Twig_NodeInterface $else = null, $lineno, $tag = null)
22
    {
23
        parent::__construct(array('tests' => $tests, 'else' => $else), array(), $lineno, $tag);
24
    }
25

    
26
    /**
27
     * Compiles the node to PHP.
28
     *
29
     * @param Twig_Compiler A Twig_Compiler instance
30
     */
31
    public function compile(Twig_Compiler $compiler)
32
    {
33
        $compiler->addDebugInfo($this);
34
        for ($i = 0; $i < count($this->getNode('tests')); $i += 2) {
35
            if ($i > 0) {
36
                $compiler
37
                    ->outdent()
38
                    ->write("} elseif (")
39
                ;
40
            } else {
41
                $compiler
42
                    ->write('if (')
43
                ;
44
            }
45

    
46
            $compiler
47
                ->subcompile($this->getNode('tests')->getNode($i))
48
                ->raw(") {\n")
49
                ->indent()
50
                ->subcompile($this->getNode('tests')->getNode($i + 1))
51
            ;
52
        }
53

    
54
        if ($this->hasNode('else') && null !== $this->getNode('else')) {
55
            $compiler
56
                ->outdent()
57
                ->write("} else {\n")
58
                ->indent()
59
                ->subcompile($this->getNode('else'))
60
            ;
61
        }
62

    
63
        $compiler
64
            ->outdent()
65
            ->write("}\n");
66
    }
67
}
(11-11/23)