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 | 12 | public function attr($name, $value = null) |
|
22 | { |
||
23 | 12 | if (is_array($name)) { |
|
24 | 3 | foreach ($name as $key => $val) { |
|
25 | 3 | $this->setAttr($key, $val); |
|
26 | } |
||
27 | |||
28 | 3 | return $this; |
|
29 | } |
||
30 | |||
31 | 10 | if (!is_null($value)) { |
|
32 | 4 | return $this->setAttr($name, $value); |
|
33 | } |
||
34 | |||
35 | 8 | 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 | 10 | public function getAttr(string $name) |
|
46 | { |
||
47 | return $this->mapFirst(function (HtmlElement $node) use ($name) { |
||
48 | 10 | return $node->getAttr($name); |
|
49 | 10 | }); |
|
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 | 9 | public function setAttr(string $name, string $value) |
|
61 | { |
||
62 | return $this->each(function (HtmlElement $node) use ($name, $value) { |
||
63 | 8 | $node->setAttr($name, $value); |
|
64 | 9 | }); |
|
65 | } |
||
66 | |||
67 | /** |
||
68 | * Remove an attribute from every matched nodes. |
||
69 | * |
||
70 | * @param string $attributeName |
||
71 | * |
||
72 | * @return static |
||
73 | */ |
||
74 | 8 | public function removeAttr(string $attributeName) |
|
75 | { |
||
76 | return $this->each(function (HtmlElement $node) use ($attributeName) { |
||
77 | 8 | $node->removeAttr($attributeName); |
|
78 | 8 | }); |
|
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 | 2 | public function removeAllAttrs($except = []) |
|
89 | { |
||
90 | return $this->each(function (HtmlElement $node) use ($except) { |
||
91 | 2 | $node->removeAllAttrs($except); |
|
92 | 2 | }); |
|
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 | 2 | public function hasAttr(string $attributeName) |
|
103 | { |
||
104 | 2 | return $this->mapAnyTrue( |
|
105 | function (HtmlElement $node) use ($attributeName) { |
||
106 | 2 | return $node->hasAttr($attributeName); |
|
107 | 2 | } |
|
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 | 1 | public function prop($name, $value = null) |
|
120 | { |
||
121 | 1 | return $this->attr($name, $value); |
|
122 | } |
||
123 | |||
124 | /** |
||
125 | * Alias of removeAttr |
||
126 | * |
||
127 | * @param string $attributeName |
||
128 | * |
||
129 | * @return static |
||
130 | */ |
||
131 | 1 | public function removeProp(string $attributeName) |
|
132 | { |
||
133 | 1 | 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 | 2 | public function data($name, $value = null) |
|
149 | { |
||
150 | 2 | if (is_array($name)) { |
|
151 | array_walk($name, function ($val, $key) { |
||
152 | 1 | $this->data($key, $val); |
|
153 | 1 | }); |
|
154 | |||
155 | 1 | return $this; |
|
156 | } |
||
157 | |||
158 | 2 | $name = 'data-' . $name; |
|
159 | |||
160 | 2 | if (is_null($value)) { |
|
161 | 1 | $result = $this->getAttr($name); |
|
162 | |||
163 | 1 | $json = json_decode($result); |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
164 | 1 | if (json_last_error() === JSON_ERROR_NONE) { |
|
165 | 1 | return $json; |
|
166 | } |
||
167 | |||
168 | 1 | return $result; |
|
169 | } |
||
170 | |||
171 | 2 | if (is_array($value)) { |
|
172 | 1 | $value = (string) json_encode($value); |
|
173 | } |
||
174 | |||
175 | 2 | 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 | 1 | public function hasData(string $name) |
|
187 | { |
||
188 | 1 | 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 | 2 | public function removeData(string $name) |
|
199 | { |
||
200 | 2 | return $this->removeAttr('data-' . $name); |
|
201 | } |
||
202 | } |
||
203 |