Selector::isXPath()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
nc 1
cc 1
eloc 5
nop 1
1
<?php
2
namespace Zrashwani\NewsScrapper;
3
4
use Symfony\Component\CssSelector\CssSelector;
5
use Symfony\Component\CssSelector\Exception\ParseException;
6
7
/**
8
 * Set of useful functions for using CSS and XPath selector.
9
 * inspired from CodeCeption locator
10
 */
11
class Selector
12
{
13
    
14
    /**
15
     * @param $selector
16
     * @return bool
17
     */
18
    public static function isCSS($selector)
19
    {
20
        try {
21
            CssSelector::toXPath($selector);
22
        } catch (ParseException $e) {
23
            return false;
24
        }
25
        return true;
26
    }
27
    
28
29
    /**
30
     * Checks that locator is an XPath
31
     *
32
     * @param $selector
33
     * @return bool
34
     */
35
    public static function isXPath($selector)
36
    {
37
        libxml_use_internal_errors(true);
38
        $document = new \DOMDocument('1.0', 'UTF-8');
39
        $xpath = new \DOMXPath($document);
40
        return $xpath->evaluate($selector, $document) !== false;
41
    }
42
}
43