|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Author: Nil Portugués Calderó <[email protected]> |
|
4
|
|
|
* Date: 12/18/15 |
|
5
|
|
|
* Time: 10:43 PM. |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace NilPortugues\SchemaOrg\Generator; |
|
12
|
|
|
|
|
13
|
|
|
class SchemaRdfaData |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var array |
|
17
|
|
|
*/ |
|
18
|
|
|
private $classes = []; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var array |
|
22
|
|
|
*/ |
|
23
|
|
|
private $properties = []; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var bool |
|
27
|
|
|
*/ |
|
28
|
|
|
private $hasBeenCalculated = false; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var |
|
32
|
|
|
*/ |
|
33
|
|
|
private $calculatedOutput; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param $classUrl |
|
37
|
|
|
* @param $label |
|
38
|
|
|
* @param $classComment |
|
39
|
|
|
* @param $subClassOf |
|
40
|
|
|
*/ |
|
41
|
|
|
public function addClass($classUrl, $label, $classComment, $subClassOf) |
|
42
|
|
|
{ |
|
43
|
|
|
$url = explode('/', $classUrl); |
|
44
|
|
|
$nameFromUrl = array_pop($url); |
|
45
|
|
|
|
|
46
|
|
|
if ($label !== $nameFromUrl) { |
|
47
|
|
|
$label = $nameFromUrl; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$label = $this->renameIfPhpReservedWord(trim($label)); |
|
51
|
|
|
|
|
52
|
|
|
$this->classes[$label] = (object) [ |
|
53
|
|
|
'name' => $label, |
|
54
|
|
|
'doc' => $classComment, |
|
55
|
|
|
'url' => $classUrl, |
|
56
|
|
|
'properties' => [], |
|
57
|
|
|
'subClassOf' => array_map([$this, 'renameIfPhpReservedWord'], array_filter($subClassOf)), |
|
58
|
|
|
]; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param $classUrl |
|
63
|
|
|
* @param $label |
|
64
|
|
|
* @param $classComment |
|
65
|
|
|
*/ |
|
66
|
|
|
public function addDataType($classUrl, $label, $classComment) |
|
67
|
|
|
{ |
|
68
|
|
|
$this->addClass($classUrl, $label, $classComment, [], []); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param $propertyUrl |
|
73
|
|
|
* @param $label |
|
74
|
|
|
* @param $propertyComment |
|
75
|
|
|
* @param $mainClass |
|
76
|
|
|
* @param $expectedType |
|
77
|
|
|
*/ |
|
78
|
|
|
public function addProperty($propertyUrl, $label, $propertyComment, $mainClass, $expectedType) |
|
79
|
|
|
{ |
|
80
|
|
|
$label = $this->renameIfPhpReservedWord(trim($label)); |
|
81
|
|
|
|
|
82
|
|
|
$this->properties[$label] = [ |
|
83
|
|
|
'name' => $label, |
|
84
|
|
|
'doc' => $propertyComment, |
|
85
|
|
|
'url' => $propertyUrl, |
|
86
|
|
|
'usedOnClass' => array_map([$this, 'renameIfPhpReservedWord'], array_filter($mainClass)), |
|
87
|
|
|
'expectedType' => array_map([$this, 'renameIfPhpReservedWord'], array_filter($expectedType)), |
|
88
|
|
|
]; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param $name |
|
93
|
|
|
* |
|
94
|
|
|
* @return string |
|
95
|
|
|
*/ |
|
96
|
|
|
private function renameIfPhpReservedWord($name) |
|
97
|
|
|
{ |
|
98
|
|
|
$keywords = [ |
|
99
|
|
|
//PHP7 |
|
100
|
|
|
'int', |
|
101
|
|
|
'float', |
|
102
|
|
|
'bool', |
|
103
|
|
|
'string', |
|
104
|
|
|
'true', |
|
105
|
|
|
'false', |
|
106
|
|
|
'null', |
|
107
|
|
|
'resource', |
|
108
|
|
|
'object', |
|
109
|
|
|
'mixed', |
|
110
|
|
|
'numeric', |
|
111
|
|
|
//PHP7 and under |
|
112
|
|
|
'__halt_compiler', |
|
113
|
|
|
'abstract', |
|
114
|
|
|
'and', |
|
115
|
|
|
'array', |
|
116
|
|
|
'as', |
|
117
|
|
|
'break', |
|
118
|
|
|
'callable', |
|
119
|
|
|
'case', |
|
120
|
|
|
'catch', |
|
121
|
|
|
'class', |
|
122
|
|
|
'clone', |
|
123
|
|
|
'const', |
|
124
|
|
|
'continue', |
|
125
|
|
|
'declare', |
|
126
|
|
|
'default', |
|
127
|
|
|
'die', |
|
128
|
|
|
'do', |
|
129
|
|
|
'echo', |
|
130
|
|
|
'else', |
|
131
|
|
|
'elseif', |
|
132
|
|
|
'empty', |
|
133
|
|
|
'enddeclare', |
|
134
|
|
|
'endfor', |
|
135
|
|
|
'endforeach', |
|
136
|
|
|
'endif', |
|
137
|
|
|
'endswitch', |
|
138
|
|
|
'endwhile', |
|
139
|
|
|
'eval', |
|
140
|
|
|
'exit', |
|
141
|
|
|
'extends', |
|
142
|
|
|
'final', |
|
143
|
|
|
'for', |
|
144
|
|
|
'foreach', |
|
145
|
|
|
'function', |
|
146
|
|
|
'global', |
|
147
|
|
|
'goto', |
|
148
|
|
|
'if', |
|
149
|
|
|
'implements', |
|
150
|
|
|
'include', |
|
151
|
|
|
'include_once', |
|
152
|
|
|
'instanceof', |
|
153
|
|
|
'insteadof', |
|
154
|
|
|
'interface', |
|
155
|
|
|
'isset', |
|
156
|
|
|
'list', |
|
157
|
|
|
'namespace', |
|
158
|
|
|
'new', |
|
159
|
|
|
'or', |
|
160
|
|
|
'print', |
|
161
|
|
|
'private', |
|
162
|
|
|
'protected', |
|
163
|
|
|
'public', |
|
164
|
|
|
'require', |
|
165
|
|
|
'require_once', |
|
166
|
|
|
'return', |
|
167
|
|
|
'static', |
|
168
|
|
|
'switch', |
|
169
|
|
|
'throw', |
|
170
|
|
|
'trait', |
|
171
|
|
|
'try', |
|
172
|
|
|
'unset', |
|
173
|
|
|
'use', |
|
174
|
|
|
'var', |
|
175
|
|
|
'while', |
|
176
|
|
|
'xor', |
|
177
|
|
|
]; |
|
178
|
|
|
|
|
179
|
|
|
if (in_array(strtolower($name), $keywords, true)) { |
|
180
|
|
|
$name = $name.'Type'; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
return $name; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* @return array |
|
188
|
|
|
*/ |
|
189
|
|
|
public function getProperties() |
|
190
|
|
|
{ |
|
191
|
|
|
return $this->properties; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* @return array |
|
196
|
|
|
*/ |
|
197
|
|
|
public function getClassesWithPropertyNames() |
|
198
|
|
|
{ |
|
199
|
|
|
if ($this->hasBeenCalculated) { |
|
200
|
|
|
return $this->calculatedOutput; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
$result = $this->classes; |
|
204
|
|
|
|
|
205
|
|
|
//Link the subClasses |
|
206
|
|
|
foreach ($result as &$resultingClass) { |
|
207
|
|
|
if (!empty($resultingClass->subClassOf)) { |
|
208
|
|
|
foreach ($resultingClass->subClassOf as &$class) { |
|
209
|
|
|
$class = $result[$class]; |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
//Add the properties |
|
215
|
|
|
foreach ($this->properties as $propertyName => $propertyStructure) { |
|
216
|
|
|
foreach ($propertyStructure['usedOnClass'] as $class) { |
|
217
|
|
|
$result[$class]->properties[$propertyName] = ['name' => $propertyName, 'parent' => $class]; |
|
218
|
|
|
} |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
//Flatten the properties for easy use. |
|
222
|
|
|
foreach ($result as $resultingClass) { |
|
223
|
|
|
if (!empty($resultingClass->subClassOf) && is_array($resultingClass->subClassOf)) { |
|
224
|
|
|
foreach ($resultingClass->subClassOf as $stdClassObject) { |
|
225
|
|
|
if (is_object($stdClassObject)) { |
|
226
|
|
|
$resultingClass->properties = array_merge($resultingClass->properties, $stdClassObject->properties); |
|
227
|
|
|
|
|
228
|
|
|
$next = $stdClassObject->subClassOf; |
|
229
|
|
|
while (false !== $next && $object = array_pop($next)) { |
|
230
|
|
|
$resultingClass->properties = array_merge($resultingClass->properties, $object->properties); |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
} |
|
234
|
|
|
} |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
$this->hasBeenCalculated = true; |
|
238
|
|
|
$this->calculatedOutput = $result; |
|
239
|
|
|
|
|
240
|
|
|
return $result; |
|
241
|
|
|
} |
|
242
|
|
|
} |
|
243
|
|
|
|