|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace PHPHtmlParser\Selector; |
|
6
|
|
|
|
|
7
|
|
|
use PHPHtmlParser\Contracts\Selector\ParserInterface; |
|
8
|
|
|
use PHPHtmlParser\Contracts\Selector\SeekerInterface; |
|
9
|
|
|
use PHPHtmlParser\Contracts\Selector\SelectorInterface; |
|
10
|
|
|
use PHPHtmlParser\Discovery\SeekerDiscovery; |
|
11
|
|
|
use PHPHtmlParser\Discovery\SelectorParserDiscovery; |
|
12
|
|
|
use PHPHtmlParser\Dom\Node\AbstractNode; |
|
13
|
|
|
use PHPHtmlParser\Dom\Node\Collection; |
|
14
|
|
|
use PHPHtmlParser\DTO\Selector\ParsedSelectorCollectionDTO; |
|
15
|
|
|
use PHPHtmlParser\DTO\Selector\RuleDTO; |
|
16
|
|
|
use PHPHtmlParser\Exceptions\ChildNotFoundException; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class Selector. |
|
20
|
|
|
*/ |
|
21
|
|
|
class Selector implements SelectorInterface |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var ParsedSelectorCollectionDTO |
|
25
|
|
|
*/ |
|
26
|
|
|
private $ParsedSelectorCollectionDTO; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var SeekerInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
private $seeker; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Constructs with the selector string. |
|
35
|
|
|
*/ |
|
36
|
351 |
|
public function __construct(string $selector, ?ParserInterface $parser = null, ?SeekerInterface $seeker = null) |
|
37
|
|
|
{ |
|
38
|
351 |
|
if ($parser == null) { |
|
39
|
288 |
|
$parser = SelectorParserDiscovery::find(); |
|
40
|
|
|
} |
|
41
|
351 |
|
if ($seeker == null) { |
|
42
|
351 |
|
$seeker = SeekerDiscovery::find(); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
351 |
|
$this->ParsedSelectorCollectionDTO = $parser->parseSelectorString($selector); |
|
46
|
351 |
|
$this->seeker = $seeker; |
|
47
|
351 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Returns the selectors that where found in __construct. |
|
51
|
|
|
*/ |
|
52
|
12 |
|
public function getParsedSelectorCollectionDTO(): ParsedSelectorCollectionDTO |
|
53
|
|
|
{ |
|
54
|
12 |
|
return $this->ParsedSelectorCollectionDTO; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Attempts to find the selectors starting from the given |
|
59
|
|
|
* node object. |
|
60
|
|
|
* |
|
61
|
|
|
* @throws ChildNotFoundException |
|
62
|
|
|
*/ |
|
63
|
339 |
|
public function find(AbstractNode $node): Collection |
|
64
|
|
|
{ |
|
65
|
339 |
|
$results = new Collection(); |
|
66
|
339 |
|
foreach ($this->ParsedSelectorCollectionDTO->getParsedSelectorDTO() as $selector) { |
|
67
|
339 |
|
$nodes = [$node]; |
|
68
|
339 |
|
if (\count($selector->getRules()) == 0) { |
|
69
|
|
|
continue; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
339 |
|
$options = []; |
|
73
|
339 |
|
foreach ($selector->getRules() as $rule) { |
|
74
|
339 |
|
if ($rule->isAlterNext()) { |
|
75
|
3 |
|
$options[] = $this->alterNext($rule); |
|
76
|
3 |
|
continue; |
|
77
|
|
|
} |
|
78
|
339 |
|
$nodes = $this->seeker->seek($nodes, $rule, $options); |
|
79
|
|
|
// clear the options |
|
80
|
339 |
|
$options = []; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
// this is the final set of nodes |
|
84
|
339 |
|
foreach ($nodes as $result) { |
|
85
|
270 |
|
$results[] = $result; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
339 |
|
return $results; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Attempts to figure out what the alteration will be for |
|
94
|
|
|
* the next element. |
|
95
|
|
|
*/ |
|
96
|
3 |
|
private function alterNext(RuleDTO $rule): array |
|
97
|
|
|
{ |
|
98
|
3 |
|
$options = []; |
|
99
|
3 |
|
if ($rule->getTag() == '>') { |
|
100
|
3 |
|
$options['checkGrandChildren'] = false; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
3 |
|
return $options; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|