1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the API Platform project. |
5
|
|
|
* |
6
|
|
|
* (c) Kévin Dunglas <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace ApiPlatform\Core\JsonSchema; |
15
|
|
|
|
16
|
|
|
use Symfony\Component\PropertyInfo\Type; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* {@inheritdoc} |
20
|
|
|
* |
21
|
|
|
* @experimental |
22
|
|
|
* |
23
|
|
|
* @author Kévin Dunglas <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
final class TypeFactory implements TypeFactoryInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var SchemaFactoryInterface|null |
29
|
|
|
*/ |
30
|
|
|
private $schemaFactory; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Injects the JSON Schema factory to use. |
34
|
|
|
*/ |
35
|
|
|
public function setSchemaFactory(SchemaFactoryInterface $schemaFactory): void |
36
|
|
|
{ |
37
|
|
|
$this->schemaFactory = $schemaFactory; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Gets the OpenAPI type corresponding to the given PHP type, and recursively adds needed new schema to the current schema if provided. |
42
|
|
|
*/ |
43
|
|
|
public function getType(Type $type, string $format = 'json', ?bool $readableLink = null, ?array $serializerContext = null, Schema $schema = null): array |
44
|
|
|
{ |
45
|
|
|
if ($type->isCollection()) { |
46
|
|
|
$subType = new Type($type->getBuiltinType(), $type->isNullable(), $type->getClassName(), false); |
47
|
|
|
|
48
|
|
|
return [ |
49
|
|
|
'type' => 'array', |
50
|
|
|
'items' => $this->getType($subType, $format, $readableLink, $serializerContext, $schema), |
51
|
|
|
]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
switch ($type->getBuiltinType()) { |
55
|
|
|
case Type::BUILTIN_TYPE_INT: |
56
|
|
|
return ['type' => 'integer']; |
57
|
|
|
case Type::BUILTIN_TYPE_FLOAT: |
58
|
|
|
return ['type' => 'number']; |
59
|
|
|
case Type::BUILTIN_TYPE_BOOL: |
60
|
|
|
return ['type' => 'boolean']; |
61
|
|
|
case Type::BUILTIN_TYPE_OBJECT: |
62
|
|
|
return $this->getClassType($type->getClassName(), $format, $readableLink, $serializerContext, $schema); |
63
|
|
|
default: |
64
|
|
|
return ['type' => 'string']; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Gets the OpenAPI type corresponding to the given PHP class, and recursively adds needed new schema to the current schema if provided. |
70
|
|
|
*/ |
71
|
|
|
private function getClassType(?string $className, string $format = 'json', ?bool $readableLink = null, ?array $serializerContext = null, ?Schema $schema = null): array |
72
|
|
|
{ |
73
|
|
|
if (null === $className) { |
74
|
|
|
return ['type' => 'string']; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if (is_subclass_of($className, \DateTimeInterface::class)) { |
78
|
|
|
return ['type' => 'string', 'format' => 'date-time']; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if (null !== $this->schemaFactory && true === $readableLink && null !== $schema) { // Skip if $baseSchema is null (filters only support basic types) |
82
|
|
|
$version = $schema->getVersion(); |
83
|
|
|
|
84
|
|
|
$subSchema = new Schema($version); |
85
|
|
|
/* |
86
|
|
|
* @var Schema $schema Prevents a false positive in PHPStan |
87
|
|
|
*/ |
88
|
|
|
$subSchema->setDefinitions($schema->getDefinitions()); // Populate definitions of the main schema |
89
|
|
|
|
90
|
|
|
$this->schemaFactory->buildSchema($className, $format, true, null, null, $subSchema, $serializerContext); |
91
|
|
|
|
92
|
|
|
return ['$ref' => $subSchema['$ref']]; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return ['type' => 'string']; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|