Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
365 lars 1
<?php declare(strict_types=1);
2
 
3
namespace PHPHtmlParser;
4
 
5
use PHPHtmlParser\Dom\AbstractNode;
6
use PHPHtmlParser\Dom\InnerNode;
7
use PHPHtmlParser\Exceptions\ChildNotFoundException;
8
use PHPHtmlParser\Exceptions\ParentNotFoundException;
9
 
10
class Finder
11
{
12
    /**
13
     * @var int
14
     */
15
    private $id;
16
 
17
    /**
18
     * Finder constructor.
19
     * @param $id
20
     */
21
    public function __construct($id)
22
    {
23
        $this->id = $id;
24
    }
25
 
26
    /**
27
     * Find node in tree by id
28
     * @param AbstractNode $node
29
     * @return bool|AbstractNode
30
     * @throws ChildNotFoundException
31
     * @throws ParentNotFoundException
32
     */
33
    public function find(AbstractNode $node)
34
    {
35
        if (!$node->id() && $node instanceof InnerNode) {
36
            return $this->find($node->firstChild());
37
        }
38
 
39
        if ($node->id() == $this->id) {
40
            return $node;
41
        }
42
 
43
        if ($node->hasNextSibling()) {
44
            $nextSibling = $node->nextSibling();
45
            if ($nextSibling->id() == $this->id) {
46
                return $nextSibling;
47
            }
48
            if ($nextSibling->id() > $this->id && $node instanceof InnerNode) {
49
                return $this->find($node->firstChild());
50
            }
51
            if ($nextSibling->id() < $this->id) {
52
                return $this->find($nextSibling);
53
            }
54
        } else if (!$node->isTextNode() && $node instanceof InnerNode) {
55
            return $this->find($node->firstChild());
56
        }
57
 
58
        return false;
59
    }
60
}