Completed
Push — master ( 5b2184...b7d9e9 )
by Han Hui
25s queued 13s
created

TypeFactory::addNullabilityToTypeDefinition()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 9
c 1
b 0
f 0
nc 4
nop 3
dl 0
loc 18
rs 9.6111
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 ApiPlatform\Core\Api\ResourceClassResolverInterface;
17
use ApiPlatform\Core\Util\ResourceClassInfoTrait;
18
use Ramsey\Uuid\UuidInterface;
19
use Symfony\Component\PropertyInfo\Type;
20
21
/**
22
 * {@inheritdoc}
23
 *
24
 * @experimental
25
 *
26
 * @author Kévin Dunglas <[email protected]>
27
 */
28
final class TypeFactory implements TypeFactoryInterface
29
{
30
    use ResourceClassInfoTrait;
31
32
    /**
33
     * @var SchemaFactoryInterface|null
34
     */
35
    private $schemaFactory;
36
37
    public function __construct(ResourceClassResolverInterface $resourceClassResolver = null)
38
    {
39
        $this->resourceClassResolver = $resourceClassResolver;
40
    }
41
42
    public function setSchemaFactory(SchemaFactoryInterface $schemaFactory): void
43
    {
44
        $this->schemaFactory = $schemaFactory;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function getType(Type $type, string $format = 'json', ?bool $readableLink = null, ?array $serializerContext = null, Schema $schema = null): array
51
    {
52
        if ($type->isCollection()) {
53
            $keyType = $type->getCollectionKeyType();
54
            $subType = $type->getCollectionValueType() ?? new Type($type->getBuiltinType(), false, $type->getClassName(), false);
55
56
            if (null !== $keyType && Type::BUILTIN_TYPE_STRING === $keyType->getBuiltinType()) {
57
                return $this->addNullabilityToTypeDefinition([
58
                    'type' => 'object',
59
                    'additionalProperties' => $this->getType($subType, $format, $readableLink, $serializerContext, $schema),
60
                ], $type, $schema);
61
            }
62
63
            return $this->addNullabilityToTypeDefinition([
64
                'type' => 'array',
65
                'items' => $this->getType($subType, $format, $readableLink, $serializerContext, $schema),
66
            ], $type, $schema);
67
        }
68
69
        return $this->addNullabilityToTypeDefinition($this->makeBasicType($type, $format, $readableLink, $serializerContext, $schema), $type, $schema);
70
    }
71
72
    private function makeBasicType(Type $type, string $format = 'json', ?bool $readableLink = null, ?array $serializerContext = null, Schema $schema = null): array
73
    {
74
        switch ($type->getBuiltinType()) {
75
            case Type::BUILTIN_TYPE_INT:
76
                return ['type' => 'integer'];
77
            case Type::BUILTIN_TYPE_FLOAT:
78
                return ['type' => 'number'];
79
            case Type::BUILTIN_TYPE_BOOL:
80
                return ['type' => 'boolean'];
81
            case Type::BUILTIN_TYPE_OBJECT:
82
                return $this->getClassType($type->getClassName(), $format, $readableLink, $serializerContext, $schema);
83
            default:
84
                return ['type' => 'string'];
85
        }
86
    }
87
88
    /**
89
     * 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.
90
     */
91
    private function getClassType(?string $className, string $format, ?bool $readableLink, ?array $serializerContext, ?Schema $schema): array
92
    {
93
        if (null === $className) {
94
            return ['type' => 'string'];
95
        }
96
97
        if (is_a($className, \DateTimeInterface::class, true)) {
98
            return [
99
                'type' => 'string',
100
                'format' => 'date-time',
101
            ];
102
        }
103
        if (is_a($className, \DateInterval::class, true)) {
104
            return [
105
                'type' => 'string',
106
                'format' => 'duration',
107
            ];
108
        }
109
        if (is_a($className, UuidInterface::class, true)) {
110
            return [
111
                'type' => 'string',
112
                'format' => 'uuid',
113
            ];
114
        }
115
116
        // Skip if $schema is null (filters only support basic types)
117
        if (null === $schema) {
118
            return ['type' => 'string'];
119
        }
120
121
        if ($this->isResourceClass($className) && true !== $readableLink) {
122
            return [
123
                'type' => 'string',
124
                'format' => 'iri-reference',
125
            ];
126
        }
127
128
        $version = $schema->getVersion();
129
130
        $subSchema = new Schema($version);
131
        $subSchema->setDefinitions($schema->getDefinitions()); // Populate definitions of the main schema
132
133
        if (null === $this->schemaFactory) {
134
            throw new \LogicException('The schema factory must be injected by calling the "setSchemaFactory" method.');
135
        }
136
137
        $subSchema = $this->schemaFactory->buildSchema($className, $format, Schema::TYPE_OUTPUT, null, null, $subSchema, $serializerContext);
138
139
        return ['$ref' => $subSchema['$ref']];
140
    }
141
142
    /**
143
     * @param array<string, mixed> $jsonSchema
144
     *
145
     * @return array<string, mixed>
146
     */
147
    private function addNullabilityToTypeDefinition(array $jsonSchema, Type $type, ?Schema $schema): array
148
    {
149
        if ($schema && Schema::VERSION_SWAGGER === $schema->getVersion()) {
150
            return $jsonSchema;
151
        }
152
153
        if (!$type->isNullable()) {
154
            return $jsonSchema;
155
        }
156
157
        if (\array_key_exists('$ref', $jsonSchema)) {
158
            return [
159
                'nullable' => true,
160
                'anyOf' => [$jsonSchema],
161
            ];
162
        }
163
164
        return array_merge($jsonSchema, ['nullable' => true]);
165
    }
166
}
167