MatchingNode.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\CssSelector\Node;
  11. /**
  12. * Represents a "<selector>:is(<subSelectorList>)" node.
  13. *
  14. * This component is a port of the Python cssselect library,
  15. * which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
  16. *
  17. * @author Hubert Lenoir <lenoir.hubert@gmail.com>
  18. *
  19. * @internal
  20. */
  21. class MatchingNode extends AbstractNode
  22. {
  23. /**
  24. * @param array<NodeInterface> $arguments
  25. */
  26. public function __construct(
  27. public readonly NodeInterface $selector,
  28. public readonly array $arguments = [],
  29. ) {
  30. }
  31. public function getSpecificity(): Specificity
  32. {
  33. $argumentsSpecificity = array_reduce(
  34. $this->arguments,
  35. fn ($c, $n) => 1 === $n->getSpecificity()->compareTo($c) ? $n->getSpecificity() : $c,
  36. new Specificity(0, 0, 0),
  37. );
  38. return $this->selector->getSpecificity()->plus($argumentsSpecificity);
  39. }
  40. public function __toString(): string
  41. {
  42. $selectorArguments = array_map(
  43. fn ($n): string => ltrim((string) $n, '*'),
  44. $this->arguments,
  45. );
  46. return \sprintf('%s[%s:is(%s)]', $this->getNodeName(), $this->selector, implode(', ', $selectorArguments));
  47. }
  48. }