AttributeNode.php 2.3 KB

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