Cell::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 0
cts 29
cp 0
rs 9.408
c 0
b 0
f 0
cc 3
nc 4
nop 7
crap 12
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * This file is part of TwigView.
6
 *
7
 ** (c) 2014 Cees-Jan Kiewiet
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
namespace WyriHaximus\TwigView\Lib\Twig\Node;
14
15
use Twig\Compiler;
16
use Twig\Node\Expression\AbstractExpression;
17
use Twig\Node\Expression\ArrayExpression;
18
use Twig\Node\Node;
19
use Twig\Node\NodeOutputInterface;
20
21
/**
22
 * Class Cell.
23
 * @package WyriHaximus\TwigView\Lib\Twig\Node
24
 */
25
final class Cell extends Node implements NodeOutputInterface
26
{
27
    /**
28
     * Whether to assign the data or not.
29
     *
30
     * @var bool
31
     */
32
    protected $assign = false;
33
34
    /**
35
     * Constructor.
36
     *
37
     * @param bool                                     $assign   Assign or echo.
38
     * @param mixed                                    $variable Variable to assign to.
39
     * @param \Twig\Node\Expression\AbstractExpression $name     Name.
40
     * @param \Twig\Node\Expression\AbstractExpression $data     Data array.
41
     * @param \Twig\Node\Expression\AbstractExpression $options  Options array.
42
     * @param int                                      $lineno   Line number.
43
     * @param string                                   $tag      Tag name.
44
     */
45
    public function __construct(
46
        $assign,
47
        $variable,
48
        AbstractExpression $name,
49
        ?AbstractExpression $data = null,
50
        ?AbstractExpression $options = null,
51
        int $lineno = 0,
52
        ?string $tag = null
53
    ) {
54
        if ($data === null) {
55
            $data = new ArrayExpression([], $lineno);
56
        }
57
58
        if ($options === null) {
59
            $options = new ArrayExpression([], $lineno);
60
        }
61
62
        parent::__construct(
63
            [
64
                'name' => $name,
65
                'data' => $data,
66
                'options' => $options,
67
            ],
68
            [
69
                'variable' => $variable,
70
            ],
71
            $lineno,
72
            $tag
73
        );
74
75
        $this->assign = $assign;
76
    }
77
78
    /**
79
     * Compile tag.
80
     *
81
     * @param \Twig\Compiler $compiler Compiler.
82
     *
83
     */
84
    public function compile(Compiler $compiler)
85
    {
86
        $compiler->addDebugInfo($this);
87
88
        if ($this->assign) {
89
            $compiler->raw('$context[\'' . $this->getAttribute('variable') . '\'] = ');
90
        } else {
91
            $compiler->raw('echo ');
92
        }
93
        $compiler->raw('$context[\'_view\']->cell(');
94
        $compiler->subcompile($this->getNode('name'));
95
        $data = $this->getNode('data');
96
        if ($data !== null) {
97
            $compiler->raw(',');
98
            $compiler->subcompile($data);
99
        }
100
        $options = $this->getNode('options');
101
        if ($options !== null) {
102
            $compiler->raw(',');
103
            $compiler->subcompile($options);
104
        }
105
        $compiler->raw(");\n");
106
    }
107
}
108