Completed
Push — master ( 2a517c...a42c86 )
by Lars
04:47
created

SelectorConverter::toXPath()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 31
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 31
ccs 16
cts 16
cp 1
rs 8.439
cc 6
eloc 15
nc 6
nop 1
crap 6
1
<?php
2
3
namespace voku\helper;
4
5
use Symfony\Component\CssSelector\CssSelectorConverter;
6
7
/**
8
 * Class SelectorConverter
9
 *
10
 * @package voku\helper
11
 */
12
class SelectorConverter
13
{
14
  protected static $compiled = array();
15
16
  /**
17
   * @param $selector
18
   *
19
   * @return mixed|string
20
   *
21
   * @throws \RuntimeException
22
   */
23 81
  public static function toXPath($selector)
24
  {
25 81
    if (isset(self::$compiled[$selector])) {
26 61
      return self::$compiled[$selector];
27
    }
28
29
    // Select DOMText
30 38
    if ($selector === 'text') {
31 3
      return '//text()';
32
    }
33
34
    // Select DOMComment
35 35
    if ($selector === 'comment') {
36 3
      return '//comment()';
37
    }
38
39 33
    if (strpos($selector, '//') === 0) {
40 1
      return $selector;
41
    }
42
43 33
    if (!class_exists('Symfony\\Component\\CssSelector\\CssSelectorConverter')) {
44 1
      throw new \RuntimeException('Unable to filter with a CSS selector as the Symfony CssSelector 2.8+ is not installed (you can use filterXPath instead).');
45 1
    }
46
47 33
    $converter = new CssSelectorConverter(true);
48
49 33
    $xPathQuery = $converter->toXPath($selector);
50 33
    self::$compiled[$selector] = $xPathQuery;
51
52 33
    return $xPathQuery;
53
  }
54
}
55