HtmlElement   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 177
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 56
c 1
b 0
f 0
dl 0
loc 177
ccs 68
cts 68
cp 1
rs 10
wmc 24

10 Methods

Rating   Name   Duplication   Size   Complexity  
A hasAttr() 0 3 1
A getAttr() 0 3 1
A removeAttr() 0 3 1
A removeAllAttrs() 0 9 3
A getSelectVal() 0 22 4
A setVal() 0 12 4
A __construct() 0 3 1
A setAttr() 0 3 1
A setSelectVal() 0 21 4
A getVal() 0 12 4
1
<?php
2
3
namespace Sulao\HtmlQuery;
4
5
use DOMElement;
6
7
/**
8
 * Class HtmlElement
9
 *
10
 * @package Sulao\HtmlQuery
11
 */
12
class HtmlElement extends HtmlNode
13
{
14
    use HtmlElementCss;
15
16
    /**
17
     * @var DOMElement
18
     */
19
    protected $node;
20
21
    /**
22
     * HtmlElement constructor.
23
     *
24
     * @param DOMElement $node
25
     */
26 29
    public function __construct(DOMElement $node)
27
    {
28 29
        $this->node = $node;
29 29
    }
30
31
    /**
32
     * Get the value of an attribute
33
     *
34
     * @param string $name
35
     *
36
     * @return string|null
37
     */
38 16
    public function getAttr(string $name)
39
    {
40 16
        return $this->node->getAttribute($name);
41
    }
42
43
    /**
44
     * Set attribute.
45
     *
46
     * @param string $name
47
     * @param string $value
48
     */
49 13
    public function setAttr(string $name, string $value)
50
    {
51 13
        $this->node->setAttribute($name, $value);
52 13
    }
53
54
    /**
55
     * Remove an attribute.
56
     *
57
     * @param string $attributeName
58
     */
59 10
    public function removeAttr(string $attributeName)
60
    {
61 10
        $this->node->removeAttribute($attributeName);
62 10
    }
63
64
    /**
65
     * Remove all attributes except the specified ones.
66
     *
67
     * @param string|array $except The attribute name(s) that won't be removed
68
     */
69 2
    public function removeAllAttrs($except = [])
70
    {
71 2
        $names = [];
72 2
        foreach (iterator_to_array($this->node->attributes) as $attribute) {
0 ignored issues
show
Bug introduced by
It seems like $this->node->attributes can also be of type null; however, parameter $iterator of iterator_to_array() does only seem to accept Traversable, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

72
        foreach (iterator_to_array(/** @scrutinizer ignore-type */ $this->node->attributes) as $attribute) {
Loading history...
73 2
            $names[] = $attribute->name;
74
        }
75
76 2
        foreach (array_diff($names, (array) $except) as $name) {
77 2
            $this->node->removeAttribute($name);
78
        }
79 2
    }
80
81
    /**
82
     * Determine whether the node has the given attribute.
83
     *
84
     * @param string $attributeName
85
     *
86
     * @return bool
87
     */
88 6
    public function hasAttr(string $attributeName)
89
    {
90 6
        return $this->node->hasAttribute($attributeName);
91
    }
92
93
    /**
94
     * Get the current value of the node.
95
     *
96
     * @return string|null
97
     */
98 1
    public function getVal()
99
    {
100 1
        switch ($this->node->tagName) {
101 1
            case 'input':
102 1
                return $this->node->getAttribute('value');
103 1
            case 'textarea':
104 1
                return $this->node->nodeValue;
105 1
            case 'select':
106 1
                return $this->getSelectVal();
107
        }
108
109 1
        return null;
110
    }
111
112
    /**
113
     * Set the value of the node.
114
     *
115
     * @param string $value
116
     */
117 1
    public function setVal(string $value)
118
    {
119 1
        switch ($this->node->tagName) {
120 1
            case 'input':
121 1
                $this->node->setAttribute('value', $value);
122 1
                break;
123 1
            case 'textarea':
124 1
                $this->node->nodeValue = $value;
125 1
                break;
126 1
            case 'select':
127 1
                $this->setSelectVal($value);
128 1
                break;
129
        }
130 1
    }
131
132
    /**
133
     * Set select hag value
134
     *
135
     * @param string $value
136
     */
137 1
    protected function setSelectVal(string $value)
138
    {
139 1
        if ($this->node->tagName == 'select') {
140 1
            $nodes = Helper::xpathQuery(
141 1
                Helper::toXpath('option:selected', 'child::'),
142 1
                $this->getDoc(),
143 1
                $this->node
144
            );
145
146 1
            foreach ($nodes as $node) {
147 1
                $node->removeAttribute('selected');
148
            }
149
150 1
            $nodes = Helper::xpathQuery(
151 1
                Helper::toXpath("option[value='{$value}']", 'child::'),
152 1
                $this->getDoc(),
153 1
                $this->node
154
            );
155
156 1
            if (count($nodes)) {
157 1
                $nodes[0]->setAttribute('selected', 'selected');
158
            }
159
        }
160 1
    }
161
162
    /**
163
     * Get select tag value
164
     *
165
     * @return string|null
166
     */
167 1
    protected function getSelectVal()
168
    {
169 1
        if ($this->node->tagName === 'select') {
170
            $xpaths = [
171 1
                Helper::toXpath('option:selected', 'child::'),
172 1
                'child::option[1]'
173
            ];
174
175 1
            foreach ($xpaths as $xpath) {
176 1
                $nodes = Helper::xpathQuery(
177 1
                    $xpath,
178 1
                    $this->getDoc(),
179 1
                    $this->node
180
                );
181
182 1
                if (count($nodes)) {
183 1
                    return $nodes[0]->getAttribute('value');
184
                }
185
            }
186
        }
187
188 1
        return null;
189
    }
190
}
191