Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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