Completed
Push — master ( 05ef16...3f1b18 )
by Lars
02:53
created

SelectorConverter::toXPath()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 27
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5.0113

Importance

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