@@ -55,7 +55,49 @@ public function process(PhpClass $class, $path, $schema)
55
55
56
56
$ func = new PhpFunction ('exportSchema ' );
57
57
$ func ->setResult (PhpClass::byFQN (Schema::class));
58
- $ body = (new PhpCode ())->addSnippet (new PlaceholderString (<<<PHP
58
+ $ body = (new PhpCode ())->addSnippet ($ this ->buildHead ());
59
+
60
+ $ names = Schema::names ();
61
+ foreach ($ propertiesFound as $ name ) {
62
+ if ($ name === $ names ->items
63
+ || $ name === $ names ->additionalProperties
64
+ || $ name === $ names ->additionalItems
65
+ || $ name === $ names ->not
66
+ || $ name === $ names ->if
67
+ || $ name === $ names ->then
68
+ || $ name === $ names ->else ) {
69
+ $ body ->addSnippet ($ this ->buildSchemaProperty ($ name ));
70
+ continue ;
71
+ }
72
+
73
+ if ($ name === $ names ->allOf || $ name === $ names ->oneOf || $ name === $ names ->anyOf ) {
74
+ $ body ->addSnippet ($ this ->buildSchemasProperty ($ name ));
75
+ continue ;
76
+ }
77
+
78
+
79
+ if ($ name === $ names ->properties ) {
80
+ $ body ->addSnippet ($ this ->buildProperties ());
81
+ continue ;
82
+ }
83
+
84
+
85
+ $ body ->addSnippet ($ this ->buildDefault ($ name ));
86
+
87
+ }
88
+
89
+ $ body ->addSnippet ($ this ->buildTail ());
90
+
91
+ $ func ->setBody ($ body );
92
+ $ class ->addMethod ($ func );
93
+ $ class ->addImplements (PhpInterface::byFQN (SchemaExporter::class));
94
+ }
95
+
96
+ }
97
+
98
+ protected function buildHead ()
99
+ {
100
+ return new PlaceholderString (<<<PHP
59
101
if (null === self:: \$schemaStorage) {
60
102
self:: \$schemaStorage = new SplObjectStorage();
61
103
}
@@ -68,30 +110,23 @@ public function process(PhpClass $class, $path, $schema)
68
110
}
69
111
70
112
PHP
71
- , [':schema ' => new TypeOf (PhpClass::byFQN (Schema::class))]
72
- ));
113
+ , [':schema ' => new TypeOf (PhpClass::byFQN (Schema::class))]
114
+ );
115
+ }
73
116
74
- $ names = Schema::names ();
75
- foreach ($ propertiesFound as $ name ) {
76
- if ($ name === $ names ->items
77
- || $ name === $ names ->additionalProperties
78
- || $ name === $ names ->additionalItems
79
- || $ name === $ names ->not
80
- || $ name === $ names ->if
81
- || $ name === $ names ->then
82
- || $ name === $ names ->else ) {
83
- $ body ->addSnippet (<<<PHP
117
+ protected function buildSchemaProperty ($ name )
118
+ {
119
+ return <<<PHP
84
120
if ( \$this-> $ name !== null && \$this-> $ name instanceof SchemaExporter) {
85
121
\$schema-> $ name = \$this-> {$ name }->exportSchema();
86
122
}
87
123
88
- PHP
89
- );
90
- continue ;
91
- }
124
+ PHP ;
125
+ }
92
126
93
- if ($ name === $ names ->allOf || $ name === $ names ->oneOf || $ name === $ names ->anyOf ) {
94
- $ body ->addSnippet (<<<PHP
127
+ protected function buildSchemasProperty ($ name )
128
+ {
129
+ return <<<PHP
95
130
if (!empty( \$this-> $ name)) {
96
131
foreach ( \$this-> $ name as \$i => \$item) {
97
132
if ( \$item instanceof SchemaExporter) {
@@ -100,49 +135,38 @@ public function process(PhpClass $class, $path, $schema)
100
135
}
101
136
}
102
137
103
- PHP
104
- );
105
- continue ;
106
- }
107
-
138
+ PHP ;
139
+ }
108
140
109
- if ($ name === $ names ->properties ) {
110
- $ body ->addSnippet (<<<PHP
111
- if (!empty( \$this-> $ name)) {
112
- foreach ( \$this-> $ name as \$propertyName => \$propertySchema) {
141
+ protected function buildProperties ()
142
+ {
143
+ return <<<PHP
144
+ if (!empty( \$this->properties)) {
145
+ foreach ( \$this->properties as \$propertyName => \$propertySchema) {
113
146
if (is_string( \$propertyName) && \$propertySchema instanceof SchemaExporter) {
114
147
\$schema->setProperty( \$propertyName, \$propertySchema->exportSchema());
115
148
}
116
149
}
117
150
}
118
151
119
- PHP
120
- );
121
- continue ;
122
- }
123
-
152
+ PHP ;
153
+ }
124
154
125
- $ body ->addSnippet (<<<PHP
155
+ protected function buildDefault ($ name )
156
+ {
157
+ return <<<PHP
126
158
\$schema-> $ name = \$this-> $ name;
127
159
128
- PHP
129
- );
130
-
131
- }
160
+ PHP ;
161
+ }
132
162
133
- $ body ->addSnippet (<<<'PHP'
163
+ protected function buildTail ()
164
+ {
165
+ return <<<'PHP'
134
166
$schema->__fromRef = $this->__fromRef;
135
167
$schema->setDocumentPath($this->getDocumentPath());
136
168
$schema->addMeta($this, 'origin');
137
169
return $schema;
138
- PHP
139
- );
140
-
141
- $ func ->setBody ($ body );
142
- $ class ->addMethod ($ func );
143
- $ class ->addImplements (PhpInterface::byFQN (SchemaExporter::class));
144
- }
145
-
170
+ PHP;
146
171
}
147
-
148
172
}
0 commit comments