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
|
|
|
* @inheritdoc |
42
|
|
|
*/ |
43
|
1 |
|
public function __destruct() |
44
|
|
|
{ |
45
|
1 |
|
$this->xpath = $this->root = null; |
46
|
1 |
|
unset($this->xpath, $this->root); |
47
|
1 |
|
parent::__destruct(); |
48
|
1 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param InterfaceCache $cache |
52
|
|
|
*/ |
53
|
|
|
public function setCache(InterfaceCache $cache) |
54
|
|
|
{ |
55
|
|
|
$this->cache = $cache; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $url |
60
|
|
|
* |
61
|
|
|
* @return mixed |
62
|
|
|
*/ |
63
|
3 |
|
public function cache($url) |
64
|
|
|
{ |
65
|
3 |
|
if (!$this->cache) |
66
|
|
|
{ |
67
|
3 |
|
return \file_get_contents($url); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $this->cache->get($url); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param $html |
75
|
|
|
* @param bool $is_xml |
76
|
|
|
*/ |
77
|
4 |
|
public function load($html, $is_xml = false) |
78
|
|
|
{ |
79
|
4 |
|
$this->dom = new \DOMDocument(); |
80
|
4 |
|
if ($is_xml) |
81
|
|
|
{ |
82
|
|
|
$html = \preg_replace('/xmlns=".*?"/ ', '', $html); |
83
|
|
|
} |
84
|
|
|
|
85
|
4 |
|
$this->dom->loadHTML($html, LIBXML_NOERROR); |
86
|
4 |
|
$this->xpath = new \DOMXPath($this->dom); |
87
|
|
|
//$this->root = new AHTMLNode($this->dom->documentElement, $this->doc); |
|
|
|
|
88
|
4 |
|
$this->root = $this->at('body'); |
|
|
|
|
89
|
4 |
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param $file |
93
|
|
|
* @param bool $is_xml |
94
|
|
|
* |
95
|
|
|
* @deprecated loadFile |
96
|
|
|
*/ |
97
|
|
|
public function load_file($file, $is_xml = false) |
98
|
|
|
{ |
99
|
|
|
$this->loadFile($file, $is_xml); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $file |
104
|
|
|
* @param bool $is_xml |
105
|
|
|
* |
106
|
|
|
* @return $this |
107
|
|
|
*/ |
108
|
3 |
|
public function loadFile($file, $is_xml = false) |
109
|
|
|
{ |
110
|
3 |
|
$this->load($this->cache($file), $is_xml); |
111
|
|
|
|
112
|
3 |
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
// special cases |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return mixed |
119
|
|
|
*/ |
120
|
|
|
public function text() |
121
|
|
|
{ |
122
|
|
|
return $this->root->text; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return mixed |
127
|
|
|
*/ |
128
|
|
|
public function title() |
129
|
|
|
{ |
130
|
|
|
return $this->at('title')->text(); |
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
} |
134
|
|
|
|