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