XMLAdapter::getXmlErrors()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 17.1835

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 1
cts 16
cp 0.0625
rs 9.6
c 0
b 0
f 0
cc 4
nc 2
nop 1
crap 17.1835
1
<?php
2
namespace Goetas\Twital\SourceAdapter;
3
4
use Goetas\Twital\SourceAdapter;
5
use Goetas\Twital\Template;
6
7
/**
8
 *
9
 * @author Asmir Mustafic <[email protected]>
10
 *
11
 */
12
class XMLAdapter implements SourceAdapter
13
{
14
    /**
15
     * {@inheritdoc}
16
     */
17 308
    public function load($source)
18
    {
19 308
        $dom = $this->createDom($source);
20
21 308
        return new Template($dom, $this->collectMetadata($dom, $source));
22
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27 171
    public function dump(Template $template)
28
    {
29 171
        $metadata = $template->getMetadata();
30 171
        $dom = $template->getDocument();
31 171
        $dom->preserveWhiteSpace = true;
32 171
        $dom->formatOutput = false;
33
34 171
        if ($metadata['xmldeclaration']) {
35 1
            return $dom->saveXML();
36
        } else {
37 170
            $xml = '';
38 170
            foreach ($dom->childNodes as $node) {
39 169
                $xml .= $dom->saveXML($node);
40 169
                if ($node instanceof \DOMDocumentType) {
41 1
                    $xml .= PHP_EOL;
42 1
                }
43 170
            }
44
45 170
            return $xml;
46
        }
47
    }
48
49
    /**
50
     * Collect some metadata about $dom and $source
51
     * @param \DOMDocument $dom
52
     * @param string $source
53
     * @return mixed
54
     */
55 308
    protected function collectMetadata(\DOMDocument $dom, $source)
56
    {
57 308
        $metadata = array();
58
59 308
        $metadata['xmldeclaration'] = strpos(rtrim($source), '<?xml ') === 0;
60 308
        $metadata['doctype'] = !!$dom->doctype;
61
62 308
        return $metadata;
63
    }
64
65 308
    protected function createDom($source)
66
    {
67 308
        $internalErrors = libxml_use_internal_errors(true);
68 308
        libxml_clear_errors();
69 308
70
        $dom = new \DOMDocument('1.0', 'UTF-8');
71 308
        if ('' !== trim($source) && !$dom->loadXML($source, LIBXML_NONET | (defined('LIBXML_COMPACT') ? LIBXML_COMPACT : 0))) {
72 308
73
            throw new \InvalidArgumentException(implode("\n", $this->getXmlErrors($internalErrors)));
74
        }
75
76
        libxml_use_internal_errors($internalErrors);
77
78 308
        return $dom;
79 308
    }
80
81 308
    protected function getXmlErrors($internalErrors)
82
    {
83
        $errors = array();
84
        foreach (libxml_get_errors() as $error) {
85
            $errors[] = sprintf(
86
                '[%s %s] %s (in %s - line %d, column %d)',
87
                LIBXML_ERR_WARNING == $error->level ? 'WARNING' : 'ERROR',
88
                $error->code,
89
                trim($error->message),
90
                $error->file ? $error->file : 'n/a',
91
                $error->line,
92
                $error->column
93
            );
94
        }
95
96
        libxml_clear_errors();
97
        libxml_use_internal_errors($internalErrors);
98
99
        return $errors;
100
    }
101
}
102