Document::toString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 5
ccs 2
cts 2
cp 1
rs 9.4285
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Jclyons52\PHPQuery;
4
5
use Jclyons52\PHPQuery\Support\NodeCollection;
6
use Symfony\Component\CssSelector\CssSelectorConverter;
7
8
class Document
9
{
10
11
    private $dom;
12
13
    private $xpath;
14
15 42
    public function __construct($html)
16
    {
17 42
        $this->dom = new \DOMDocument();
18
19 42
        $this->dom->loadHtml($html);
20
21 42
        $this->xpath = new \DOMXPath($this->dom);
22 42
    }
23
24
    /**
25
     * @param string $selector
26
     * @return Node
27
     */
28 24
    public function querySelector($selector)
29
    {
30 24
        $converter = new CssSelectorConverter();
31
32 24
        $xpathQuery = $converter->toXPath($selector);
33
34 24
        $result = $this->xpath->query($xpathQuery);
35
36 24
        return new Node($result->item(0));
37
38
    }
39
40
    /**
41
     * @param string $selector
42
     * @return NodeCollection
43
     */
44 21
    public function querySelectorAll($selector)
45 3
    {
46 21
        $converter = new CssSelectorConverter();
47
48 18
        $xpathQuery = $converter->toXPath($selector);
49
50 18
        $results = $this->xpath->query($xpathQuery);
51
52 18
        $return = new NodeCollection();
53
54 18
        for ($i = 0; $i < $results->length; $i++) {
55 18
            $node = new Node($results->item($i));
56 18
            $return->append($node);
57 18
        }
58
59 18
        return $return;
60
    }
61
62
    /**
63
     * @return string
64
     */
65 3
    public function toString()
66
    {
67 3
        return $this->dom->saveHTML();
68
69
    }
70
}
71