XHTMLAdapter   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 54
c 0
b 0
f 0
wmc 5
cbo 2
ccs 37
cts 37
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A dump() 0 21 4
A replaceShortTags() 0 26 1
1
<?php
2
namespace Goetas\Twital\SourceAdapter;
3
4
use Goetas\Twital\Template;
5
6
/**
7
 *
8
 * @author Asmir Mustafic <[email protected]>
9
 *
10
 */
11
class XHTMLAdapter extends XMLAdapter
12
{
13
    /**
14
     * {@inheritdoc}
15
     */
16 135
    public function dump(Template $template)
17
    {
18 135
        $metadata = $template->getMetadata();
19 135
        $dom = $template->getDocument();
20 135
        $dom->preserveWhiteSpace = true;
21 135
        $dom->formatOutput = false;
22
23 135
        if ($metadata['xmldeclaration']) {
24 1
            $xml = $dom->saveXML();
25 1
        } else {
26 134
            $xml = '';
27 134
            foreach ($dom->childNodes as $node) {
28 133
                $xml .= $dom->saveXML($node, LIBXML_NOEMPTYTAG);
29 133
                if ($node instanceof \DOMDocumentType) {
30 1
                    $xml .= PHP_EOL;
31 1
                }
32 134
            }
33
        }
34
35 135
        return $this->replaceShortTags($xml);
36
    }
37
38 135
    protected function replaceShortTags($str)
39
    {
40
        $selfClosingTags = array(
41 135
            "area",
42 135
            "base",
43 135
            "br",
44 135
            "col",
45 135
            "embed",
46 135
            "hr",
47 135
            "img",
48 135
            "input",
49 135
            "keygen",
50 135
            "link",
51 135
            "menuitem",
52 135
            "meta",
53 135
            "param",
54 135
            "source",
55 135
            "track",
56
            "wbr"
57 135
        );
58 135
        $regex = implode("|", array_map(function ($tag) {
59 135
            return "></\s*($tag)\s*>";
60 135
        }, $selfClosingTags));
61
62 135
        return preg_replace("#$regex#i", "/>", $str);
63
    }
64
}
65