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 VERSION_JSON_SCHEMA = 'json-schema'; |
||
31 | public const VERSION_SWAGGER = 'swagger'; |
||
32 | public const VERSION_OPENAPI = 'openapi'; |
||
33 | |||
34 | private $version; |
||
35 | |||
36 | public function __construct(string $version = self::VERSION_JSON_SCHEMA) |
||
37 | { |
||
38 | $this->version = $version; |
||
39 | |||
40 | parent::__construct(self::VERSION_JSON_SCHEMA === $this->version ? ['$schema' => 'http://json-schema.org/draft-07/schema#'] : []); |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * The flavor used for this document: JSON Schema, OpenAPI v2 or OpenAPI v3. |
||
45 | */ |
||
46 | public function getVersion(): string |
||
47 | { |
||
48 | return $this->version; |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * @param bool $includeDefinitions if set to false, definitions will not be included in the resulting array |
||
53 | */ |
||
54 | public function getArrayCopy(bool $includeDefinitions = true): array |
||
55 | { |
||
56 | $schema = parent::getArrayCopy(); |
||
57 | |||
58 | if (!$includeDefinitions) { |
||
59 | unset($schema['definitions'], $schema['components']); |
||
60 | } |
||
61 | |||
62 | return $schema; |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Retrieves the definitions used by this schema. |
||
67 | */ |
||
68 | public function getDefinitions(): \ArrayObject |
||
69 | { |
||
70 | $definitions = $this['definitions'] ?? $this['components']['schemas'] ?? new \ArrayObject(); |
||
71 | $this->setDefinitions($definitions); |
||
72 | |||
73 | return $definitions; |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * Associates existing definitions to this schema. |
||
78 | */ |
||
79 | public function setDefinitions(\ArrayObject $definitions): void |
||
80 | { |
||
81 | if (self::VERSION_OPENAPI === $this->version) { |
||
82 | $this['components']['schemas'] = $definitions; |
||
83 | |||
84 | return; |
||
85 | } |
||
86 | |||
87 | $this['definitions'] = $definitions; |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Returns the name of the root definition, if defined. |
||
92 | */ |
||
93 | public function getRootDefinitionKey(): ?string |
||
94 | { |
||
95 | if (!isset($this['$ref'])) { |
||
96 | return null; |
||
97 | } |
||
98 | |||
99 | // strlen('#/definitions/') = 14 |
||
0 ignored issues
–
show
|
|||
100 | // 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...
|
|||
101 | $prefix = self::VERSION_OPENAPI === $this->version ? 21 : 14; |
||
102 | |||
103 | return substr($this['$ref'], $prefix); |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Checks if this schema is initialized. |
||
108 | */ |
||
109 | public function isDefined(): bool |
||
110 | { |
||
111 | return isset($this['$ref']) || isset($this['type']); |
||
112 | } |
||
113 | } |
||
114 |
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.