1 | <?php |
||
15 | class Locator { |
||
16 | |||
17 | const DEFAULT_TYPE = 'css'; |
||
18 | |||
19 | /** |
||
20 | * Converted xpath cache |
||
21 | * @var string |
||
22 | */ |
||
23 | protected $_xpath; |
||
24 | |||
25 | /** |
||
26 | * The type of the locator, can be css, field, xpath, label, link or button |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $_type; |
||
30 | |||
31 | /** |
||
32 | * The selector used to generate the xpath |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $_selector; |
||
36 | |||
37 | /** |
||
38 | * Additional filters to apply after the xpath is found |
||
39 | * @var array |
||
40 | */ |
||
41 | protected $_filters; |
||
42 | |||
43 | /** |
||
44 | * @param string $type |
||
45 | * @param string $selector |
||
46 | * @param array $filters |
||
47 | */ |
||
48 | 67 | function __construct($type, $selector, array $filters = array()) |
|
54 | |||
55 | /** |
||
56 | * Check if a Node item matches the current filters |
||
57 | * @param Node $item |
||
58 | * @param integer $index |
||
59 | * @return boolean |
||
60 | */ |
||
61 | 4 | public function is_filtered(Node $item, $index) |
|
102 | |||
103 | /** |
||
104 | * Try matching "at" filter |
||
105 | * |
||
106 | * @param Node $item |
||
107 | * @param integer $index |
||
108 | * @param string $value |
||
109 | * @return boolean |
||
110 | */ |
||
111 | 2 | public function filter_by_at(Node $item, $index, $value) |
|
115 | |||
116 | /** |
||
117 | * Try matching "value" filter |
||
118 | * |
||
119 | * @param Node $item |
||
120 | * @param integer $index |
||
121 | * @param string $value |
||
122 | * @return boolean |
||
123 | */ |
||
124 | 2 | public function filter_by_value(Node $item, $index, $value) |
|
128 | |||
129 | /** |
||
130 | * Try matching "text" filter |
||
131 | * |
||
132 | * @param Node $item |
||
133 | * @param integer $index |
||
134 | * @param string $value |
||
135 | * @return boolean |
||
136 | */ |
||
137 | 2 | public function filter_by_text(Node $item, $index, $value) |
|
143 | |||
144 | /** |
||
145 | * Try matching "visible" filter |
||
146 | * |
||
147 | * @param Node $item |
||
148 | * @param integer $index |
||
149 | * @param boolean $value |
||
150 | * @return boolean |
||
151 | */ |
||
152 | 1 | public function filter_by_visible(Node $item, $index, $value) |
|
156 | |||
157 | /** |
||
158 | * Try matching "attributes" filter |
||
159 | * |
||
160 | * @param Node $item |
||
161 | * @param integer $index |
||
162 | * @param array $value |
||
163 | * @return boolean |
||
164 | */ |
||
165 | 1 | public function filter_by_attributes(Node $item, $index, array $value) |
|
175 | |||
176 | /** |
||
177 | * Return the xpath representaiton of the selector, using the appropriate method |
||
178 | * @return string |
||
179 | */ |
||
180 | 49 | public function xpath() |
|
217 | |||
218 | /** |
||
219 | * Getter. Current type, one of css, field, xpath, label, link or button |
||
220 | * @return string |
||
221 | */ |
||
222 | 58 | public function type() |
|
226 | |||
227 | /** |
||
228 | * Getter. Current selector |
||
229 | * @return string |
||
230 | */ |
||
231 | 49 | public function selector() |
|
235 | |||
236 | /** |
||
237 | * Getter. Current filters |
||
238 | * @return array |
||
239 | */ |
||
240 | 18 | public function filters() |
|
244 | |||
245 | /** |
||
246 | * Convert field selector into xpath |
||
247 | * |
||
248 | * @param string $selector |
||
249 | * @return string |
||
250 | */ |
||
251 | 17 | public function field_to_xpath($selector) |
|
263 | |||
264 | /** |
||
265 | * Convert label selector to xpath |
||
266 | * @param string $selector |
||
267 | * @return string |
||
268 | */ |
||
269 | 1 | public function label_to_xpath($selector) |
|
280 | |||
281 | /** |
||
282 | * Convert link selector to xpath |
||
283 | * @param string $selector |
||
284 | * @return string |
||
285 | */ |
||
286 | 9 | public function link_to_xpath($selector) |
|
295 | |||
296 | /** |
||
297 | * Convert button selector to xpath |
||
298 | * @param string $selector |
||
299 | * @return string |
||
300 | */ |
||
301 | 12 | public function button_to_xpath($selector) |
|
314 | |||
315 | /** |
||
316 | * Return a pretty printed representation of the locator |
||
317 | * @return string |
||
318 | */ |
||
319 | 2 | public function __toString() |
|
332 | |||
333 | /** |
||
334 | * Convert a CSS selector to XPath. |
||
335 | * Uses Symfony CSS Selector 2.8 and above API if possible. |
||
336 | * Otherwise fallback to pre-2.8 static interface. |
||
337 | * |
||
338 | * @param string $css_selector CSS selector |
||
339 | * @return string XPath selector |
||
340 | */ |
||
341 | private function convert_css_selector_to_xpath($css_selector) |
||
350 | } |
||
351 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.