AttributeNode.php 2.6 KB

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