AttributeNode.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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>[<namespace>|<attribute> <operator> <value>]" 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 Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
  18. *
  19. * @internal
  20. */
  21. class AttributeNode extends AbstractNode
  22. {
  23. public function __construct(
  24. private NodeInterface $selector,
  25. private ?string $namespace,
  26. private string $attribute,
  27. private string $operator,
  28. private ?string $value,
  29. ) {
  30. }
  31. public function getSelector(): NodeInterface
  32. {
  33. return $this->selector;
  34. }
  35. public function getNamespace(): ?string
  36. {
  37. return $this->namespace;
  38. }
  39. public function getAttribute(): string
  40. {
  41. return $this->attribute;
  42. }
  43. public function getOperator(): string
  44. {
  45. return $this->operator;
  46. }
  47. public function getValue(): ?string
  48. {
  49. return $this->value;
  50. }
  51. public function getSpecificity(): Specificity
  52. {
  53. return $this->selector->getSpecificity()->plus(new Specificity(0, 1, 0));
  54. }
  55. public function __toString(): string
  56. {
  57. $attribute = $this->namespace ? $this->namespace.'|'.$this->attribute : $this->attribute;
  58. return 'exists' === $this->operator
  59. ? \sprintf('%s[%s[%s]]', $this->getNodeName(), $this->selector, $attribute)
  60. : \sprintf("%s[%s[%s %s '%s']]", $this->getNodeName(), $this->selector, $attribute, $this->operator, $this->value);
  61. }
  62. }