XHTMLAdapter::dump()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 16
cts 16
cp 1
rs 9.584
c 0
b 0
f 0
cc 4
nc 2
nop 1
crap 4
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