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