Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
92.31% |
12 / 13 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| ComplexSelector | |
92.31% |
12 / 13 |
|
66.67% |
2 / 3 |
7.02 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| specificity | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| toString | |
87.50% |
7 / 8 |
|
0.00% |
0 / 1 |
4.03 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Phpdftk\Css\Selector; |
| 6 | |
| 7 | /** |
| 8 | * One selector in a SelectorList — a sequence of compound selectors joined |
| 9 | * by combinators (descendant / `>` / `+` / `~` / `||`). |
| 10 | * |
| 11 | * Holds the parsed `compounds` list per the cross-package contract. The |
| 12 | * right-most compound's `combinatorToNext` is `null`. The raw source text |
| 13 | * is preserved for diagnostics / serialization. |
| 14 | */ |
| 15 | final readonly class ComplexSelector |
| 16 | { |
| 17 | /** @param list<CompoundSelectorWithCombinator> $compounds */ |
| 18 | public function __construct( |
| 19 | public array $compounds, |
| 20 | public string $text = '', |
| 21 | ) {} |
| 22 | |
| 23 | public function specificity(): Specificity |
| 24 | { |
| 25 | $total = new Specificity(); |
| 26 | foreach ($this->compounds as $c) { |
| 27 | $total = $total->add($c->compound->specificity()); |
| 28 | } |
| 29 | return $total; |
| 30 | } |
| 31 | |
| 32 | public function toString(): string |
| 33 | { |
| 34 | $parts = []; |
| 35 | foreach ($this->compounds as $c) { |
| 36 | $parts[] = $c->compound->toString(); |
| 37 | if ($c->combinatorToNext !== null) { |
| 38 | if ($c->combinatorToNext === Combinator::Descendant) { |
| 39 | $parts[] = ' '; |
| 40 | } else { |
| 41 | $parts[] = ' ' . $c->combinatorToNext->value . ' '; |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | return implode('', $parts); |
| 46 | } |
| 47 | } |