XPathExpr.php 2.8 KB

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