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

Document   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 63.64%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 15
c 3
b 0
f 0
lcom 0
cbo 3
dl 0
loc 103
ccs 28
cts 44
cp 0.6364
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A document() 0 3 1
A collection() 0 3 1
A result() 0 7 2
A parent() 0 3 1
A parents() 0 3 1
A replaceWith() 0 5 1
A getHtml() 0 3 1
B setHtml() 0 28 6
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