Extension
4 years ago
Translator.php
4 years ago
TranslatorInterface.php
4 years ago
XPathExpr.php
4 years ago
XPathExpr.php
132 lines
| 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 | |
| 12 | namespace 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 | */ |
| 24 | class XPathExpr |
| 25 | { |
| 26 | private $path; |
| 27 | private $element; |
| 28 | private $condition; |
| 29 | |
| 30 | /** |
| 31 | * @param string $path |
| 32 | * @param string $element |
| 33 | * @param string $condition |
| 34 | * @param bool $starPrefix |
| 35 | */ |
| 36 | public function __construct($path = '', $element = '*', $condition = '', $starPrefix = false) |
| 37 | { |
| 38 | $this->path = $path; |
| 39 | $this->element = $element; |
| 40 | $this->condition = $condition; |
| 41 | |
| 42 | if ($starPrefix) { |
| 43 | $this->addStarPrefix(); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @return string |
| 49 | */ |
| 50 | public function getElement() |
| 51 | { |
| 52 | return $this->element; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * @param $condition |
| 57 | * |
| 58 | * @return $this |
| 59 | */ |
| 60 | public function addCondition($condition) |
| 61 | { |
| 62 | $this->condition = $this->condition ? sprintf('(%s) and (%s)', $this->condition, $condition) : $condition; |
| 63 | |
| 64 | return $this; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * @return string |
| 69 | */ |
| 70 | public function getCondition() |
| 71 | { |
| 72 | return $this->condition; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * @return $this |
| 77 | */ |
| 78 | public function addNameTest() |
| 79 | { |
| 80 | if ('*' !== $this->element) { |
| 81 | $this->addCondition('name() = '.Translator::getXpathLiteral($this->element)); |
| 82 | $this->element = '*'; |
| 83 | } |
| 84 | |
| 85 | return $this; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * @return $this |
| 90 | */ |
| 91 | public function addStarPrefix() |
| 92 | { |
| 93 | $this->path .= '*/'; |
| 94 | |
| 95 | return $this; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Joins another XPathExpr with a combiner. |
| 100 | * |
| 101 | * @param string $combiner |
| 102 | * @param XPathExpr $expr |
| 103 | * |
| 104 | * @return $this |
| 105 | */ |
| 106 | public function join($combiner, self $expr) |
| 107 | { |
| 108 | $path = $this->__toString().$combiner; |
| 109 | |
| 110 | if ('*/' !== $expr->path) { |
| 111 | $path .= $expr->path; |
| 112 | } |
| 113 | |
| 114 | $this->path = $path; |
| 115 | $this->element = $expr->element; |
| 116 | $this->condition = $expr->condition; |
| 117 | |
| 118 | return $this; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * @return string |
| 123 | */ |
| 124 | public function __toString() |
| 125 | { |
| 126 | $path = $this->path.$this->element; |
| 127 | $condition = null === $this->condition || '' === $this->condition ? '' : '['.$this->condition.']'; |
| 128 | |
| 129 | return $path.$condition; |
| 130 | } |
| 131 | } |
| 132 |