AttributeNode.php 2.1 KB

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