AttributeNode.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. /**
  29. * @param NodeInterface $selector
  30. * @param string $namespace
  31. * @param string $attribute
  32. * @param string $operator
  33. * @param string $value
  34. */
  35. public function __construct(NodeInterface $selector, $namespace, $attribute, $operator, $value)
  36. {
  37. $this->selector = $selector;
  38. $this->namespace = $namespace;
  39. $this->attribute = $attribute;
  40. $this->operator = $operator;
  41. $this->value = $value;
  42. }
  43. /**
  44. * @return NodeInterface
  45. */
  46. public function getSelector()
  47. {
  48. return $this->selector;
  49. }
  50. /**
  51. * @return string
  52. */
  53. public function getNamespace()
  54. {
  55. return $this->namespace;
  56. }
  57. /**
  58. * @return string
  59. */
  60. public function getAttribute()
  61. {
  62. return $this->attribute;
  63. }
  64. /**
  65. * @return string
  66. */
  67. public function getOperator()
  68. {
  69. return $this->operator;
  70. }
  71. /**
  72. * @return string
  73. */
  74. public function getValue()
  75. {
  76. return $this->value;
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public function getSpecificity()
  82. {
  83. return $this->selector->getSpecificity()->plus(new Specificity(0, 1, 0));
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. public function __toString()
  89. {
  90. $attribute = $this->namespace ? $this->namespace.'|'.$this->attribute : $this->attribute;
  91. return 'exists' === $this->operator
  92. ? sprintf('%s[%s[%s]]', $this->getNodeName(), $this->selector, $attribute)
  93. : sprintf("%s[%s[%s %s '%s']]", $this->getNodeName(), $this->selector, $attribute, $this->operator, $this->value);
  94. }
  95. }