1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/* |
3
|
|
|
* This file is part of the KleijnWeb\PhpApi\Descriptions package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace KleijnWeb\PhpApi\Descriptions\Description\Schema; |
10
|
|
|
|
11
|
|
|
use KleijnWeb\PhpApi\Descriptions\Description\ComplexType; |
12
|
|
|
use KleijnWeb\PhpApi\Descriptions\Hydrator\ClassNameResolver; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @author John Kleijn <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class SchemaFactory |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
private $schemas = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var ComplexType[] |
26
|
|
|
*/ |
27
|
|
|
private $complexTypes = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var ObjectSchema[] |
31
|
|
|
*/ |
32
|
|
|
private $typedSchemas = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
private $definitions = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var ClassNameResolver |
41
|
|
|
*/ |
42
|
|
|
private $classNameResolver; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param \stdClass|null $definition |
46
|
|
|
* @param string|null $name |
47
|
|
|
* |
48
|
|
|
* @return Schema |
49
|
|
|
*/ |
50
|
|
|
public function create(\stdClass $definition = null, string $name = null): Schema |
51
|
|
|
{ |
52
|
|
|
if (!$definition) { |
53
|
|
|
$definition = (object)[]; |
54
|
|
|
$definition->type = Schema::TYPE_ANY; |
55
|
|
|
} |
56
|
|
|
if (!isset($definition->type)) { |
57
|
|
|
$definition = clone $definition; |
58
|
|
|
if (isset($definition->allOf)) { |
59
|
|
|
foreach ($definition->allOf as $nested) { |
60
|
|
|
if (isset($nested->type)) { |
61
|
|
|
$definition->type = $nested->type; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
if (!isset($definition->type)) { |
66
|
|
|
$definition->type = Schema::TYPE_STRING; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
if (isset($definition->properties)) { |
70
|
|
|
$definition->type = 'object'; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$index = var_export($definition, true); |
74
|
|
|
|
75
|
|
|
if (!isset($this->schemas[$index])) { |
76
|
|
|
if ($definition->type == Schema::TYPE_OBJECT) { |
77
|
|
|
$propertySchemas = (object)[]; |
78
|
|
|
|
79
|
|
View Code Duplication |
if (isset($definition->properties)) { |
|
|
|
|
80
|
|
|
foreach ($definition->properties as $attributeName => $propertyDefinition) { |
81
|
|
|
$propertySchemas->$attributeName = $this->create($propertyDefinition); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if (isset($definition->allOf)) { |
86
|
|
|
foreach ($definition->allOf as $nested) { |
87
|
|
|
if (isset($nested->{'x-ref-id'})) { |
88
|
|
|
continue; |
89
|
|
|
} |
90
|
|
View Code Duplication |
if (isset($nested->properties)) { |
|
|
|
|
91
|
|
|
foreach ($nested->properties as $attributeName => $propertyDefinition) { |
92
|
|
|
$propertySchemas->$attributeName = $this->create($propertyDefinition); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
unset($definition->type); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if ($name === null && isset($definition->{'x-ref-id'})) { |
100
|
|
|
$name = $this->getTypeNameFromRefId($definition); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$schema = new ObjectSchema($definition, $propertySchemas, null, $name); |
104
|
|
|
|
105
|
|
|
if (null !== $name) { |
106
|
|
|
if (isset($this->typedSchemas[$name])) { |
107
|
|
|
$a = var_export($schema, true); |
|
|
|
|
108
|
|
|
$b = var_export($this->typedSchemas[$name], true); |
|
|
|
|
109
|
|
|
$a2 = var_export($definition, true); |
|
|
|
|
110
|
|
|
$b2 = var_export($this->typedSchemas[$name]->getDefinition(), true); |
|
|
|
|
111
|
|
|
throw new \InvalidArgumentException("Type '$name' already exists"); |
112
|
|
|
} |
113
|
|
|
$this->typedSchemas[$name] = $schema; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
} elseif ($definition->type == Schema::TYPE_ARRAY) { |
117
|
|
|
$itemsSchema = isset($definition->items) ? $this->create($definition->items) : null; |
118
|
|
|
$schema = new ArraySchema($definition, $itemsSchema); |
119
|
|
|
} elseif ($definition->type == Schema::TYPE_ANY) { |
120
|
|
|
$schema = new AnySchema($definition); |
121
|
|
|
} else { |
122
|
|
|
$schema = new ScalarSchema($definition); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$this->schemas[$index] = $schema; |
126
|
|
|
|
127
|
|
|
return $schema; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $this->schemas[$index]; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return ComplexType[] |
135
|
|
|
*/ |
136
|
|
|
public function resolveTypes(): array |
137
|
|
|
{ |
138
|
|
|
foreach ($this->typedSchemas as $name => $schema) { |
139
|
|
|
$schema->setComplexType(new ComplexType( |
140
|
|
|
$name, |
141
|
|
|
$schema, |
142
|
|
|
$this->classNameResolver->resolve($name) |
143
|
|
|
)); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
foreach ($this->typedSchemas as $name => $schema) { |
147
|
|
|
$definition = $schema->getDefinition(); |
148
|
|
|
|
149
|
|
|
if (isset($definition->allOf)) { |
150
|
|
|
foreach ($definition->allOf as $partial) { |
151
|
|
|
if (isset($partial->{'x-ref-id'})) { |
152
|
|
|
$this->typedSchemas[$schema->getXType()] |
153
|
|
|
->getComplexType() |
154
|
|
|
->addParent( |
155
|
|
|
$this->typedSchemas[$this->getTypeNameFromRefId($partial)]->getComplexType() |
|
|
|
|
156
|
|
|
); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return array_map(function(ObjectSchema $schema){ |
163
|
|
|
return $schema->getComplexType(); |
164
|
|
|
}, $this->typedSchemas); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param ClassNameResolver $classNameResolver |
169
|
|
|
*/ |
170
|
|
|
public function setClassNameResolver(ClassNameResolver $classNameResolver) |
171
|
|
|
{ |
172
|
|
|
$this->classNameResolver = $classNameResolver; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
private function getTypeNameFromRefId(\stdClass $definition) |
176
|
|
|
{ |
177
|
|
|
return substr($definition->{'x-ref-id'}, strrpos($definition->{'x-ref-id'}, '/') + 1); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.