4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\CssSelector\XPath;
15 * XPath expression translator interface.
17 * This component is a port of the Python cssselect library,
18 * which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
20 * @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
43 * @param string $element
44 * @param string $condition
45 * @param bool $starPrefix
47 public function __construct($path = '', $element = '*', $condition = '', $starPrefix = false)
50 $this->element = $element;
51 $this->condition = $condition;
54 $this->addStarPrefix();
61 public function getElement()
63 return $this->element;
71 public function addCondition($condition)
73 $this->condition = $this->condition ? sprintf('%s and (%s)', $this->condition, $condition) : $condition;
81 public function getCondition()
83 return $this->condition;
89 public function addNameTest()
91 if ('*' !== $this->element) {
92 $this->addCondition('name() = '.Translator::getXpathLiteral($this->element));
102 public function addStarPrefix()
110 * Joins another XPathExpr with a combiner.
112 * @param string $combiner
113 * @param XPathExpr $expr
117 public function join($combiner, XPathExpr $expr)
119 $path = $this->__toString().$combiner;
121 if ('*/' !== $expr->path) {
122 $path .= $expr->path;
126 $this->element = $expr->element;
127 $this->condition = $expr->condition;
135 public function __toString()
137 $path = $this->path.$this->element;
138 $condition = null === $this->condition || '' === $this->condition ? '' : '['.$this->condition.']';
140 return $path.$condition;