Element::getOuterHtml()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * Webino (http://webino.sk)
4
 *
5
 * @link        https://github.com/webino/WebinoDraw for the canonical source repository
6
 * @copyright   Copyright (c) 2012-2017 Webino, s. r. o. (http://webino.sk)
7
 * @author      Peter Bačinský <[email protected]>
8
 * @license     BSD-3-Clause
9
 */
10
11
namespace WebinoDraw\Dom;
12
13
use DOMElement;
14
use DOMNode;
15
use DOMText;
16
17
/**
18
 * Class Element
19
 *
20
 * Extended DOMElement
21
 *
22
 * @method $this appendChild(DOMNode $newnode)
23
 */
24
class Element extends DOMElement implements
25
    NodeInterface,
26
    Locator\LocatorAwareInterface
27
{
28
    use NodeTrait;
29
    use Locator\LocatorAwareTrait;
30
31
    const NODE_NAME_PROPERTY  = 'nodeName';
32
    const NODE_PATH_PROPERTY  = 'nodePath';
33
34
    /**
35
     * @param string $locator CSS selector or XPath (xpath=)
36
     * @return NodeList
37
     */
38
    public function query($locator)
39
    {
40
        return new NodeList($this->getLocator(), $this->getLocator()->locate($this, $locator));
41
    }
42
43
    /**
44
     * Attributes mass assignment
45
     *
46
     * @param array $attribs
47
     * @param callable $callback Called on each attribute value
48
     * @return self
49
     */
50
    public function setAttributes(array $attribs, $callback = null)
51
    {
52
        foreach ($attribs as $name => $value) {
53
54
            is_callable($callback)
55
                and $value = $callback($value, $name);
56
57
            if (empty($value) && !is_numeric($value)) {
58
                $this->removeAttribute($name);
59
            } else {
60
                $this->setAttribute($name, trim($value));
61
            }
62
        }
63
64
        return $this;
65
    }
66
67
    /**
68
     * Returns the node body html
69
     *
70
     * @return string
71
     */
72
    public function getInnerHtml()
73
    {
74
        if (null === $this->childNodes) {
75
            return '';
76
        }
77
78
        $innerHtml = '';
79
        foreach ($this->childNodes as $child) {
80
            $childHtml = $child->ownerDocument->saveXML($child);
81
            empty($childHtml) or $innerHtml .= $childHtml;
82
        }
83
        return $innerHtml;
84
    }
85
86
    /**
87
     * Returns the node html
88
     *
89
     * @return string
90
     */
91
    public function getOuterHtml()
92
    {
93
        return trim($this->ownerDocument->saveXML($this));
94
    }
95
96
    /**
97
     * Returns the node text value and attributes in the array
98
     *
99
     * @param string $prefix
100
     * @return array
101
     */
102
    public function getProperties($prefix = null)
103
    {
104
        $properties = [
105
            $prefix . $this::NODE_NAME_PROPERTY  => empty($this->nodeName)  ? '' : $this->nodeName,
106
            $prefix . $this::NODE_VALUE_PROPERTY => empty($this->nodeValue) ? '' : $this->nodeValue,
107
            $prefix . $this::NODE_PATH_PROPERTY  => empty($this->nodeName)  ? '' : $this->getNodePath(),
108
        ];
109
110
        if (!empty($this->attributes)) {
111
            foreach ($this->attributes as $attr) {
112
                $properties[$prefix . $attr->name] = $attr->value;
113
            }
114
        }
115
116
        return $properties;
117
    }
118
119
    /**
120
     * @return bool
121
     */
122
    public function isEmpty()
123
    {
124
        $nodeValue = trim($this->nodeValue);
125
126
        if (!empty($nodeValue) || is_numeric($nodeValue)) {
127
            return false;
128
        }
129
130
        // node value is empty,
131
        // check for children other than text
132
        foreach ($this->childNodes as $childNode) {
133
            if (!($childNode instanceof DOMText)) {
134
                return false;
135
            }
136
        }
137
138
        return true;
139
    }
140
141
    /**
142
     * @param string $source
143
     * @return $this
144
     */
145
    public function appendHtml($source)
146
    {
147
        $errors = libxml_use_internal_errors();
148
        libxml_use_internal_errors(true);
149
150
        // from fragment
151
        $frag = $this->ownerDocument->createDocumentFragment();
152
        $frag->appendXml($source);
153
154
        if ($frag->hasChildNodes()) {
155
            $this->appendChild($frag);
156
            libxml_use_internal_errors($errors);
157
            return $this;
158
        }
159
160
        // from document fallback
161
        $dom = new Document;
162
        $dom->loadHTML($source, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
0 ignored issues
show
Documentation introduced by
LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD is of type integer, but the function expects a array|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
163
        $elm = $this->ownerDocument->importNode($dom->getDocumentElement(), true);
164
        $this->appendChild($elm);
165
166
        libxml_use_internal_errors($errors);
167
        return $this;
168
    }
169
}
170