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

BlockInnerAttribute   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
cbo 2
dl 0
loc 33
ccs 19
cts 19
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B visit() 0 30 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