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|null |
16
|
|
|
*/ |
17
|
|
|
private $absoluteUriHandler; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* MicrodataParser constructor. |
21
|
|
|
* |
22
|
|
|
* @param MicrodataDOMDocument $dom |
23
|
|
|
* @param callable|null $absoluteUriHandler Can be set later with MicrodataParser::setAbsoluteUriHandler() |
24
|
|
|
* |
25
|
|
|
* @see MicrodataParser::$absoluteUriHandler |
26
|
|
|
*/ |
27
|
48 |
|
public function __construct(MicrodataDOMDocument $dom, callable $absoluteUriHandler = null) |
28
|
|
|
{ |
29
|
48 |
|
$dom->registerNodeClass(\DOMElement::class, MicrodataDOMElement::class); |
30
|
|
|
|
31
|
48 |
|
$this->dom = $dom; |
32
|
16 |
|
$this->absoluteUriHandler = $absoluteUriHandler ?: function ($value, $base) { |
33
|
30 |
|
return $base . $value; |
34
|
48 |
|
}; |
35
|
48 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Extracts and converts microdata to associative array |
39
|
|
|
* |
40
|
|
|
* @return array |
41
|
|
|
*/ |
42
|
12 |
|
public function toArray() : array |
43
|
|
|
{ |
44
|
|
|
// Somewhat hacky way to convert deep objects |
45
|
12 |
|
return json_decode(json_encode($this->extractMicrodata()), true); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Extracts and converts microdata to object |
50
|
|
|
* |
51
|
|
|
* @return \stdClass |
52
|
|
|
*/ |
53
|
15 |
|
public function toObject() : \stdClass |
54
|
|
|
{ |
55
|
15 |
|
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
|
12 |
|
public function toJSON($options = 0, $depth = 512) |
69
|
|
|
{ |
70
|
12 |
|
return json_encode($this->extractMicrodata(), $options, $depth); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return \stdClass |
75
|
|
|
*/ |
76
|
39 |
|
protected function extractMicrodata() : \stdClass |
77
|
|
|
{ |
78
|
39 |
|
$result = new \stdClass; |
79
|
|
|
|
80
|
39 |
|
$result->items = []; |
81
|
|
|
|
82
|
39 |
|
foreach ($this->dom->getItems() as $item) { |
83
|
39 |
|
$result->items[] = $this->getObject($item); |
84
|
|
|
} |
85
|
|
|
|
86
|
39 |
|
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
|
39 |
|
protected function getObject(MicrodataDOMElement $item, $memory = []) : \stdClass |
98
|
|
|
{ |
99
|
39 |
|
$result = new \stdClass; |
100
|
|
|
|
101
|
39 |
|
$memory[] = $item; |
102
|
|
|
|
103
|
39 |
|
$result->type = $item->tokenizeAttribute('itemtype'); |
104
|
|
|
// @todo Check if types are valid absolute urls |
105
|
|
|
|
106
|
39 |
|
if ($item->hasAttribute('itemid')) { |
107
|
9 |
|
$result->id = $item->getAttribute('itemid'); |
108
|
|
|
} |
109
|
|
|
// @todo Check if item ids are valid absolute urls or like isbn:xxx |
110
|
|
|
|
111
|
39 |
|
$properties = new \stdClass; |
112
|
|
|
|
113
|
39 |
|
foreach ($item->getProperties() as $element) { |
114
|
39 |
|
$value = $element->getPropertyValue($this->absoluteUriHandler); |
115
|
|
|
|
116
|
39 |
|
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
|
39 |
|
foreach ($element->getPropertyNames() as $name) { |
130
|
39 |
|
$properties->{$name}[] = $value; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
39 |
|
$result->properties = $properties; |
135
|
|
|
|
136
|
39 |
|
return $result; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Set absolute uri handler |
141
|
|
|
* |
142
|
|
|
* @param callable $handler |
143
|
|
|
*/ |
144
|
3 |
|
public function setAbsoluteUriHandler(callable $handler) |
145
|
|
|
{ |
146
|
3 |
|
$this->absoluteUriHandler = $handler; |
147
|
3 |
|
} |
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
|
39 |
|
protected function isItem($element) : bool |
157
|
|
|
{ |
158
|
39 |
|
return $element instanceof MicrodataDOMElement && $element->hasAttribute('itemscope'); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|