菜谱项目

XPathExpr.php 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. * @param $condition
  50. *
  51. * @return $this
  52. */
  53. public function addCondition($condition)
  54. {
  55. $this->condition = $this->condition ? sprintf('%s and (%s)', $this->condition, $condition) : $condition;
  56. return $this;
  57. }
  58. /**
  59. * @return string
  60. */
  61. public function getCondition()
  62. {
  63. return $this->condition;
  64. }
  65. /**
  66. * @return $this
  67. */
  68. public function addNameTest()
  69. {
  70. if ('*' !== $this->element) {
  71. $this->addCondition('name() = '.Translator::getXpathLiteral($this->element));
  72. $this->element = '*';
  73. }
  74. return $this;
  75. }
  76. /**
  77. * @return $this
  78. */
  79. public function addStarPrefix()
  80. {
  81. $this->path .= '*/';
  82. return $this;
  83. }
  84. /**
  85. * Joins another XPathExpr with a combiner.
  86. *
  87. * @param string $combiner
  88. * @param XPathExpr $expr
  89. *
  90. * @return $this
  91. */
  92. public function join($combiner, XPathExpr $expr)
  93. {
  94. $path = $this->__toString().$combiner;
  95. if ('*/' !== $expr->path) {
  96. $path .= $expr->path;
  97. }
  98. $this->path = $path;
  99. $this->element = $expr->element;
  100. $this->condition = $expr->condition;
  101. return $this;
  102. }
  103. /**
  104. * @return string
  105. */
  106. public function __toString()
  107. {
  108. $path = $this->path.$this->element;
  109. $condition = null === $this->condition || '' === $this->condition ? '' : '['.$this->condition.']';
  110. return $path.$condition;
  111. }
  112. }