Node::compile()   B
last analyzed

Complexity

Conditions 7
Paths 15

Size

Total Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
nc 15
nop 1
dl 0
loc 41
ccs 0
cts 31
cp 0
crap 56
rs 8.3306
c 0
b 0
f 0
1
<?php
2
3
namespace Rocket\UI\Forms\Support\Twig;
4
5
use Twig_Compiler;
6
use Twig_Node;
7
8
/**
9
 * Form extension for twig
10
 */
11
12
/**
13
 * Form extension for twig
14
 *
15
 * @author Stéphane Goetz
16
 */
17
class Node extends Twig_Node
18
{
19
    /**
20
     * Form parameters
21
     *
22
     * @var array
23
     */
24
    private $_parameters;
25
26
    /**
27
     * Form methods
28
     *
29
     * @var array
30
     */
31
    private $_methods;
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function __construct(array $parameters = [], array $methods = [], $lineno = 0, $tag = null)
37
    {
38
        $this->_parameters = $parameters;
39
        $this->_methods = $methods;
40
41
        parent::__construct([new Twig_Node()], [], $lineno, $tag);
42
    }
43
44
    /**
45
     * Compiles the node to PHP.
46
     *
47
     * @param Twig_Compiler $compiler A Twig_Compiler instance
48
     */
49
    public function compile(Twig_Compiler $compiler)
50
    {
51
        $compiler->addDebugInfo($this);
52
53
        $compiler->write('echo \Rocket\UI\Forms\Forms::field(');
54
55
        $first = true;
56
        foreach ($this->_parameters as $param) {
57
            if ($first) {
58
                $first = false;
59
            } else {
60
                $compiler->raw(',');
61
            }
62
63
            $compiler->subcompile($param);
64
        }
65
66
        $compiler->raw(')');
67
68
        foreach ($this->_methods as $methods) {
69
            foreach ($methods as $method => $parameters) {
70
                $compiler->raw('->' . $method . '(');
71
72
                $first = true;
73
74
                foreach ($parameters as $param) {
75
                    if ($first) {
76
                        $first = false;
77
                    } else {
78
                        $compiler->raw(',');
79
                    }
80
81
                    $compiler->subcompile($param);
82
                }
83
84
                $compiler->raw(')');
85
            }
86
        }
87
88
        $compiler->raw('->render();');
89
    }
90
}
91