Passed
Pull Request — master (#2983)
by Kévin
04:28
created

Schema   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 31
c 2
b 0
f 0
dl 0
loc 65
rs 10
wmc 16

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getVersion() 0 3 1
B __construct() 0 14 7
A getSchema() 0 17 6
A getDefinitions() 0 3 1
A getRootDefinitionId() 0 3 1
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
/**
17
 * Represents a JSON Schema.
18
 *
19
 * @see https://json-schema.org/latest/json-schema-core.html
20
 *
21
 * @author Kévin Dunglas <[email protected]>
22
 */
23
final class Schema
24
{
25
    public const VERSION_JSON_SCHEMA = 'json-schema';
26
    public const VERSION_SWAGGER = 'swagger';
27
    public const VERSION_OPENAPI = 'openapi';
28
29
    private $version;
30
    private $schema;
31
    private $definitions;
32
    private $rootDefinitionId;
33
34
    public function __construct(string $version = self::VERSION_JSON_SCHEMA, ?\ArrayObject $baseSchema = null, ?string $rootDefinitionId = null, ?\ArrayObject $baseDefinitions = null)
35
    {
36
        $this->version = $version;
37
        $this->schema = $baseSchema ?? new \ArrayObject();
38
        $this->rootDefinitionId = $rootDefinitionId;
39
40
        if (null !== $baseDefinitions) {
41
            $this->definitions = $baseDefinitions;
42
        } elseif (self::VERSION_OPENAPI === $version && isset($this->schema['components']['schemas'])) {
43
            $this->definitions = $this->schema['components']['schemas'] instanceof \ArrayObject ? $this->schema['components']['schemas'] : new \ArrayObject();
44
        } elseif (isset($this->schema['definitions'])) {
45
            $this->definitions = $this->schema['definitions'] instanceof \ArrayObject ? $this->schema['definitions'] : new \ArrayObject();
46
        } else {
47
            $this->definitions = new \ArrayObject();
48
        }
49
    }
50
51
    public function getVersion(): string
52
    {
53
        return $this->version;
54
    }
55
56
    public function getSchema(): \ArrayObject
57
    {
58
        if (self::VERSION_JSON_SCHEMA === $this->version && !isset($this->schema['$schema'])) {
59
            $this->schema['$schema'] = 'http://json-schema.org/draft-07/schema#';
60
        }
61
62
        if (!isset($this->schema['type']) && !isset($this->schema['$ref'])) {
63
            $this->schema['$ref'] = '#/definitions/'.$this->rootDefinitionId;
64
        }
65
66
        if (self::VERSION_OPENAPI === $this->version) {
67
            $this->schema['components']['schemas'] = $this->definitions;
68
        } else {
69
            $this->schema['definitions'] = $this->definitions;
70
        }
71
72
        return $this->schema;
73
    }
74
75
    /**
76
     * @see https://json-schema.org/latest/json-schema-core.html#rfc.section.8.2.4
77
     *
78
     * @internal
79
     */
80
    public function getDefinitions(): \ArrayObject
81
    {
82
        return $this->definitions;
83
    }
84
85
    public function getRootDefinitionId(): ?string
86
    {
87
        return $this->rootDefinitionId;
88
    }
89
}
90