AttributeNode.php 2.1 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. /**
  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. }