Completed
Branch scrutinizer (fbaa3d)
by Thomas
02:21
created

HtmlElement   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 52
c 1
b 1
f 0
dl 0
loc 152
rs 10
wmc 20

8 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
B setVal() 0 31 6
A __construct() 0 3 1
A setAttr() 0 3 1
A getVal() 0 28 6
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
    public function __construct(DOMElement $node)
27
    {
28
        $this->node = $node;
29
    }
30
31
    /**
32
     * Get the value of an attribute
33
     *
34
     * @param string $name
35
     *
36
     * @return string|null
37
     */
38
    public function getAttr(string $name)
39
    {
40
        return $this->node->getAttribute($name);
41
    }
42
43
    /**
44
     * Set attribute.
45
     *
46
     * @param string $name
47
     * @param string $value
48
     */
49
    public function setAttr(string $name, string $value)
50
    {
51
        $this->node->setAttribute($name, $value);
52
    }
53
54
    /**
55
     * Remove an attribute.
56
     *
57
     * @param string $attributeName
58
     */
59
    public function removeAttr(string $attributeName)
60
    {
61
        $this->node->removeAttribute($attributeName);
62
    }
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
    public function removeAllAttrs($except = [])
70
    {
71
        $names = [];
72
        foreach (iterator_to_array($this->node->attributes) as $attribute) {
73
            $names[] = $attribute->name;
74
        }
75
76
        foreach (array_diff($names, (array) $except) as $name) {
77
            $this->node->removeAttribute($name);
78
        }
79
    }
80
81
    /**
82
     * Determine whether the node has the given attribute.
83
     *
84
     * @param string $attributeName
85
     *
86
     * @return bool
87
     */
88
    public function hasAttr(string $attributeName)
89
    {
90
        return $this->node->hasAttribute($attributeName);
91
    }
92
93
    /**
94
     * Get the current value of the node.
95
     *
96
     * @return string|null
97
     */
98
    public function getVal()
99
    {
100
        switch ($this->node->tagName) {
101
            case 'input':
102
                return $this->node->getAttribute('value');
103
            case 'textarea':
104
                return $this->node->nodeValue;
105
            case 'select':
106
                $xpaths = [
107
                    Helper::toXpath('option:selected', 'child::'),
108
                    'child::option[1]'
109
                ];
110
                foreach ($xpaths as $xpath) {
111
                    $nodes = Helper::xpathQuery(
112
                        $xpath,
113
                        $this->getDoc(),
114
                        $this->node
115
                    );
116
117
                    if (count($nodes)) {
118
                        return $nodes[0]->getAttribute('value');
119
                    }
120
                }
121
122
                break;
123
        }
124
125
        return null;
126
    }
127
128
    /**
129
     * Set the value of the node.
130
     *
131
     * @param string $value
132
     */
133
    public function setVal(string $value)
134
    {
135
        switch ($this->node->tagName) {
136
            case 'input':
137
                $this->node->setAttribute('value', $value);
138
                break;
139
            case 'textarea':
140
                $this->node->nodeValue = $value;
141
                break;
142
            case 'select':
143
                $nodes = Helper::xpathQuery(
144
                    Helper::toXpath('option:selected', 'child::'),
145
                    $this->getDoc(),
146
                    $this->node
147
                );
148
149
                foreach ($nodes as $node) {
150
                    $node->removeAttribute('selected');
151
                }
152
153
                $nodes = Helper::xpathQuery(
154
                    Helper::toXpath("option[value='{$value}']", 'child::'),
155
                    $this->getDoc(),
156
                    $this->node
157
                );
158
159
                if (count($nodes)) {
160
                    $nodes[0]->setAttribute('selected', 'selected');
161
                }
162
163
                break;
164
        }
165
    }
166
}
167