Completed
Push — master ( 05cc52...a48403 )
by Lars
01:56 queued 11s
created

AbstractSimpleHtmlDomNode::find()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
ccs 0
cts 0
cp 0
c 0
b 0
f 0
nc 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace voku\helper;
6
7
abstract class AbstractSimpleHtmlDomNode extends \ArrayObject
8
{
9
    /** @noinspection MagicMethodsValidityInspection */
10
11
    /**
12
     * @param string $name
13
     *
14
     * @return array|null
15
     */
16 4
    public function __get($name)
17
    {
18
        // init
19 4
        $name = \strtolower($name);
20
21 4
        if ($this->count() > 0) {
22 3
            $return = [];
23
24 3
            foreach ($this as $node) {
25 3
                if ($node instanceof SimpleHtmlDomInterface) {
26 3
                    $return[] = $node->{$name};
27
                }
28
            }
29
30 3
            return $return;
31
        }
32
33 1
        if ($name === 'plaintext' || $name === 'outertext') {
34 1
            return [];
35
        }
36
37
        return null;
38
    }
39
40
    /**
41
     * @param string   $selector
42
     * @param int|null $idx
43
     *
44
     * @return SimpleHtmlDomNodeInterface|SimpleHtmlDomNodeInterface[]|null
45
     */
46
    public function __invoke($selector, $idx = null)
47
    {
48
        return $this->find($selector, $idx);
49
    }
50
51
    /**
52
     * @return string
53
     */
54 3
    public function __toString()
55
    {
56
        // init
57 3
        $html = '';
58
59 3
        foreach ($this as $node) {
60 3
            $html .= $node->outertext;
61
        }
62
63 3
        return $html;
64
    }
65
66
    abstract public function find(string $selector, $idx = null);
67
}
68