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