|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace YusufKandemir\MicrodataParser; |
|
4
|
|
|
|
|
5
|
|
|
abstract class Microdata |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* Creates a MicrodataParser from HTML string |
|
9
|
|
|
* |
|
10
|
|
|
* @param string $html HTML string to be parsed |
|
11
|
|
|
* @param string $documentURI DocumentURI to be used in absolutizing URIs |
|
12
|
|
|
* |
|
13
|
|
|
* @return MicrodataParser |
|
14
|
|
|
*/ |
|
15
|
3 |
|
public static function fromHTML(string $html, string $documentURI = '') : MicrodataParser |
|
16
|
|
|
{ |
|
17
|
3 |
|
$dom = new MicrodataDOMDocument; |
|
18
|
3 |
|
$dom->loadHTML($html, LIBXML_NOERROR); |
|
19
|
3 |
|
$dom->documentURI = $documentURI; |
|
20
|
|
|
|
|
21
|
3 |
|
return new MicrodataParser($dom); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Creates a MicrodataParser from a HTML file |
|
26
|
|
|
* |
|
27
|
|
|
* @param string $filename Path to the file to be parsed |
|
28
|
|
|
* @param string $documentURI DocumentURI to be used in absolutizing URIs |
|
29
|
|
|
* |
|
30
|
|
|
* @return MicrodataParser |
|
31
|
|
|
*/ |
|
32
|
3 |
|
public static function fromHTMLFile(string $filename, string $documentURI = '') : MicrodataParser |
|
33
|
|
|
{ |
|
34
|
3 |
|
$dom = new MicrodataDOMDocument; |
|
35
|
3 |
|
$dom->loadHTMLFile($filename, LIBXML_NOERROR); |
|
36
|
3 |
|
$dom->documentURI = $documentURI; |
|
37
|
|
|
|
|
38
|
3 |
|
return new MicrodataParser($dom); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Creates a MicrodataParser from a DOMDocument instance. |
|
43
|
|
|
* If you have MicrodataDOMDocument then instantiate MicrodataParser class directly to avoid conversion. |
|
44
|
|
|
* |
|
45
|
|
|
* @param \DOMDocument $domDocument DOMDocument to be parsed. |
|
46
|
|
|
* Needs to have documentURI property to be used in absolutizing URIs if wanted. |
|
47
|
|
|
* |
|
48
|
|
|
* @return MicrodataParser |
|
49
|
|
|
*/ |
|
50
|
3 |
|
public static function fromDOMDocument(\DOMDocument $domDocument) : MicrodataParser |
|
51
|
|
|
{ |
|
52
|
3 |
|
$dom = new MicrodataDOMDocument; |
|
53
|
3 |
|
$importedNode = $dom->importNode($domDocument->documentElement, true); |
|
54
|
3 |
|
$dom->appendChild($importedNode); |
|
55
|
|
|
|
|
56
|
3 |
|
return new MicrodataParser($dom); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|