Passed
Branch master (8d332b)
by Andrew
02:53
created

Document::_clone()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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