Skip to content

Commit 079af6c

Browse files
committed
Split schema exporter into protected methods
1 parent 6ab0517 commit 079af6c

File tree

1 file changed

+72
-48
lines changed

1 file changed

+72
-48
lines changed

src/JsonSchema/SchemaExporterInterface.php

Lines changed: 72 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,49 @@ public function process(PhpClass $class, $path, $schema)
5555

5656
$func = new PhpFunction('exportSchema');
5757
$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
59101
if (null === self::\$schemaStorage) {
60102
self::\$schemaStorage = new SplObjectStorage();
61103
}
@@ -68,30 +110,23 @@ public function process(PhpClass $class, $path, $schema)
68110
}
69111
70112
PHP
71-
, [':schema' => new TypeOf(PhpClass::byFQN(Schema::class))]
72-
));
113+
, [':schema' => new TypeOf(PhpClass::byFQN(Schema::class))]
114+
);
115+
}
73116

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
84120
if (\$this->$name !== null && \$this->$name instanceof SchemaExporter) {
85121
\$schema->$name = \$this->{$name}->exportSchema();
86122
}
87123
88-
PHP
89-
);
90-
continue;
91-
}
124+
PHP;
125+
}
92126

93-
if ($name === $names->allOf || $name === $names->oneOf || $name === $names->anyOf) {
94-
$body->addSnippet(<<<PHP
127+
protected function buildSchemasProperty($name)
128+
{
129+
return <<<PHP
95130
if (!empty(\$this->$name)) {
96131
foreach (\$this->$name as \$i => \$item) {
97132
if (\$item instanceof SchemaExporter) {
@@ -100,49 +135,38 @@ public function process(PhpClass $class, $path, $schema)
100135
}
101136
}
102137
103-
PHP
104-
);
105-
continue;
106-
}
107-
138+
PHP;
139+
}
108140

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) {
113146
if (is_string(\$propertyName) && \$propertySchema instanceof SchemaExporter) {
114147
\$schema->setProperty(\$propertyName, \$propertySchema->exportSchema());
115148
}
116149
}
117150
}
118151
119-
PHP
120-
);
121-
continue;
122-
}
123-
152+
PHP;
153+
}
124154

125-
$body->addSnippet(<<<PHP
155+
protected function buildDefault($name)
156+
{
157+
return <<<PHP
126158
\$schema->$name = \$this->$name;
127159
128-
PHP
129-
);
130-
131-
}
160+
PHP;
161+
}
132162

133-
$body->addSnippet(<<<'PHP'
163+
protected function buildTail()
164+
{
165+
return <<<'PHP'
134166
$schema->__fromRef = $this->__fromRef;
135167
$schema->setDocumentPath($this->getDocumentPath());
136168
$schema->addMeta($this, 'origin');
137169
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;
146171
}
147-
148172
}

0 commit comments

Comments
 (0)