1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace YusufKandemir\MicrodataParser; |
4
|
|
|
|
5
|
|
|
class MicrodataParser |
6
|
|
|
{ |
7
|
|
|
/** @var callable|null */ |
8
|
|
|
protected $absoluteUriHandler; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @param callable|null $absoluteUriHandler |
12
|
|
|
* |
13
|
|
|
* @see MicrodataElementParser::$absoluteUriHandler |
14
|
|
|
*/ |
15
|
39 |
|
public function __construct(callable $absoluteUriHandler = null) |
16
|
|
|
{ |
17
|
39 |
|
$this->absoluteUriHandler = $absoluteUriHandler; |
18
|
39 |
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Parses HTML string, extracts microdata from it |
22
|
|
|
* |
23
|
|
|
* @param string $html HTML string to be parsed |
24
|
|
|
* @param string $documentURI DocumentURI to be used in absolutizing URIs |
25
|
|
|
* |
26
|
|
|
* @return \stdClass |
27
|
|
|
*/ |
28
|
15 |
|
public function parseHTML(string $html, string $documentURI = '') : \stdClass |
29
|
|
|
{ |
30
|
15 |
|
$dom = new \DOMDocument; |
31
|
15 |
|
$dom->loadHTML($html, \LIBXML_NOERROR); |
32
|
15 |
|
$dom->documentURI = $documentURI; |
33
|
|
|
|
34
|
15 |
|
return $this->parse($dom); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Parses HTML file, extracts microdata from it |
39
|
|
|
* |
40
|
|
|
* @param string $path Path to the file to be parsed |
41
|
|
|
* @param string $documentURI DocumentURI to be used in absolutizing URIs |
42
|
|
|
* |
43
|
|
|
* @return \stdClass |
44
|
|
|
*/ |
45
|
12 |
|
public function parseHTMLFile(string $path, string $documentURI = '') : \stdClass |
46
|
|
|
{ |
47
|
12 |
|
$dom = new \DOMDocument; |
48
|
12 |
|
$dom->loadHTMLFile($path, \LIBXML_NOERROR); |
49
|
12 |
|
$dom->documentURI = $documentURI; |
50
|
|
|
|
51
|
12 |
|
return $this->parse($dom); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Creates a MicrodataParser from a DOMDocument instance. |
56
|
|
|
* If you have MicrodataDOMDocument then instantiate MicrodataParser class directly to avoid conversion. |
57
|
|
|
* |
58
|
|
|
* @param \DOMDocument $domDocument DOMDocument to be parsed. |
59
|
|
|
* @param string $documentURI If non-empty value is provided, |
60
|
|
|
* it will be new value of documentURI property of $domDocument. |
61
|
|
|
* |
62
|
|
|
* @return \stdClass |
63
|
|
|
*/ |
64
|
12 |
|
public function parseDOMDocument(\DOMDocument $domDocument, string $documentURI = '') : \stdClass |
65
|
|
|
{ |
66
|
12 |
|
if (!empty($documentURI)) { |
67
|
|
|
$domDocument->documentURI = $documentURI; |
68
|
|
|
} |
69
|
|
|
|
70
|
12 |
|
return $this->parse($domDocument); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param \DOMDocument $dom |
75
|
|
|
* |
76
|
|
|
* @see MicrodataElementParser::$absoluteUriHandler |
77
|
|
|
* |
78
|
|
|
* @return \stdClass |
79
|
|
|
*/ |
80
|
39 |
|
protected function parse(\DOMDocument $dom) : \stdClass |
81
|
|
|
{ |
82
|
39 |
|
$elementParser = new MicrodataElementParser($this->absoluteUriHandler); |
83
|
39 |
|
$documentParser = new MicrodataDocumentParser($dom, $elementParser); |
84
|
|
|
|
85
|
39 |
|
return $documentParser->parse(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param callable|null $absoluteUriHandler |
90
|
|
|
* |
91
|
|
|
* @see MicrodataElementParser::$absoluteUriHandler |
92
|
|
|
* |
93
|
|
|
* @return MicrodataParser |
94
|
|
|
*/ |
95
|
3 |
|
public function setAbsoluteUriHandler(callable $absoluteUriHandler = null) : self |
96
|
|
|
{ |
97
|
3 |
|
$this->absoluteUriHandler = $absoluteUriHandler; |
98
|
|
|
|
99
|
3 |
|
return $this; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|