FunctionNode.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 $selector;
  25. private $name;
  26. private $arguments;
  27. /**
  28. * @param string $name
  29. * @param Token[] $arguments
  30. */
  31. public function __construct(NodeInterface $selector, $name, array $arguments = [])
  32. {
  33. $this->selector = $selector;
  34. $this->name = strtolower($name);
  35. $this->arguments = $arguments;
  36. }
  37. /**
  38. * @return NodeInterface
  39. */
  40. public function getSelector()
  41. {
  42. return $this->selector;
  43. }
  44. /**
  45. * @return string
  46. */
  47. public function getName()
  48. {
  49. return $this->name;
  50. }
  51. /**
  52. * @return Token[]
  53. */
  54. public function getArguments()
  55. {
  56. return $this->arguments;
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function getSpecificity()
  62. {
  63. return $this->selector->getSpecificity()->plus(new Specificity(0, 1, 0));
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. public function __toString()
  69. {
  70. $arguments = implode(', ', array_map(function (Token $token) {
  71. return "'".$token->getValue()."'";
  72. }, $this->arguments));
  73. return sprintf('%s[%s:%s(%s)]', $this->getNodeName(), $this->selector, $this->name, $arguments ? '['.$arguments.']' : '');
  74. }
  75. }