| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <fabien@symfony.com> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Symfony\Component\CssSelector\Node; |
| 13 |
|
| 14 |
/** |
| 15 |
* Represents a "<selector>:has(<subselector>)" node. |
| 16 |
* |
| 17 |
* This component is a port of the Python cssselect library, |
| 18 |
* which is copyright Ian Bicking, @see https://github.com/scrapy/cssselect. |
| 19 |
* |
| 20 |
* @author Franck Ranaivo-Harisoa <franckranaivo@gmail.com> |
| 21 |
* |
| 22 |
* @internal |
| 23 |
*/ |
| 24 |
class RelationNode extends AbstractNode |
| 25 |
{ |
| 26 |
/** |
| 27 |
* @param list<array{0: string, 1: NodeInterface}> $arguments |
| 28 |
*/ |
| 29 |
public function __construct( |
| 30 |
private NodeInterface $selector, |
| 31 |
private array $arguments, |
| 32 |
) { |
| 33 |
} |
| 34 |
|
| 35 |
public function getSelector(): NodeInterface |
| 36 |
{ |
| 37 |
return $this->selector; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* @return list<array{0: string, 1: NodeInterface}> |
| 42 |
*/ |
| 43 |
public function getArguments(): array |
| 44 |
{ |
| 45 |
return $this->arguments; |
| 46 |
} |
| 47 |
|
| 48 |
public function getSpecificity(): Specificity |
| 49 |
{ |
| 50 |
$argumentsSpecificity = array_reduce( |
| 51 |
$this->arguments, |
| 52 |
static fn (Specificity $c, array $a) => 1 === $a[1]->getSpecificity()->compareTo($c) ? $a[1]->getSpecificity() : $c, |
| 53 |
new Specificity(0, 0, 0), |
| 54 |
); |
| 55 |
|
| 56 |
return $this->selector->getSpecificity()->plus($argumentsSpecificity); |
| 57 |
} |
| 58 |
|
| 59 |
public function __toString(): string |
| 60 |
{ |
| 61 |
$parts = array_map( |
| 62 |
static fn (array $a): string => (' ' === $a[0] ? '' : $a[0].' ').$a[1], |
| 63 |
$this->arguments, |
| 64 |
); |
| 65 |
|
| 66 |
return \sprintf('%s[%s:has(%s)]', $this->getNodeName(), $this->selector, implode(', ', $parts)); |
| 67 |
} |
| 68 |
} |
| 69 |
|