Completed
Push — develop ( 7f077f...304037 )
by Peter
10:03
created

Element::getOuterHtml()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
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-2016 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
use WebinoDraw\Exception;
17
18
/**
19
 * Class Element
20
 *
21
 * Extended DOMElement
22
 *
23
 * @method $this appendChild(DOMNode $newnode)
24
 */
25
class Element extends DOMElement implements
26
    NodeInterface,
27
    Locator\LocatorAwareInterface
28
{
29
    use NodeTrait;
30
    use Locator\LocatorAwareTrait;
31
32
    const NODE_NAME_PROPERTY  = 'nodeName';
33
    const NODE_PATH_PROPERTY  = 'nodePath';
34
35
    /**
36
     * @param string $locator CSS selector or XPath (xpath=)
37
     * @return NodeList
38
     */
39
    public function query($locator)
40
    {
41
        return new NodeList($this->getLocator(), $this->getLocator()->locate($this, $locator));
42
    }
43
44
    /**
45
     * Attributes mass assignment
46
     *
47
     * @param array $attribs
48
     * @param callable $callback Called on each attribute value
49
     * @return self
50
     */
51
    public function setAttributes(array $attribs, $callback = null)
52
    {
53
        foreach ($attribs as $name => $value) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
54
55
            is_callable($callback)
56
                and $value = $callback($value, $name);
57
58
            if (empty($value) && !is_numeric($value)) {
59
                $this->removeAttribute($name);
60
            } else {
61
                $this->setAttribute($name, trim($value));
62
            }
63
        }
64
65
        return $this;
66
    }
67
68
    /**
69
     * Returns the node body html
70
     *
71
     * @return string
72
     */
73
    public function getInnerHtml()
74
    {
75
        $innerHtml = '';
76
        foreach ($this->childNodes as $child) {
77
            $childHtml = $child->ownerDocument->saveXML($child);
78
            empty($childHtml) or $innerHtml .= $childHtml;
79
        }
80
        return $innerHtml;
81
    }
82
83
    /**
84
     * Returns the node html
85
     *
86
     * @return string
87
     */
88
    public function getOuterHtml()
89
    {
90
        return trim($this->ownerDocument->saveXML($this));
91
    }
92
93
    /**
94
     * Returns the node text value and attributes in the array
95
     *
96
     * @param string $prefix
97
     * @return array
98
     */
99
    public function getProperties($prefix = null)
100
    {
101
        $properties = [
102
            $prefix . self::NODE_NAME_PROPERTY  => empty($this->nodeName)  ? '' : $this->nodeName,
103
            $prefix . self::NODE_VALUE_PROPERTY => empty($this->nodeValue) ? '' : $this->nodeValue,
104
            $prefix . self::NODE_PATH_PROPERTY  => empty($this->nodeName)  ? '' : $this->getNodePath(),
105
        ];
106
107
        if (!empty($this->attributes)) {
108
            foreach ($this->attributes as $attr) {
109
                $properties[$prefix . $attr->name] = $attr->value;
110
            }
111
        }
112
113
        return $properties;
114
    }
115
116
    /**
117
     * @return bool
118
     */
119
    public function isEmpty()
120
    {
121
        $nodeValue = trim($this->nodeValue);
122
123
        if (!empty($nodeValue) || is_numeric($nodeValue)) {
124
            return false;
125
        }
126
127
        // node value is empty,
128
        // check for childs other than text
129
        foreach ($this->childNodes as $childNode) {
130
            if (!($childNode instanceof DOMText)) {
131
                return false;
132
            }
133
        }
134
135
        return true;
136
    }
137
138
    /**
139
     * @param string $source
140
     * @return $this
141
     */
142
    public function appendHtml($source)
143
    {
144
        $errors = libxml_use_internal_errors();
145
        libxml_use_internal_errors(true);
146
147
        // from fragment
148
        $frag = $this->ownerDocument->createDocumentFragment();
149
        $frag->appendXml($source);
150
151
        if ($frag->hasChildNodes()) {
152
            $this->appendChild($frag);
153
            libxml_use_internal_errors($errors);
154
            return $this;
155
        }
156
157
        // from document fallback
158
        $dom = new Document;
159
        $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...
160
        $elm = $this->ownerDocument->importNode($dom->getDocumentElement(), true);
161
        $this->appendChild($elm);
162
163
        libxml_use_internal_errors($errors);
164
        return $this;
165
    }
166
}
167