-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTypeFactory.php
200 lines (168 loc) · 6.93 KB
/
TypeFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ApiPlatform\JsonSchema;
use ApiPlatform\Metadata\ResourceClassResolverInterface;
use ApiPlatform\Metadata\Util\ResourceClassInfoTrait;
use Ramsey\Uuid\UuidInterface;
use Symfony\Component\PropertyInfo\Type;
use Symfony\Component\Uid\Ulid;
use Symfony\Component\Uid\Uuid;
/**
* {@inheritdoc}
*
* @author Kévin Dunglas <[email protected]>
*/
final class TypeFactory implements TypeFactoryInterface
{
use ResourceClassInfoTrait;
private ?SchemaFactoryInterface $schemaFactory = null;
public function __construct(?ResourceClassResolverInterface $resourceClassResolver = null)
{
$this->resourceClassResolver = $resourceClassResolver;
}
public function setSchemaFactory(SchemaFactoryInterface $schemaFactory): void
{
$this->schemaFactory = $schemaFactory;
}
/**
* {@inheritdoc}
*/
public function getType(Type $type, string $format = 'json', ?bool $readableLink = null, ?array $serializerContext = null, ?Schema $schema = null): array
{
if ($type->isCollection()) {
$keyType = $type->getCollectionKeyTypes()[0] ?? null;
$subType = ($type->getCollectionValueTypes()[0] ?? null) ?? new Type($type->getBuiltinType(), false, $type->getClassName(), false);
if (null !== $keyType && Type::BUILTIN_TYPE_STRING === $keyType->getBuiltinType()) {
return $this->addNullabilityToTypeDefinition([
'type' => 'object',
'additionalProperties' => $this->getType($subType, $format, $readableLink, $serializerContext, $schema),
], $type, $schema);
}
return $this->addNullabilityToTypeDefinition([
'type' => 'array',
'items' => $this->getType($subType, $format, $readableLink, $serializerContext, $schema),
], $type, $schema);
}
return $this->addNullabilityToTypeDefinition($this->makeBasicType($type, $format, $readableLink, $serializerContext, $schema), $type, $schema);
}
private function makeBasicType(Type $type, string $format = 'json', ?bool $readableLink = null, ?array $serializerContext = null, ?Schema $schema = null): array
{
return match ($type->getBuiltinType()) {
Type::BUILTIN_TYPE_INT => ['type' => 'integer'],
Type::BUILTIN_TYPE_FLOAT => ['type' => 'number'],
Type::BUILTIN_TYPE_BOOL => ['type' => 'boolean'],
Type::BUILTIN_TYPE_OBJECT => $this->getClassType($type->getClassName(), $type->isNullable(), $format, $readableLink, $serializerContext, $schema),
default => ['type' => 'string'],
};
}
/**
* Gets the JSON Schema document which specifies the data type corresponding to the given PHP class, and recursively adds needed new schema to the current schema if provided.
*/
private function getClassType(?string $className, bool $nullable, string $format, ?bool $readableLink, ?array $serializerContext, ?Schema $schema): array
{
if (null === $className) {
return ['type' => 'string'];
}
if (is_a($className, \DateTimeInterface::class, true)) {
return [
'type' => 'string',
'format' => 'date-time',
];
}
if (is_a($className, \DateInterval::class, true)) {
return [
'type' => 'string',
'format' => 'duration',
];
}
if (is_a($className, UuidInterface::class, true) || is_a($className, Uuid::class, true)) {
return [
'type' => 'string',
'format' => 'uuid',
];
}
if (is_a($className, Ulid::class, true)) {
return [
'type' => 'string',
'format' => 'ulid',
];
}
if (is_a($className, \SplFileInfo::class, true)) {
return [
'type' => 'string',
'format' => 'binary',
];
}
if (!$this->isResourceClass($className) && is_a($className, \BackedEnum::class, true)) {
$enumCases = array_map(static fn (\BackedEnum $enum): string|int => $enum->value, $className::cases());
$type = \is_string($enumCases[0] ?? '') ? 'string' : 'integer';
if ($nullable) {
$enumCases[] = null;
}
return [
'type' => $type,
'enum' => $enumCases,
];
}
// Skip if $schema is null (filters only support basic types)
if (null === $schema) {
return ['type' => 'string'];
}
if (true !== $readableLink && $this->isResourceClass($className)) {
return [
'type' => 'string',
'format' => 'iri-reference',
'example' => 'https://example.com/',
];
}
$version = $schema->getVersion();
$subSchema = new Schema($version);
$subSchema->setDefinitions($schema->getDefinitions()); // Populate definitions of the main schema
if (null === $this->schemaFactory) {
throw new \LogicException('The schema factory must be injected by calling the "setSchemaFactory" method.');
}
$serializerContext += [SchemaFactory::FORCE_SUBSCHEMA => true];
$subSchema = $this->schemaFactory->buildSchema($className, $format, Schema::TYPE_OUTPUT, null, $subSchema, $serializerContext, false);
return ['$ref' => $subSchema['$ref']];
}
/**
* @param array<string, mixed> $jsonSchema
*
* @return array<string, mixed>
*/
private function addNullabilityToTypeDefinition(array $jsonSchema, Type $type, ?Schema $schema): array
{
if ($schema && Schema::VERSION_SWAGGER === $schema->getVersion()) {
return $jsonSchema;
}
if (!$type->isNullable()) {
return $jsonSchema;
}
if (\array_key_exists('$ref', $jsonSchema)) {
$typeDefinition = ['anyOf' => [$jsonSchema]];
if ($schema && Schema::VERSION_JSON_SCHEMA === $schema->getVersion()) {
$typeDefinition['anyOf'][] = ['type' => 'null'];
} else {
// OpenAPI < 3.1
$typeDefinition['nullable'] = true;
}
return $typeDefinition;
}
if ($schema && Schema::VERSION_JSON_SCHEMA === $schema->getVersion()) {
return [...$jsonSchema, ...[
'type' => \is_array($jsonSchema['type'])
? array_merge($jsonSchema['type'], ['null'])
: [$jsonSchema['type'], 'null'],
]];
}
return [...$jsonSchema, ...['nullable' => true]];
}
}