| 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 |
||||||
|
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||||||
| 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) { |
|||||
|
0 ignored issues
–
show
|
|||||||
| 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'); |
|||||
|
0 ignored issues
–
show
The method
at() does not exist on Bavix\AdvancedHtmlDom\AdvancedHtmlDom. Since you implemented __call, consider adding a @method annotation.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 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(); |
||||||
|
0 ignored issues
–
show
The method
text() does not exist on Bavix\AdvancedHtmlDom\AHTMLNodeList. Since you implemented __call, consider adding a @method annotation.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 128 | } |
||||||
| 129 | |||||||
| 130 | } |
||||||
| 131 |