HtmlQuery::setCss()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Sulao\HtmlQuery;
4
5
use DOMDocument;
6
use DOMNode;
7
use DOMNodeList;
8
use Traversable;
9
10
/**
11
 * Class HtmlQuery
12
 *
13
 * @package Sulao\HtmlQuery
14
 */
15
class HtmlQuery extends HtmlQueryNode
16
{
17
    const VERSION = '1.0.1';
18
19
    /**
20
     * @var DOMDocument
21
     */
22
    protected $doc;
23
24
    /**
25
     * @var DOMNode[]
26
     */
27
    protected $nodes;
28
29
    /**
30
     * HtmlQuery constructor.
31
     *
32
     * @param DOMDocument                   $doc
33
     * @param DOMNode|DOMNode[]|DOMNodeList $nodes
34
     *
35
     * @throws Exception
36
     */
37 82
    public function __construct(DOMDocument $doc, $nodes)
38
    {
39 82
        $this->doc = $doc;
40 82
        $this->nodes = $this->validateNodes($nodes);
41
    }
42
43
    /**
44
     * Get the outer HTML contents of the first matched node.
45
     *
46
     * @return string|null
47
     */
48 29
    public function outerHtml()
49
    {
50 29
        return $this->mapFirst(function (HtmlNode $node) {
51 29
            return $node->outerHtml();
52 29
        });
53
    }
54
55
    /**
56
     * Get the inner HTML contents of the first matched node or
57
     * set the inner HTML contents of every matched node.
58
     *
59
     * @param string|null $html
60
     *
61
     * @return string|null|static
62
     */
63 26
    public function html(?string $html = null)
64
    {
65 26
        if (!is_null($html)) {
66 2
            return $this->setHtml($html);
67
        }
68
69 26
        return $this->getHtml();
70
    }
71
72
    /**
73
     * Get the inner HTML contents of the first matched node.
74
     *
75
     * @return string|null
76
     */
77 26
    public function getHtml()
78
    {
79 26
        return $this->mapFirst(function (HtmlNode $node) {
80 26
            return $node->getHtml();
81 26
        });
82
    }
83
84
    /**
85
     * Set the inner HTML contents of every matched node.
86
     *
87
     * @param string $html
88
     *
89
     * @return static
90
     */
91 2
    public function setHtml(string $html)
92
    {
93 2
        $this->empty();
94
95 2
        if ($html !== '') {
96 2
            $this->append($html);
97
        }
98
99 2
        return $this;
100
    }
101
102
    /**
103
     * Get the combined text contents of the first matched node, including
104
     * it's descendants, or set the text contents of every matched node.
105
     *
106
     * @param string|null $text
107
     *
108
     * @return string|null|static
109
     */
110 5
    public function text(?string $text = null)
111
    {
112 5
        if (!is_null($text)) {
113 2
            return $this->setText($text);
114
        }
115
116 4
        return $this->getText();
117
    }
118
119
    /**
120
     * Get the combined text contents of the first matched node,
121
     * including it's descendants.
122
     *
123
     * @return string|null
124
     */
125 4
    public function getText()
126
    {
127 4
        return $this->mapFirst(function (HtmlNode $node) {
128 4
            return $node->getText();
129 4
        });
130
    }
131
132
    /**
133
     * set the text contents of every matched node.
134
     *
135
     * @param string $text
136
     *
137
     * @return static
138
     */
139 2
    public function setText(string $text)
140
    {
141 2
        return $this->each(function (HtmlNode $node) use ($text) {
142 2
            $node->setText($text);
143 2
        });
144
    }
145
146
    /**
147
     * Remove all child nodes of all matched nodes from the DOM.
148
     *
149
     * @return static
150
     */
151 3
    public function empty()
152
    {
153 3
        return $this->each(function (HtmlNode $node) {
154 3
            $node->empty();
155 3
        });
156
    }
157
158
    /**
159
     * Remove the matched nodes from the DOM.
160
     * optionally filtered by a selector.
161
     *
162
     * @param string|null $selector
163
     *
164
     * @return static
165
     */
166 2
    public function remove(?string $selector = null)
167
    {
168 2
        if (!is_null($selector)) {
169 1
            $this->filter($selector)->remove();
170
        } else {
171 2
            $this->each(function (HtmlNode $node) {
172 2
                $node->remove();
173 2
            });
174
        }
175
176 2
        return $this;
177
    }
178
179
    /**
180
     * Get the current value of the first matched node
181
     * or set the value of every matched node.
182
     *
183
     * @param string|null $value
184
     *
185
     * @return string|null|static
186
     */
187 1
    public function val(?string $value = null)
188
    {
189 1
        if (is_null($value)) {
190 1
            return $this->getVal();
191
        }
192
193 1
        return $this->setVal($value);
194
    }
195
196
    /**
197
     * Get the current value of the first matched node
198
     *
199
     * @return string|null
200
     */
201 1
    public function getVal()
202
    {
203 1
        return $this->mapFirst(function (HtmlElement $node) {
204 1
            return $node->getVal();
205 1
        });
206
    }
207
208
    /**
209
     * Set the value of every matched node.
210
     *
211
     * @param string $value
212
     *
213
     * @return static
214
     */
215 1
    public function setVal(string $value)
216
    {
217 1
        return $this->each(function (HtmlElement $node) use ($value) {
218 1
            $node->setVal($value);
219 1
        });
220
    }
221
222
    /**
223
     * Adds the specified class(es) to each node in the matched nodes.
224
     *
225
     * @param string $className
226
     *
227
     * @return static
228
     */
229 4
    public function addClass(string $className)
230
    {
231 4
        return $this->each(function (HtmlElement $node) use ($className) {
232 4
            $node->addClass($className);
233 4
        });
234
    }
235
236
    /**
237
     * Determine whether any of the matched nodes are assigned the given class.
238
     *
239
     * @param string $className
240
     *
241
     * @return bool
242
     */
243 2
    public function hasClass(string $className)
244
    {
245 2
        return $this->mapAnyTrue(
246 2
            function (HtmlElement $node) use ($className) {
247 2
                return $node->hasClass($className);
248 2
            }
249 2
        );
250
    }
251
252
    /**
253
     * Remove a single class, multiple classes, or all classes
254
     * from each matched node.
255
     *
256
     * @param string|null $className
257
     *
258
     * @return static
259
     */
260 2
    public function removeClass(?string $className = null)
261
    {
262 2
        return $this->each(function (HtmlElement $node) use ($className) {
263 2
            $node->removeClass($className);
264 2
        });
265
    }
266
267
    /**
268
     * Add or remove class(es) from each matched node, depending on
269
     * either the class's presence or the value of the state argument.
270
     *
271
     * @param string $className
272
     * @param bool|null   $state
273
     *
274
     * @return static
275
     */
276 1
    public function toggleClass(string $className, ?bool $state = null)
277
    {
278 1
        return $this->each(function (HtmlElement $node) use ($className, $state) {
279 1
            $node->toggleClass($className, $state);
280 1
        });
281
    }
282
283
    /**
284
     * Get the value of a computed style property for the first matched node
285
     * or set one or more CSS properties for every matched node.
286
     *
287
     * @param string|array $name
288
     * @param string|null  $value
289
     *
290
     * @return static|string|null
291
     */
292 1
    public function css($name, $value = null)
293
    {
294 1
        if (is_null($value) && !is_array($name)) {
295 1
            return $this->getCss($name);
296
        }
297
298 1
        if (is_array($name)) {
299 1
            foreach ($name as $key => $val) {
300 1
                $this->setCss($key, $val);
301
            }
302
        } else {
303 1
            $this->setCss($name, $value);
304
        }
305
306 1
        return $this;
307
    }
308
309
    /**
310
     * Get the value of a computed style property for the first matched node
311
     *
312
     * @param string $name
313
     *
314
     * @return string|null
315
     */
316 1
    public function getCss(string $name)
317
    {
318 1
        return $this->mapFirst(function (HtmlElement $node) use ($name) {
319 1
            return $node->getCss($name);
320 1
        });
321
    }
322
323
    /**
324
     * Set or Remove one CSS property for every matched node.
325
     *
326
     * @param string      $name
327
     * @param string|null $value
328
     *
329
     * @return static
330
     */
331 1
    public function setCss(string $name, ?string $value)
332
    {
333 1
        return $this->each(function (HtmlElement $node) use ($name, $value) {
334 1
            $node->setCss($name, $value);
335 1
        });
336
    }
337
338
    /**
339
     * Remove one CSS property for every matched node.
340
     *
341
     * @param string $name
342
     *
343
     * @return static
344
     */
345 1
    public function removeCss(string $name)
346
    {
347 1
        return $this->each(function (HtmlElement $node) use ($name) {
348 1
            $node->removeCss($name);
349 1
        });
350
    }
351
352
    /**
353
     * Validate the nodes
354
     *
355
     * @param DOMNode|DOMNode[]|DOMNodeList|static $nodes
356
     *
357
     * @return DOMNode[]
358
     */
359 82
    protected function validateNodes($nodes)
360
    {
361 82
        $nodes = $this->convertNodes($nodes);
362
363 82
        array_map(function ($node) {
364 80
            if (!($node instanceof DOMNode)) {
365 2
                throw new Exception(
366 2
                    'Expect an instance of DOMNode, '
367 2
                        . gettype($node) . ' given.'
368 2
                );
369
            }
370
371 80
            $document = $node->ownerDocument ?: $node;
372
373 80
            if ($document !== $this->doc) {
374 1
                throw new Exception(
375 1
                    'The DOMNode does not belong to the DOMDocument.'
376 1
                );
377
            }
378 82
        }, $nodes);
379
380 82
        return $nodes;
381
    }
382
383
    /**
384
     * Convert nodes to array
385
     *
386
     * @param DOMNode|DOMNode[]|DOMNodeList|static $nodes
387
     *
388
     * @return array
389
     */
390 82
    protected function convertNodes($nodes): array
391
    {
392 82
        if (empty($nodes)) {
393 21
            $nodes = [];
394 80
        } elseif ($nodes instanceof Traversable) {
395 1
            $nodes = iterator_to_array($nodes);
396 80
        } elseif ($nodes instanceof DOMNode || !is_array($nodes)) {
397 57
            $nodes = [$nodes];
398
        }
399
400 82
        return Helper::strictArrayUnique($nodes);
401
    }
402
}
403