Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\CssSelector\XPath; Chris@0: Chris@0: /** Chris@0: * XPath expression translator interface. Chris@0: * Chris@0: * This component is a port of the Python cssselect library, Chris@0: * which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect. Chris@0: * Chris@0: * @author Jean-François Simon Chris@0: * Chris@0: * @internal Chris@0: */ Chris@0: class XPathExpr Chris@0: { Chris@0: private $path; Chris@0: private $element; Chris@0: private $condition; Chris@0: Chris@0: /** Chris@0: * @param string $path Chris@0: * @param string $element Chris@0: * @param string $condition Chris@0: * @param bool $starPrefix Chris@0: */ Chris@0: public function __construct($path = '', $element = '*', $condition = '', $starPrefix = false) Chris@0: { Chris@0: $this->path = $path; Chris@0: $this->element = $element; Chris@0: $this->condition = $condition; Chris@0: Chris@0: if ($starPrefix) { Chris@0: $this->addStarPrefix(); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * @return string Chris@0: */ Chris@0: public function getElement() Chris@0: { Chris@0: return $this->element; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @return $this Chris@0: */ Chris@0: public function addCondition($condition) Chris@0: { Chris@14: $this->condition = $this->condition ? sprintf('(%s) and (%s)', $this->condition, $condition) : $condition; Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @return string Chris@0: */ Chris@0: public function getCondition() Chris@0: { Chris@0: return $this->condition; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @return $this Chris@0: */ Chris@0: public function addNameTest() Chris@0: { Chris@0: if ('*' !== $this->element) { Chris@0: $this->addCondition('name() = '.Translator::getXpathLiteral($this->element)); Chris@0: $this->element = '*'; Chris@0: } Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @return $this Chris@0: */ Chris@0: public function addStarPrefix() Chris@0: { Chris@0: $this->path .= '*/'; Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Joins another XPathExpr with a combiner. Chris@0: * Chris@0: * @param string $combiner Chris@0: * @param XPathExpr $expr Chris@0: * Chris@0: * @return $this Chris@0: */ Chris@14: public function join($combiner, self $expr) Chris@0: { Chris@0: $path = $this->__toString().$combiner; Chris@0: Chris@0: if ('*/' !== $expr->path) { Chris@0: $path .= $expr->path; Chris@0: } Chris@0: Chris@0: $this->path = $path; Chris@0: $this->element = $expr->element; Chris@0: $this->condition = $expr->condition; Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @return string Chris@0: */ Chris@0: public function __toString() Chris@0: { Chris@0: $path = $this->path.$this->element; Chris@0: $condition = null === $this->condition || '' === $this->condition ? '' : '['.$this->condition.']'; Chris@0: Chris@0: return $path.$condition; Chris@0: } Chris@0: }