|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace YusufKandemir\MicrodataParser; |
|
4
|
|
|
|
|
5
|
|
|
class MicrodataParser |
|
6
|
|
|
{ |
|
7
|
|
|
/** @var MicrodataDOMDocument */ |
|
8
|
|
|
protected $dom; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Handler will be called with $value(non-absolute uri string) and $base(base uri) parameters |
|
12
|
|
|
* |
|
13
|
|
|
* Should return a string value |
|
14
|
|
|
* |
|
15
|
|
|
* @var callable |
|
16
|
|
|
*/ |
|
17
|
|
|
private $absoluteUriHandler; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* MicrodataParser constructor. |
|
21
|
|
|
* |
|
22
|
|
|
* @param MicrodataDOMDocument $dom |
|
23
|
|
|
* @param callable $absoluteUriHandler Can be set later with MicrodataParser::setAbsoluteUriHandler() |
|
24
|
|
|
* |
|
25
|
|
|
* @see MicrodataParser::$absoluteUriHandler |
|
26
|
|
|
*/ |
|
27
|
36 |
|
public function __construct(MicrodataDOMDocument $dom, callable $absoluteUriHandler = null) |
|
28
|
|
|
{ |
|
29
|
36 |
|
$dom->registerNodeClass(\DOMElement::class, MicrodataDOMElement::class); |
|
30
|
|
|
|
|
31
|
36 |
|
$this->dom = $dom; |
|
32
|
12 |
|
$this->absoluteUriHandler = $absoluteUriHandler ?: function ($value, $base) { |
|
33
|
27 |
|
return $base . $value; |
|
34
|
36 |
|
}; |
|
35
|
36 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Extracts and converts microdata to associative array |
|
39
|
|
|
* |
|
40
|
|
|
* @return array |
|
41
|
|
|
*/ |
|
42
|
9 |
|
public function toArray() : array |
|
43
|
|
|
{ |
|
44
|
|
|
// Somewhat hacky way to convert deep objects |
|
45
|
9 |
|
return json_decode(json_encode($this->extractMicrodata()), true); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Extracts and converts microdata to object |
|
50
|
|
|
* |
|
51
|
|
|
* @return \stdClass |
|
52
|
|
|
*/ |
|
53
|
9 |
|
public function toObject() : \stdClass |
|
54
|
|
|
{ |
|
55
|
9 |
|
return $this->extractMicrodata(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Extracts and converts microdata to json using \json_encode() |
|
60
|
|
|
* |
|
61
|
|
|
* @see \json_encode() to description of parameters and return values |
|
62
|
|
|
* |
|
63
|
|
|
* @param int $options |
|
64
|
|
|
* @param int $depth |
|
65
|
|
|
* |
|
66
|
|
|
* @return false|string |
|
67
|
|
|
*/ |
|
68
|
9 |
|
public function toJSON($options = 0, $depth = 512) |
|
69
|
|
|
{ |
|
70
|
9 |
|
return json_encode($this->extractMicrodata(), $options, $depth); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @return \stdClass |
|
75
|
|
|
*/ |
|
76
|
27 |
|
protected function extractMicrodata() : \stdClass |
|
77
|
|
|
{ |
|
78
|
27 |
|
$result = new \stdClass; |
|
79
|
|
|
|
|
80
|
27 |
|
$result->items = []; |
|
81
|
|
|
|
|
82
|
27 |
|
foreach ($this->dom->getItems() as $item) { |
|
83
|
27 |
|
$result->items[] = $this->getObject($item); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
27 |
|
return $result; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @see https://www.w3.org/TR/2018/WD-microdata-20180426/#dfn-get-the-object |
|
91
|
|
|
* |
|
92
|
|
|
* @param MicrodataDOMElement $item |
|
93
|
|
|
* @param array $memory |
|
94
|
|
|
* |
|
95
|
|
|
* @return \stdClass |
|
96
|
|
|
*/ |
|
97
|
27 |
|
protected function getObject(MicrodataDOMElement $item, $memory = []) : \stdClass |
|
98
|
|
|
{ |
|
99
|
27 |
|
$result = new \stdClass; |
|
100
|
|
|
|
|
101
|
27 |
|
$memory[] = $item; |
|
102
|
|
|
|
|
103
|
27 |
|
$result->type = $item->tokenizeAttribute('itemtype'); |
|
104
|
|
|
// @todo Check if types are valid absolute urls |
|
105
|
|
|
|
|
106
|
27 |
|
if ($item->hasAttribute('itemid')) { |
|
107
|
|
|
$result->id = $item->getAttribute('itemid'); |
|
108
|
|
|
} |
|
109
|
|
|
// @todo Check if item ids are valid absolute urls or like isbn:xxx |
|
110
|
|
|
|
|
111
|
27 |
|
$properties = new \stdClass; |
|
112
|
|
|
|
|
113
|
27 |
|
foreach ($item->getProperties() as $element) { |
|
114
|
27 |
|
$value = $element->getPropertyValue($this->absoluteUriHandler); |
|
115
|
|
|
|
|
116
|
27 |
|
if ($this->isItem($value)) { |
|
117
|
18 |
|
foreach ($memory as $memory_item) { |
|
118
|
18 |
|
if ($element->isSameNode($memory_item)) { |
|
119
|
|
|
$value = 'ERROR'; |
|
120
|
18 |
|
break; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
18 |
|
if ($value != 'ERROR') { |
|
125
|
18 |
|
$value = $this->getObject($value, $memory); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
27 |
|
foreach ($element->getPropertyNames() as $name) { |
|
130
|
27 |
|
$properties->{$name}[] = $value; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
27 |
|
$result->properties = $properties; |
|
135
|
|
|
|
|
136
|
27 |
|
return $result; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Set absolute uri handler |
|
141
|
|
|
* |
|
142
|
|
|
* @param callable $handler |
|
143
|
|
|
*/ |
|
144
|
|
|
public function setAbsoluteUriHandler(callable $handler) |
|
145
|
|
|
{ |
|
146
|
|
|
$this->absoluteUriHandler = $handler; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Check if the given parameter is a MicrodataDOMElement and has itemscope attribute |
|
151
|
|
|
* |
|
152
|
|
|
* @param $element |
|
153
|
|
|
* |
|
154
|
|
|
* @return bool |
|
155
|
|
|
*/ |
|
156
|
27 |
|
protected function isItem($element) : bool |
|
157
|
|
|
{ |
|
158
|
27 |
|
return $element instanceof MicrodataDOMElement && $element->hasAttribute('itemscope'); |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|