Passed
Branch master (268bdc)
by Gilles
01:26
created

StaticDom::mount()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 2
dl 0
loc 11
ccs 7
cts 7
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace PHPHtmlParser;
3
4
use PHPHtmlParser\Exceptions\NotLoadedException;
5
6
/**
7
 * Class StaticDom
8
 *
9
 * @package PHPHtmlParser
10
 */
11
final class StaticDom
12
{
13
14
    private static $dom = null;
15
16
    /**
17
     * Attempts to call the given method on the most recent created dom
18
     * from bellow.
19
     *
20
     * @param string $method
21
     * @param array $arguments
22
     * @throws NotLoadedException
23
     * @return mixed
24
     */
25 9
    public static function __callStatic(string $method, array $arguments)
26
    {
27 9
        if (self::$dom instanceof Dom) {
28 6
            return call_user_func_array([self::$dom, $method], $arguments);
29
        } else {
30 3
            throw new NotLoadedException('The dom is not loaded. Can not call a dom method.');
31
        }
32
    }
33
34
    /**
35
     * Call this to mount the static facade. The facade allows you to use
36
     * this object as a $className.
37
     *
38
     * @param string $className
39
     * @param Dom $dom
40
     * @return bool
41
     */
42 21
    public static function mount(string $className = 'Dom', Dom $dom = null): bool
43
    {
44 21
        if (class_exists($className)) {
45 18
            return false;
46
        }
47 3
        class_alias(__CLASS__, $className);
48 3
        if ($dom instanceof Dom) {
49 3
            self::$dom = $dom;
50
        }
51
52 3
        return true;
53
    }
54
55
    /**
56
     * Creates a new dom object and calls load() on the
57
     * new object.
58
     *
59
     * @param string $str
60
     * @return Dom
61
     */
62 9
    public static function load(string $str): Dom
63
    {
64 9
        $dom       = new Dom;
65 9
        self::$dom = $dom;
66
67 9
        return $dom->load($str);
68
    }
69
70
    /**
71
     * Creates a new dom object and calls loadFromFile() on the
72
     * new object.
73
     *
74
     * @param string $file
75
     * @return Dom
76
     */
77 3
    public static function loadFromFile(string $file): Dom
78
    {
79 3
        $dom       = new Dom;
80 3
        self::$dom = $dom;
81
82 3
        return $dom->loadFromFile($file);
83
    }
84
85
    /**
86
     * Creates a new dom object and calls loadFromUrl() on the
87
     * new object.
88
     *
89
     * @param string $url
90
     * @param array $options
91
     * @param CurlInterface $curl
92
     * @return Dom
93
     */
94 3
    public static function loadFromUrl(string $url, array $options = [], CurlInterface $curl = null): Dom
95
    {
96 3
        $dom       = new Dom;
97 3
        self::$dom = $dom;
98 3
        if (is_null($curl)) {
99
            // use the default curl interface
100
            $curl = new Curl;
101
        }
102
103 3
        return $dom->loadFromUrl($url, $options, $curl);
104
    }
105
106
    /**
107
     * Sets the $dom variable to null.
108
     */
109 21
    public static function unload(): void
110
    {
111 21
        self::$dom = null;
112 21
    }
113
}
114