Completed
Push — twig-2 ( 00bf9b...3d05b1 )
by Asmir
05:31
created

BlockInnerAttribute::visit()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 30
ccs 19
cts 19
cp 1
rs 8.8571
cc 3
eloc 16
nc 4
nop 2
crap 3
1
<?php
2
namespace Goetas\Twital\Attribute;
3
4
use Goetas\Twital\Attribute as AttributeBase;
5
use Goetas\Twital\Compiler;
6
use Goetas\Twital\Helper\DOMHelper;
7
use Goetas\Twital\Twital;
8
9
/**
10
 * This will translate '<div t:block-inner="name">foo</div>' into '<div>{% block name%}foo{% endblock %}</div>'
11
 * @author Asmir Mustafic <[email protected]>
12
 *
13
 */
14
class BlockInnerAttribute implements AttributeBase
15
{
16 6
    public function visit(\DOMAttr $att, Compiler $context)
17
    {
18 6
        $node = $att->ownerElement;
19 6
        $node->removeAttributeNode($att);
20
21
        // create sandbox and append it to the node
22 6
        $sandbox = $node->ownerDocument->createElementNS(Twital::NS, "sandbox");
23
24
        // move all child to sandbox to sandbox
25 6
        while ($node->firstChild) {
26 6
            $child = $node->removeChild($node->firstChild);
27 6
            $sandbox->appendChild($child);
28 6
        }
29 6
        $node->appendChild($sandbox);
30
31
        //$context->compileAttributes($node);
32 6
        $context->compileChilds($sandbox);
33
34
35 6
        $start = $context->createControlNode("block " . $att->value);
36 6
        $end = $context->createControlNode("endblock");
37
38 6
        $sandbox->insertBefore($start, $sandbox->firstChild);
39 6
        $sandbox->appendChild($end);
40
41 6
        DOMHelper::replaceWithSet($sandbox, iterator_to_array($sandbox->childNodes));
42 6
        if ($node->parentNode === $node->ownerDocument->documentElement) {
43 3
            DOMHelper::replaceWithSet($node, iterator_to_array($node->childNodes));
44 3
        }
45 6
    }
46
}
47