Completed
Push — master ( 1b4688...6cf4d7 )
by Lars
03:17
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.0073

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 31
ccs 16
cts 17
cp 0.9412
rs 8.439
cc 6
eloc 15
nc 6
nop 1
crap 6.0073
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 77
  public static function toXPath($selector)
22
  {
23 77
    if (isset(self::$compiled[$selector])) {
24 57
      return self::$compiled[$selector];
25
    }
26
27
    // Select DOMText
28 37
    if ($selector === 'text') {
29 3
      return '//text()';
30
    }
31
32
    // Select DOMComment
33 34
    if ($selector === 'comment') {
34 2
      return '//comment()';
35
    }
36
37 32
    if (strpos($selector, '//') === 0) {
38 1
      return $selector;
39 1
    }
40
41 32
    if (!class_exists('Symfony\\Component\\CssSelector\\CssSelectorConverter')) {
42
      throw new \RuntimeException('Unable to filter with a CSS selector as the Symfony CssSelector 2.8+ is not installed (you can use filterXPath instead).');
43 1
    }
44
45 32
    $converter = new CssSelectorConverter(true);
46
47 32
    $xPathQuery = $converter->toXPath($selector);
48 32
    self::$compiled[$selector] = $xPathQuery;
49
50 32
    return $xPathQuery;
51
  }
52
}
53