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>
32 * @param string $element
33 * @param string $condition
34 * @param bool $starPrefix
36 public function __construct($path = '', $element = '*', $condition = '', $starPrefix = false)
39 $this->element = $element;
40 $this->condition = $condition;
43 $this->addStarPrefix();
50 public function getElement()
52 return $this->element;
60 public function addCondition($condition)
62 $this->condition = $this->condition ? sprintf('(%s) and (%s)', $this->condition, $condition) : $condition;
70 public function getCondition()
72 return $this->condition;
78 public function addNameTest()
80 if ('*' !== $this->element) {
81 $this->addCondition('name() = '.Translator::getXpathLiteral($this->element));
91 public function addStarPrefix()
99 * Joins another XPathExpr with a combiner.
101 * @param string $combiner
102 * @param XPathExpr $expr
106 public function join($combiner, self $expr)
108 $path = $this->__toString().$combiner;
110 if ('*/' !== $expr->path) {
111 $path .= $expr->path;
115 $this->element = $expr->element;
116 $this->condition = $expr->condition;
124 public function __toString()
126 $path = $this->path.$this->element;
127 $condition = null === $this->condition || '' === $this->condition ? '' : '['.$this->condition.']';
129 return $path.$condition;