use Symfony\Component\CssSelector\CssSelectorConverter;
8
9
/**
10
* Class SelectorConverter
11
*
12
* @package voku\helper
13
*/
14
class SelectorConverter
15
{
16
protected static $compiled = array();
17
18
/**
19
* @param string $selector
20
*
21
* @return mixed|string
22
*
23
* @throws \RuntimeException
24
*/
25
81
public static function toXPath(string $selector)
26
{
27
81
if (isset(self::$compiled[$selector])) {
28
61
return self::$compiled[$selector];
29
}
30
31
// Select DOMText
32
38
if ($selector === 'text') {
33
3
return '//text()';
34
}
35
36
// Select DOMComment
37
35
if ($selector === 'comment') {
38
2
return '//comment()';
39
}
40
41
33
if (\strpos($selector, '//') === 0) {
42
1
return $selector;
43
}
44
45
33
if (!\class_exists(CssSelectorConverter::class)) {
46
throw new \RuntimeException('Unable to filter with a CSS selector as the Symfony CssSelector 2.8+ is not installed (you can use filterXPath instead).');