Completed
Branch scrutinizer (607e7a)
by Thomas
02:04
created

HtmlQueryAttribute::getAttr()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 2
c 1
b 1
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Sulao\HtmlQuery;
4
5
/**
6
 * Class HtmlQueryAttribute
7
 *
8
 * @package Sulao\HtmlQuery
9
 */
10
abstract class HtmlQueryAttribute extends Selection
11
{
12
    /**
13
     * Get the value of an attribute for the first matched node
14
     * or set one or more attributes for every matched node.
15
     *
16
     * @param string|array $name
17
     * @param string|null  $value
18
     *
19
     * @return static|mixed|null
20
     */
21
    public function attr($name, $value = null)
22
    {
23
        if (is_array($name)) {
24
            foreach ($name as $key => $val) {
25
                $this->setAttr($key, $val);
26
            }
27
28
            return $this;
29
        }
30
31
        if (!is_null($value)) {
32
            return $this->setAttr($name, $value);
33
        }
34
35
        return $this->getAttr($name);
36
    }
37
38
    /**
39
     * Get the value of an attribute for the first matched node
40
     *
41
     * @param string $name
42
     *
43
     * @return string|null
44
     */
45
    public function getAttr(string $name)
46
    {
47
        return $this->mapFirst(function (HtmlElement $node) use ($name) {
48
            return $node->getAttr($name);
49
        });
50
    }
51
52
    /**
53
     * Set one or more attributes for every matched node.
54
     *
55
     * @param string $name
56
     * @param string $value
57
     *
58
     * @return static
59
     */
60
    public function setAttr(string $name, string $value)
61
    {
62
        return $this->each(function (HtmlElement $node) use ($name, $value) {
63
            $node->setAttr($name, $value);
64
        });
65
    }
66
67
    /**
68
     * Remove an attribute from every matched nodes.
69
     *
70
     * @param string $attributeName
71
     *
72
     * @return static
73
     */
74
    public function removeAttr(string $attributeName)
75
    {
76
        return $this->each(function (HtmlElement $node) use ($attributeName) {
77
            $node->removeAttr($attributeName);
78
        });
79
    }
80
81
    /**
82
     * Remove all attributes from every matched nodes except the specified ones.
83
     *
84
     * @param string|array $except The attribute name(s) that won't be removed
85
     *
86
     * @return static
87
     */
88
    public function removeAllAttrs($except = [])
89
    {
90
        return $this->each(function (HtmlElement $node) use ($except) {
91
            $node->removeAllAttrs($except);
92
        });
93
    }
94
95
    /**
96
     * Determine whether any of the nodes have the given attribute.
97
     *
98
     * @param string $attributeName
99
     *
100
     * @return bool
101
     */
102
    public function hasAttr(string $attributeName)
103
    {
104
        return $this->mapAnyTrue(
105
            function (HtmlElement $node) use ($attributeName) {
106
                return $node->hasAttr($attributeName);
107
            }
108
        );
109
    }
110
111
    /**
112
     * Alias of attr
113
     *
114
     * @param string|array $name
115
     * @param string|null  $value
116
     *
117
     * @return static|mixed|null
118
     */
119
    public function prop($name, $value = null)
120
    {
121
        return $this->attr($name, $value);
122
    }
123
124
    /**
125
     * Alias of removeAttr
126
     *
127
     * @param string $attributeName
128
     *
129
     * @return static
130
     */
131
    public function removeProp(string $attributeName)
132
    {
133
        return $this->removeAttr($attributeName);
134
    }
135
136
    /**
137
     * Get the value of an attribute with prefix data- for the first matched
138
     * node, if the value is valid json string, returns the value encoded in
139
     * json in appropriate PHP type
140
     *
141
     * or set one or more attributes with prefix data- for every matched node.
142
     *
143
     * @param string|array $name
144
     * @param string|array|null  $value
145
     *
146
     * @return static|mixed|null
147
     */
148
    public function data($name, $value = null)
149
    {
150
        if (is_array($name)) {
151
            array_walk($name, function ($val, $key) {
152
                $this->data($key, $val);
153
            });
154
155
            return $this;
156
        }
157
158
        $name = 'data-' . $name;
159
160
        if (is_null($value)) {
161
            $result = $this->getAttr($name);
162
163
            $json = json_decode($result);
164
            if (json_last_error() === JSON_ERROR_NONE) {
165
                return $json;
166
            }
167
168
            return $result;
169
        }
170
171
        if (is_array($value)) {
172
            $value = (string) json_encode($value);
173
        }
174
175
        return $this->setAttr($name, $value);
176
    }
177
178
    /**
179
     * Determine whether any of the nodes have the given attribute
180
     * prefix with data-.
181
     *
182
     * @param string $name
183
     *
184
     * @return bool
185
     */
186
    public function hasData(string $name)
187
    {
188
        return $this->hasAttr('data-' . $name);
189
    }
190
191
    /**
192
     * Remove an attribute prefix with data- from every matched nodes.
193
     *
194
     * @param string $name
195
     *
196
     * @return static
197
     */
198
    public function removeData(string $name)
199
    {
200
        return $this->removeAttr('data-' . $name);
201
    }
202
}
203