Completed
Pull Request — master (#7)
by Asmir
32:22 queued 29:53
created

TranslateNAttribute   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 2

Test Coverage

Coverage 94.12%

Importance

Changes 0
Metric Value
wmc 7
cbo 2
dl 0
loc 34
ccs 16
cts 17
cp 0.9412
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C visit() 0 30 7
1
<?php
2
namespace Goetas\TwitalBundle\Attribute;
3
4
use Goetas\Twital\Attribute;
5
use Goetas\Twital\Compiler;
6
use DOMAttr;
7
use Goetas\Twital\Twital;
8
use Goetas\Twital\Helper\ParserHelper;
9
10
/**
11
 *
12
 * @author Asmir Mustafic <[email protected]>
13
 *
14
 */
15
class TranslateNAttribute implements Attribute
16
{
17
18 3
    public function visit(DOMAttr $att, Compiler $context)
19
    {
20 3
        $node = $att->ownerElement;
21
22 3
        $expessions = ParserHelper::staticSplitExpression($att->value, ",");
23
24 3
        if (! isset($expessions[0]) || ! strlen($expessions[0])) {
25
            throw new \Exception("The count for trans-n is required");
26
        }
27
28 3
        $with = '{\'%count%\':' . $expessions[0] . '}';
29
30 3
        if (isset($expessions[1]) && strlen($expessions[1])) {
31 2
            $with = "$with|merge(" . $expessions[1] . ')';
32
        }
33
34 3
        $from = '';
35 3
        if (isset($expessions[2]) && strlen($expessions[2])) {
36 1
            $from = " from $expessions[2]";
37
        }
38
39 3
        $start = $context->createControlNode("transchoice " . $expessions[0] . " with $with".$from);
40 3
        $end = $context->createControlNode("endtranschoice");
41
42 3
        $node->insertBefore($start, $node->firstChild);
43
44 3
        $node->appendChild($end);
45
46 3
        $node->removeAttributeNode($att);
47 3
    }
48
}
49