|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Author: Nil Portugués Calderó <[email protected]> |
|
4
|
|
|
* Date: 12/18/15 |
|
5
|
|
|
* Time: 10:46 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
|
|
|
/** |
|
14
|
|
|
* Class SchemaClassWriter. |
|
15
|
|
|
*/ |
|
16
|
|
|
class SchemaClassWriter extends SchemaWriter |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @param SchemaRdfaData $schemaData |
|
20
|
|
|
*/ |
|
21
|
|
|
public function write(SchemaRdfaData $schemaData) |
|
22
|
|
|
{ |
|
23
|
|
|
foreach ($schemaData->getClassesWithPropertyNames() as $class) { |
|
24
|
|
|
$className = $class->name; |
|
25
|
|
|
$methods = []; |
|
26
|
|
|
$usableProperties = []; |
|
27
|
|
|
$dynamicCall = []; |
|
28
|
|
|
$useProperties = []; |
|
29
|
|
|
|
|
30
|
|
|
if (!empty($class->properties)) { |
|
31
|
|
|
foreach ($class->properties as $propertyClassName => $property) { |
|
32
|
|
|
$propertyName = $property['name']; |
|
33
|
|
|
$parentClassName = $property['parent']; |
|
34
|
|
|
$propertyNameMethodName = $propertyName; |
|
35
|
|
|
$propertyNameMethodName[0] = strtoupper($propertyNameMethodName[0]); |
|
36
|
|
|
|
|
37
|
|
|
$dynamicCall[] = ' * @method static \\NilPortugues\\SchemaOrg\\Properties\\'.$propertyNameMethodName.'Property '.$propertyName.'()'; |
|
38
|
|
|
|
|
39
|
|
|
if (empty($usableProperties[$propertyNameMethodName])) { |
|
40
|
|
|
$usableProperties[$propertyNameMethodName] = $propertyClassName; |
|
41
|
|
|
|
|
42
|
|
|
$schemaClass = '\\NilPortugues\\SchemaOrg\\Classes\\'.$className; |
|
43
|
|
|
if ($parentClassName !== $className) { |
|
44
|
|
|
$schemaClass = '\\NilPortugues\\SchemaOrg\\Classes\\'.$parentClassName; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$methods[$propertyName] = "\t\t'{$propertyName}' => [\n\t\t\t'propertyClass' => '\\NilPortugues\\SchemaOrg\\Properties\\{$propertyNameMethodName}Property',\n\t\t\t'schemaClass' => '{$schemaClass}',\n\t\t],"; |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$useProperties[] = 'use NilPortugues\SchemaOrg\Mapping;'; |
|
53
|
|
|
$useProperties[] = 'use NilPortugues\SchemaOrg\SchemaClass;'; |
|
54
|
|
|
|
|
55
|
|
|
sort($useProperties, SORT_REGULAR); |
|
56
|
|
|
$useProperties = implode("\n", array_unique($useProperties)); |
|
57
|
|
|
ksort($methods, SORT_REGULAR); |
|
58
|
|
|
$methods = implode("\n", $methods); |
|
59
|
|
|
$dynamicCall = implode("\n", $dynamicCall); |
|
60
|
|
|
|
|
61
|
|
|
$schemaData = (array) $class; |
|
62
|
|
|
|
|
63
|
|
|
$phpCode = <<<PHP |
|
64
|
|
|
<?php |
|
65
|
|
|
/** |
|
66
|
|
|
* Author: Nil Portugués Calderó <[email protected]> |
|
67
|
|
|
* Date: 12/18/15 |
|
68
|
|
|
* Time: 11:36 PM. |
|
69
|
|
|
* |
|
70
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
71
|
|
|
* file that was distributed with this source code. |
|
72
|
|
|
*/ |
|
73
|
|
|
|
|
74
|
|
|
namespace NilPortugues\SchemaOrg\Classes; |
|
75
|
|
|
|
|
76
|
|
|
{$useProperties} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* METHODSTART. |
|
80
|
|
|
{$dynamicCall} |
|
81
|
|
|
* METHODEND. |
|
82
|
|
|
* |
|
83
|
|
|
* {$schemaData['doc']} |
|
84
|
|
|
*/ |
|
85
|
|
|
class {$schemaData['name']} extends SchemaClass |
|
86
|
|
|
{ |
|
87
|
|
|
/** |
|
88
|
|
|
* @var string |
|
89
|
|
|
*/ |
|
90
|
|
|
protected static \$schemaUrl = "{$schemaData['url']}"; |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @var array |
|
94
|
|
|
*/ |
|
95
|
|
|
protected static \$supportedMethods = [ |
|
96
|
|
|
{$methods} |
|
97
|
|
|
]; |
|
98
|
|
|
} |
|
99
|
|
|
PHP; |
|
100
|
|
|
|
|
101
|
|
|
$this->fileSystem->write($this->savePath.DIRECTORY_SEPARATOR.$className.'.php', $phpCode); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|