1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bavix\AdvancedHtmlDom; |
4
|
|
|
|
5
|
|
|
use Bavix\AdvancedHtmlDom\CacheSystem\InterfaceCache; |
6
|
|
|
|
7
|
|
|
class AdvancedHtmlDom extends AdvancedHtmlBase |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @var |
12
|
|
|
*/ |
13
|
|
|
public $xpath; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var |
17
|
|
|
*/ |
18
|
|
|
public $root; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var InterfaceCache |
22
|
|
|
*/ |
23
|
|
|
protected $cache; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* AdvancedHtmlDom constructor. |
27
|
|
|
* |
28
|
|
|
* @param null $html |
|
|
|
|
29
|
|
|
* @param bool $is_xml |
30
|
|
|
*/ |
31
|
4 |
|
public function __construct($html = null, $is_xml = false) |
32
|
|
|
{ |
33
|
4 |
|
$this->doc = $this; |
34
|
4 |
|
if ($html) |
35
|
|
|
{ |
36
|
1 |
|
$this->load($html, $is_xml); |
37
|
|
|
} |
38
|
4 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param InterfaceCache $cache |
42
|
|
|
*/ |
43
|
|
|
public function setCache(InterfaceCache $cache) |
44
|
|
|
{ |
45
|
|
|
$this->cache = $cache; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $url |
50
|
|
|
* |
51
|
|
|
* @return mixed |
52
|
|
|
*/ |
53
|
3 |
|
public function cache($url) |
54
|
|
|
{ |
55
|
3 |
|
if (!$this->cache) |
56
|
|
|
{ |
57
|
3 |
|
return \file_get_contents($url); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $this->cache->get($url); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param $html |
65
|
|
|
* @param bool $is_xml |
66
|
|
|
*/ |
67
|
4 |
|
public function load($html, $is_xml = false) |
68
|
|
|
{ |
69
|
4 |
|
$this->dom = new \DOMDocument(); |
|
|
|
|
70
|
4 |
|
if ($is_xml) |
71
|
|
|
{ |
72
|
|
|
$this->dom->loadXML(\preg_replace('/xmlns=".*?"/ ', '', $html)); |
73
|
|
|
} |
74
|
|
|
else |
75
|
|
|
{ |
76
|
4 |
|
$this->dom->loadHTML($html); |
77
|
|
|
} |
78
|
4 |
|
$this->xpath = new \DOMXPath($this->dom); |
79
|
|
|
//$this->root = new AHTMLNode($this->dom->documentElement, $this->doc); |
|
|
|
|
80
|
4 |
|
$this->root = $this->at('body'); |
|
|
|
|
81
|
4 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param $file |
85
|
|
|
* @param bool $is_xml |
86
|
|
|
* |
87
|
|
|
* @deprecated loadFile |
88
|
|
|
*/ |
89
|
|
|
public function load_file($file, $is_xml = false) |
90
|
|
|
{ |
91
|
|
|
$this->loadFile($file, $is_xml); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string $file |
96
|
|
|
* @param bool $is_xml |
97
|
|
|
* |
98
|
|
|
* @return $this |
99
|
|
|
*/ |
100
|
3 |
|
public function loadFile($file, $is_xml = false) |
101
|
|
|
{ |
102
|
3 |
|
$this->load($this->cache($file), $is_xml); |
103
|
|
|
|
104
|
3 |
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
// special cases |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return mixed |
111
|
|
|
*/ |
112
|
|
|
public function text() |
113
|
|
|
{ |
114
|
|
|
return $this->root->text; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return mixed |
119
|
|
|
*/ |
120
|
|
|
public function title() |
121
|
|
|
{ |
122
|
|
|
return $this->at('title')->text(); |
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
} |
126
|
|
|
|