Issues (332)

src/JsonSchema/Schema.php (2 issues)

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 document.
18
 *
19
 * Both the standard version and the OpenAPI flavors (v2 and v3) are supported.
20
 *
21
 * @see https://json-schema.org/latest/json-schema-core.html
22
 * @see https://github.com/OAI/OpenAPI-Specification
23
 *
24
 * @experimental
25
 *
26
 * @author Kévin Dunglas <[email protected]>
27
 */
28
final class Schema extends \ArrayObject
29
{
30
    public const TYPE_INPUT = 'input';
31
    public const TYPE_OUTPUT = 'output';
32
    public const VERSION_JSON_SCHEMA = 'json-schema';
33
    public const VERSION_OPENAPI = 'openapi';
34
    public const VERSION_SWAGGER = 'swagger';
35
36
    private $version;
37
38
    public function __construct(string $version = self::VERSION_JSON_SCHEMA)
39
    {
40
        $this->version = $version;
41
42
        parent::__construct(self::VERSION_JSON_SCHEMA === $this->version ? ['$schema' => 'http://json-schema.org/draft-07/schema#'] : []);
43
    }
44
45
    /**
46
     * The flavor used for this document: JSON Schema, OpenAPI v2 or OpenAPI v3.
47
     */
48
    public function getVersion(): string
49
    {
50
        return $this->version;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     *
56
     * @param bool $includeDefinitions if set to false, definitions will not be included in the resulting array
57
     */
58
    public function getArrayCopy(bool $includeDefinitions = true): array
59
    {
60
        $schema = parent::getArrayCopy();
61
62
        if (!$includeDefinitions) {
63
            unset($schema['definitions'], $schema['components']);
64
        }
65
66
        return $schema;
67
    }
68
69
    /**
70
     * Retrieves the definitions used by this schema.
71
     */
72
    public function getDefinitions(): \ArrayObject
73
    {
74
        $definitions = $this['definitions'] ?? $this['components']['schemas'] ?? new \ArrayObject();
75
        $this->setDefinitions($definitions);
76
77
        return $definitions;
78
    }
79
80
    /**
81
     * Associates existing definitions to this schema.
82
     */
83
    public function setDefinitions(\ArrayObject $definitions): void
84
    {
85
        if (self::VERSION_OPENAPI === $this->version) {
86
            $this['components']['schemas'] = $definitions;
87
88
            return;
89
        }
90
91
        $this['definitions'] = $definitions;
92
    }
93
94
    /**
95
     * Returns the name of the root definition, if defined.
96
     */
97
    public function getRootDefinitionKey(): ?string
98
    {
99
        if (!isset($this['$ref'])) {
100
            return null;
101
        }
102
103
        // strlen('#/definitions/') = 14
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
104
        // strlen('#/components/schemas/') = 21
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
105
        $prefix = self::VERSION_OPENAPI === $this->version ? 21 : 14;
106
107
        return substr($this['$ref'], $prefix);
108
    }
109
110
    /**
111
     * Checks if this schema is initialized.
112
     */
113
    public function isDefined(): bool
114
    {
115
        return isset($this['$ref']) || isset($this['type']);
116
    }
117
}
118