Completed
Push — master ( 6b0cbf...ef4cd6 )
by Lars
02:56
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
/**
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 76
  public static function toXPath($selector)
22
  {
23 76
    if (isset(self::$compiled[$selector])) {
24 57
      return self::$compiled[$selector];
25
    }
26
27
    // Select DOMText
28 36
    if ($selector === 'text') {
29 3
      return '//text()';
30
    }
31
32
    // Select DOMComment
33 33
    if ($selector === 'comment') {
34 2
      return '//comment()';
35
    }
36
37 31
    if (!class_exists('Symfony\\Component\\CssSelector\\CssSelectorConverter')) {
38
      throw new \RuntimeException('Unable to filter with a CSS selector as the Symfony CssSelector 2.8+ is not installed (you can use filterXPath instead).');
39
    }
40
41 31
    $converter = new CssSelectorConverter(true);
42
43 31
    $xPathQuery = $converter->toXPath($selector);
44 31
    self::$compiled[$selector] = $xPathQuery;
45
46 31
    return $xPathQuery;
47
  }
48
}
49