friendship ended with social-app. php is my new best friend
at main 2.4 kB view raw
1<?php 2 3/* 4 * This file is part of the Symfony package. 5 * 6 * (c) Fabien Potencier <fabien@symfony.com> 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12namespace Symfony\Component\CssSelector\XPath; 13 14/** 15 * XPath expression translator interface. 16 * 17 * This component is a port of the Python cssselect library, 18 * which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect. 19 * 20 * @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com> 21 * 22 * @internal 23 */ 24class XPathExpr 25{ 26 public function __construct( 27 private string $path = '', 28 private string $element = '*', 29 private string $condition = '', 30 bool $starPrefix = false, 31 ) { 32 if ($starPrefix) { 33 $this->addStarPrefix(); 34 } 35 } 36 37 public function getElement(): string 38 { 39 return $this->element; 40 } 41 42 /** 43 * @return $this 44 */ 45 public function addCondition(string $condition, string $operator = 'and'): static 46 { 47 $this->condition = $this->condition ? \sprintf('(%s) %s (%s)', $this->condition, $operator, $condition) : $condition; 48 49 return $this; 50 } 51 52 public function getCondition(): string 53 { 54 return $this->condition; 55 } 56 57 /** 58 * @return $this 59 */ 60 public function addNameTest(): static 61 { 62 if ('*' !== $this->element) { 63 $this->addCondition('name() = '.Translator::getXpathLiteral($this->element)); 64 $this->element = '*'; 65 } 66 67 return $this; 68 } 69 70 /** 71 * @return $this 72 */ 73 public function addStarPrefix(): static 74 { 75 $this->path .= '*/'; 76 77 return $this; 78 } 79 80 /** 81 * Joins another XPathExpr with a combiner. 82 * 83 * @return $this 84 */ 85 public function join(string $combiner, self $expr): static 86 { 87 $path = $this->__toString().$combiner; 88 89 if ('*/' !== $expr->path) { 90 $path .= $expr->path; 91 } 92 93 $this->path = $path; 94 $this->element = $expr->element; 95 $this->condition = $expr->condition; 96 97 return $this; 98 } 99 100 public function __toString(): string 101 { 102 $path = $this->path.$this->element; 103 $condition = '' === $this->condition ? '' : '['.$this->condition.']'; 104 105 return $path.$condition; 106 } 107}