Skip to content

Commit 71c3758

Browse files
committed
Updated the way we calculate selector logic
1 parent 1e7b41d commit 71c3758

19 files changed

+737
-419
lines changed

CHANGELOG.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8-
### Changed
9-
- Added tag attribute DTO.
10-
118
## [Unreleased]
129

1310
### Added
@@ -17,7 +14,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1714
### Changed
1815
- Fixed issue with \ causing an infite loop.
1916
- CDATA should not be altered when cleanupInput is false.
20-
17+
- Added tag attribute DTO.
18+
- Cleaned up the selector logic.
2119

2220
### Removed
2321
- Removed curl interface and curl implementation.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PHPHtmlParser\Contracts\Selector;
6+
7+
use PHPHtmlParser\DTO\Selector\ParsedSelectorCollectionDTO;
8+
9+
interface ParserInterface
10+
{
11+
public function parseSelectorString(string $selector): ParsedSelectorCollectionDTO;
12+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace PHPHtmlParser\Contracts\Selector;
4+
5+
use PHPHtmlParser\DTO\Selector\RuleDTO;
6+
use PHPHtmlParser\Exceptions\ChildNotFoundException;
7+
8+
interface SeekerInterface
9+
{
10+
/**
11+
* Attempts to find all children that match the rule
12+
* given.
13+
*
14+
* @throws ChildNotFoundException
15+
*/
16+
public function seek(array $nodes, RuleDTO $rule, array $options, bool $depthFirst): array;
17+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PHPHtmlParser\Contracts\Selector;
6+
7+
use PHPHtmlParser\Dom\AbstractNode;
8+
use PHPHtmlParser\Dom\Collection;
9+
use PHPHtmlParser\DTO\Selector\ParsedSelectorCollectionDTO;
10+
use PHPHtmlParser\Exceptions\ChildNotFoundException;
11+
12+
interface SelectorInterface
13+
{
14+
/**
15+
* Constructs with the selector string.
16+
*/
17+
public function __construct(string $selector, ?ParserInterface $parser = null, ?SeekerInterface $seeker = null);
18+
19+
/**
20+
* Returns the selectors that where found.
21+
*/
22+
public function getParsedSelectorCollectionDTO(): ParsedSelectorCollectionDTO;
23+
24+
public function setDepthFirstFind(bool $status): void;
25+
26+
/**
27+
* Attempts to find the selectors starting from the given
28+
* node object.
29+
*
30+
* @throws ChildNotFoundException
31+
*/
32+
public function find(AbstractNode $node): Collection;
33+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PHPHtmlParser\DTO\Selector;
6+
7+
final class ParsedSelectorCollectionDTO
8+
{
9+
/**
10+
* @var ParsedSelectorDTO[]
11+
*/
12+
private $parsedSelectorDTO = [];
13+
14+
public function __construct(array $values)
15+
{
16+
foreach ($values as $value) {
17+
if ($value instanceof ParsedSelectorDTO) {
18+
$this->parsedSelectorDTO[] = $value;
19+
}
20+
}
21+
}
22+
23+
/**
24+
* @return ParsedSelectorDTO[]
25+
*/
26+
public function getParsedSelectorDTO(): array
27+
{
28+
return $this->parsedSelectorDTO;
29+
}
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PHPHtmlParser\DTO\Selector;
6+
7+
final class ParsedSelectorDTO
8+
{
9+
/**
10+
* @var RuleDTO[]
11+
*/
12+
private $rules = [];
13+
14+
public function __construct(array $values)
15+
{
16+
foreach ($values as $value) {
17+
if ($value instanceof RuleDTO) {
18+
$this->rules[] = $value;
19+
}
20+
}
21+
}
22+
23+
/**
24+
* @return RuleDTO[]
25+
*/
26+
public function getRules(): array
27+
{
28+
return $this->rules;
29+
}
30+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PHPHtmlParser\DTO\Selector;
6+
7+
final class RuleDTO
8+
{
9+
/**
10+
* @var string
11+
*/
12+
private $tag;
13+
14+
/**
15+
* @var string
16+
*/
17+
private $operator;
18+
19+
/**
20+
* @var string|array|null
21+
*/
22+
private $key;
23+
24+
/**
25+
* @var string|array|null
26+
*/
27+
private $value;
28+
29+
/**
30+
* @var bool
31+
*/
32+
private $noKey;
33+
34+
/**
35+
* @var bool
36+
*/
37+
private $alterNext;
38+
39+
public function __construct(array $values)
40+
{
41+
$this->tag = $values['tag'];
42+
$this->operator = $values['operator'];
43+
$this->key = $values['key'];
44+
$this->value = $values['value'];
45+
$this->noKey = $values['noKey'];
46+
$this->alterNext = $values['alterNext'];
47+
}
48+
49+
/**
50+
* @return string
51+
*/
52+
public function getTag(): string
53+
{
54+
return $this->tag;
55+
}
56+
57+
/**
58+
* @return string
59+
*/
60+
public function getOperator(): string
61+
{
62+
return $this->operator;
63+
}
64+
65+
/**
66+
* @return string|array|null
67+
*/
68+
public function getKey()
69+
{
70+
return $this->key;
71+
}
72+
73+
/**
74+
* @return string|array|null
75+
*/
76+
public function getValue()
77+
{
78+
return $this->value;
79+
}
80+
81+
/**
82+
* @return bool
83+
*/
84+
public function isNoKey(): bool
85+
{
86+
return $this->noKey;
87+
}
88+
89+
/**
90+
* @return bool
91+
*/
92+
public function isAlterNext(): bool
93+
{
94+
return $this->alterNext;
95+
}
96+
}

src/PHPHtmlParser/DTO/Tag/AttributeDTO.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@
77
use stringEncode\Encode;
88
use stringEncode\Exception;
99

10-
class AttributeDTO
10+
final class AttributeDTO
1111
{
1212
/**
1313
* @var ?string
1414
*/
15-
protected $value;
15+
private $value;
1616

1717
/**
1818
* @var bool
1919
*/
20-
protected $doubleQuote = true;
20+
private $doubleQuote = true;
2121

2222
public function __construct(array $values)
2323
{
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PHPHtmlParser\Discovery;
6+
7+
use PHPHtmlParser\Contracts\Selector\ParserInterface;
8+
use PHPHtmlParser\Selector\Parser;
9+
10+
class ParserDiscovery
11+
{
12+
/**
13+
* @var ParserInterface|null
14+
*/
15+
private static $parser = null;
16+
17+
public static function find(): ParserInterface
18+
{
19+
if (self::$parser == null) {
20+
self::$parser = new Parser();
21+
}
22+
23+
return self::$parser;
24+
}
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PHPHtmlParser\Discovery;
6+
7+
use PHPHtmlParser\Contracts\Selector\SeekerInterface;
8+
use PHPHtmlParser\Selector\Seeker;
9+
10+
class SeekerDiscovery
11+
{
12+
/**
13+
* @var SeekerInterface|null
14+
*/
15+
private static $seeker = null;
16+
17+
public static function find(): SeekerInterface
18+
{
19+
if (self::$seeker == null) {
20+
self::$seeker = new Seeker();
21+
}
22+
23+
return self::$seeker;
24+
}
25+
}

0 commit comments

Comments
 (0)