Completed
Pull Request — master (#17)
by Auke
03:27
created

xml::parse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 5
Bugs 2 Features 2
Metric Value
c 5
b 2
f 2
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
/*
4
 * This file is part of the Ariadne Component Library.
5
 *
6
 * (c) Muze <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace arc;
13
14
class xml
15
{
16
17 2
    public static function __callStatic( $name, $args )
18
    {
19 2
        return call_user_func_array( [ new xml\Writer(), $name ], $args );
20
    }
21
22 2
    public static function parse( $xml, $encoding = null )
23
    {
24 2
        $parser = new xml\Parser();
25 2
        return $parser->parse( $xml, $encoding );
26
    }
27
28 1
    public static function css2XPath( $cssSelector )
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
29
    {
30
        /* (c) Tijs Verkoyen - http://blog.verkoyen.eu/blog/p/detail/css-selector-to-xpath-query/ */
31
        $cssSelectors = array(
32
            // E F: Matches any F element that is a descendant of an E element
33 1
            '/(\w+)\s+(?=([^"]*"[^"]*")*[^"]*$)(\w+)/',
34
            // E > F: Matches any F element that is a child of an element E
35
            '/(\w+)\s*>\s*(\w+)/',
36
            // E:first-child: Matches element E when E is the first child of its parent
37
            '/(\w+):first-child/',
38
            // E + F: Matches any F element immediately preceded by an element
39
            '/(\w+)\s*\+\s*(\w+)/',
40
            // E ~ F: Matches any F element preceded by an element
41
            '/(\w+)\s*\~\s*(\w+)/',
42
            // E[foo]: Matches any E element with the "foo" attribute set (whatever the value)
43
            '/(\w+)\[([\w\-]+)]/',
44
            // E[foo="warning"]: Matches any E element whose "foo" attribute value is exactly equal to "warning"
45
            '/(\w+)\[([\w\-]+)\=\"(.*)\"]/',
46
            // .warning: HTML only. The same as *[class~="warning"]
47
            '/(^|\s)\.([\w\-]+)+/',
48
            // div.warning: HTML only. The same as DIV[class~="warning"]
49
            '/(\w+|\*)\.([\w\-]+)+/',
50
            // E#myid: Matches any E element with id-attribute equal to "myid"
51
            '/(\w+)+\#([\w\-]+)/',
52
            // #myid: Matches any E element with id-attribute equal to "myid"
53 1
            '/\#([\w\-]+)/'
54
        );
55
56
        $xPathQueries = array(
57
            '\1//\3',
58
            '\1/\2',
59
            '*[1]/self::\1',
60
            '\1/following-sibling::*[1]/self::\2',
61
            '\1/following-sibling::*/self::\2',
62
            '\1 [ @\2 ]',
63
            '\1[ contains( concat( " ", normalize-space(@\2), " " ), concat( " ", "\3", " " ) ) ]',
64 1
            '*[ contains( concat( " ", normalize-space(@class), " " ), concat( " ", "\2", " " ) ) ]',
65
            '\1[ contains( concat( " ", normalize-space(@class), " " ), concat( " ", "\2", " " ) ) ]',
66
            '\1[ @id = "\2" ]',
67
            '*[ @id = "\1" ]'
68
        );
69
70
        $continue = true;
71
        while ( $continue ) {
72
            $cssSelector = (string) preg_replace($cssSelectors, $xPathQueries, $cssSelector);
73
            $continue = false;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
74
            foreach ( $cssSelectors as $selector ) {
75
                if ( preg_match($selector, $cssSelector) ) {
76
                    $continue = true;
77
                    break;
78
                }       
79
            }
80
        }
81
        return '//'.$cssSelector;
82
    }
83
}
84