XPathExpr.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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\XPath;
  11. /**
  12. * XPath expression translator interface.
  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 XPathExpr
  22. {
  23. private $path;
  24. private $element;
  25. private $condition;
  26. /**
  27. * @param string $path
  28. * @param string $element
  29. * @param string $condition
  30. * @param bool $starPrefix
  31. */
  32. public function __construct($path = '', $element = '*', $condition = '', $starPrefix = false)
  33. {
  34. $this->path = $path;
  35. $this->element = $element;
  36. $this->condition = $condition;
  37. if ($starPrefix) {
  38. $this->addStarPrefix();
  39. }
  40. }
  41. /**
  42. * @return string
  43. */
  44. public function getElement()
  45. {
  46. return $this->element;
  47. }
  48. /**
  49. * @return $this
  50. */
  51. public function addCondition($condition)
  52. {
  53. $this->condition = $this->condition ? sprintf('(%s) and (%s)', $this->condition, $condition) : $condition;
  54. return $this;
  55. }
  56. /**
  57. * @return string
  58. */
  59. public function getCondition()
  60. {
  61. return $this->condition;
  62. }
  63. /**
  64. * @return $this
  65. */
  66. public function addNameTest()
  67. {
  68. if ('*' !== $this->element) {
  69. $this->addCondition('name() = '.Translator::getXpathLiteral($this->element));
  70. $this->element = '*';
  71. }
  72. return $this;
  73. }
  74. /**
  75. * @return $this
  76. */
  77. public function addStarPrefix()
  78. {
  79. $this->path .= '*/';
  80. return $this;
  81. }
  82. /**
  83. * Joins another XPathExpr with a combiner.
  84. *
  85. * @param string $combiner
  86. * @param XPathExpr $expr
  87. *
  88. * @return $this
  89. */
  90. public function join($combiner, self $expr)
  91. {
  92. $path = $this->__toString().$combiner;
  93. if ('*/' !== $expr->path) {
  94. $path .= $expr->path;
  95. }
  96. $this->path = $path;
  97. $this->element = $expr->element;
  98. $this->condition = $expr->condition;
  99. return $this;
  100. }
  101. /**
  102. * @return string
  103. */
  104. public function __toString()
  105. {
  106. $path = $this->path.$this->element;
  107. $condition = null === $this->condition || '' === $this->condition ? '' : '['.$this->condition.']';
  108. return $path.$condition;
  109. }
  110. }