FunctionNode.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. use Symfony\Component\CssSelector\Parser\Token;
  12. /**
  13. * Represents a "<selector>:<name>(<arguments>)" node.
  14. *
  15. * This component is a port of the Python cssselect library,
  16. * which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
  17. *
  18. * @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
  19. *
  20. * @internal
  21. */
  22. class FunctionNode extends AbstractNode
  23. {
  24. private string $name;
  25. /**
  26. * @param Token[] $arguments
  27. */
  28. public function __construct(
  29. private NodeInterface $selector,
  30. string $name,
  31. private array $arguments = [],
  32. ) {
  33. $this->name = strtolower($name);
  34. }
  35. public function getSelector(): NodeInterface
  36. {
  37. return $this->selector;
  38. }
  39. public function getName(): string
  40. {
  41. return $this->name;
  42. }
  43. /**
  44. * @return Token[]
  45. */
  46. public function getArguments(): array
  47. {
  48. return $this->arguments;
  49. }
  50. public function getSpecificity(): Specificity
  51. {
  52. return $this->selector->getSpecificity()->plus(new Specificity(0, 1, 0));
  53. }
  54. public function __toString(): string
  55. {
  56. $arguments = implode(', ', array_map(fn (Token $token) => "'".$token->getValue()."'", $this->arguments));
  57. return \sprintf('%s[%s:%s(%s)]', $this->getNodeName(), $this->selector, $this->name, $arguments ? '['.$arguments.']' : '');
  58. }
  59. }