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

BlockOuterAttribute::visit()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 25
ccs 15
cts 15
cp 1
rs 8.8571
cc 1
eloc 14
nc 1
nop 2
crap 1
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-outer="name">foo</div>' into '{% block name%}<div>foo</div>{% endblock %}'
11
 *
12
 * @author Asmir Mustafic <[email protected]>
13
 *
14
 */
15
class BlockOuterAttribute implements AttributeBase
16
{
17 6
    public function visit(\DOMAttr $att, Compiler $context)
18
    {
19 6
        $node = $att->ownerElement;
20 6
        $node->removeAttributeNode($att);
21
22
        // create sandbox
23 6
        $sandbox = $node->ownerDocument->createElementNS(Twital::NS, "sandbox");
24 6
        $node->parentNode->insertBefore($sandbox, $node);
25
26
        // move to sandbox
27 6
        $node->parentNode->removeChild($node);
28 6
        $sandbox->appendChild($node);
29
30 6
        $context->compileAttributes($node);
31 6
        $context->compileChilds($node);
32
33
34 6
        $start = $context->createControlNode("block " . $att->value);
35 6
        $end = $context->createControlNode("endblock");
36
37 6
        $sandbox->insertBefore($start, $sandbox->firstChild);
38 6
        $sandbox->appendChild($end);
39
40 6
        DOMHelper::replaceWithSet($sandbox, iterator_to_array($sandbox->childNodes));
41 6
    }
42
}
43