Completed
Push — master ( 0f01ca...ac4bd7 )
by Andrew
04:46
created

Document::getHtml()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace DOMWrap;
4
5
use DOMWrap\Traits\CommonTrait;
6
use DOMWrap\Traits\TraversalTrait;
7
use DOMWrap\Traits\ManipulationTrait;
8
9
/**
10
 * Document Node
11
 *
12
 * @package DOMWrap
13
 * @license http://opensource.org/licenses/BSD-3-Clause BSD 3 Clause
14
 */
15
class Document extends \DOMDocument
16
{
17
    use CommonTrait;
18
    use TraversalTrait;
19
    use ManipulationTrait;
20
21 132
    public function __construct($version = null, $encoding = null) {
22 132
        parent::__construct($version, $encoding);
23
24 132
        $this->registerNodeClass('DOMText', 'DOMWrap\\Text');
25 132
        $this->registerNodeClass('DOMElement', 'DOMWrap\\Element');
26 132
        $this->registerNodeClass('DOMComment', 'DOMWrap\\Comment');
27 132
        $this->registerNodeClass('DOMDocumentType', 'DOMWrap\\DocumentType');
28 132
        $this->registerNodeClass('DOMProcessingInstruction', 'DOMWrap\\ProcessingInstruction');
29 132
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 132
    public function document() {
35 132
        return $this;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 132
    public function collection() {
42 132
        return $this->newNodeList([$this]);
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function result($nodeList) {
49
        if ($nodeList->count()) {
50
            return $nodeList->first();
51
        }
52
53
        return null;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function parent() {
60
        return null;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function parents() {
67
        return $this->newNodeList();
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function replaceWith($newNode) {
74
        $this->replaceChild($newNode, $this);
75
76
        return $this;
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82 1
    public function getHtml() {
83 1
        return $this->getOuterHtml();
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89 132
    public function setHtml($html) {
90 132
        if (!is_string($html) || trim($html) == '') {
91 1
            return $this;
92
        }
93
94 132
        $internalErrors = libxml_use_internal_errors(true);
95 132
        $disableEntities = libxml_disable_entity_loader(true);
96
97 132
        if (mb_detect_encoding($html, mb_detect_order(), true) !== 'UTF-8') {
98 132
            $charset = 'auto';
99
100 132
            if (preg_match('@<meta.*?charset=["]?([^"\s]+)@im', $html, $matches)) {
101
                if (in_array($matches[1], mb_list_encodings())) {
102
                    $charset = strtoupper($matches[1]);
103
                    $html = preg_replace('@(charset=["]?)([^"\s]+)([^"]*["]?)@im', '$1UTF-8$3', $html);
104
                }
105
            }
106
107 132
            $html = mb_convert_encoding($html, 'UTF-8', $charset);
108 132
        }
109
110 132
        $this->loadHTML('<?xml encoding="utf-8"?>' . $html);
111
112 132
        libxml_use_internal_errors($internalErrors);
113 132
        libxml_disable_entity_loader($disableEntities);
114
115 132
        return $this;
116
    }
117
}
118