Completed
Push — develop ( b20675...717e04 )
by Peter
02:16
created

Element::appendHtml()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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