forked from zircote/swagger-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAugmentProperties.php
192 lines (178 loc) · 7.38 KB
/
AugmentProperties.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php declare(strict_types=1);
/**
* @license Apache 2.0
*/
namespace OpenApi\Processors;
use OpenApi\Analysis;
use OpenApi\Annotations\Components;
use OpenApi\Annotations\Items;
use OpenApi\Annotations\Property;
use OpenApi\Annotations\Schema;
use OpenApi\Context;
use OpenApi\Util;
/**
* Use the property context to extract useful information and inject that into the annotation.
*/
class AugmentProperties
{
public static $types = [
'array' => 'array',
'byte' => ['string', 'byte'],
'boolean' => 'boolean',
'bool' => 'boolean',
'int' => 'integer',
'integer' => 'integer',
'long' => ['integer', 'long'],
'float' => ['number', 'float'],
'double' => ['number', 'double'],
'string' => 'string',
'date' => ['string', 'date'],
'datetime' => ['string', 'date-time'],
'\datetime' => ['string', 'date-time'],
'datetimeimmutable' => ['string', 'date-time'],
'\datetimeimmutable' => ['string', 'date-time'],
'datetimeinterface' => ['string', 'date-time'],
'\datetimeinterface' => ['string', 'date-time'],
'number' => 'number',
'object' => 'object',
];
public function __invoke(Analysis $analysis)
{
$refs = [];
if ($analysis->openapi->components !== UNDEFINED && $analysis->openapi->components->schemas !== UNDEFINED) {
foreach ($analysis->openapi->components->schemas as $schema) {
if ($schema->schema !== UNDEFINED) {
$refs[strtolower($schema->_context->fullyQualifiedName($schema->_context->class))]
= Components::SCHEMA_REF.Util::refEncode($schema->schema);
}
}
}
$allProperties = $analysis->getAnnotationsOfType(Property::class);
foreach ($allProperties as $property) {
$context = $property->_context;
// Use the property names for @OA\Property()
if ($property->property === UNDEFINED) {
$property->property = $context->property;
}
if ($property->ref !== UNDEFINED) {
continue;
}
$comment = str_replace("\r\n", "\n", (string) $context->comment);
if ($property->type === UNDEFINED && $context->type && $context->type !== UNDEFINED) {
if ($context->nullable === true) {
$property->nullable = true;
}
$type = strtolower($context->type);
if (isset(self::$types[$type])) {
$this->applyType($property, static::$types[$type]);
} else {
$key = strtolower($context->fullyQualifiedName($type));
if ($property->ref === UNDEFINED && array_key_exists($key, $refs)) {
$this->applyRef($property, $refs[$key]);
continue;
}
}
} elseif (preg_match('/@var\s+(?<type>[^\s]+)([ \t])?(?<description>.+)?$/im', $comment, $varMatches)) {
if ($property->type === UNDEFINED) {
preg_match('/^([^\[]+)(.*$)/', trim($varMatches['type']), $typeMatches);
$isNullable = $this->isNullable($typeMatches[1]);
$type = $this->stripNull($typeMatches[1]);
if (array_key_exists(strtolower($type), static::$types) === false) {
$key = strtolower($context->fullyQualifiedName($type));
if ($property->ref === UNDEFINED && $typeMatches[2] === '' && array_key_exists($key, $refs)) {
if ($isNullable) {
$property->oneOf = [
new Schema([
'_context' => $property->_context,
'ref' => $refs[$key],
]),
];
$property->nullable = true;
} else {
$property->ref = $refs[$key];
}
continue;
}
} else {
$type = static::$types[strtolower($type)];
if (is_array($type)) {
if ($property->format === UNDEFINED) {
$property->format = $type[1];
}
$type = $type[0];
}
$property->type = $type;
}
if ($typeMatches[2] === '[]') {
if ($property->items === UNDEFINED) {
$property->items = new Items(
[
'type' => $property->type,
'_context' => new Context(['generated' => true], $context),
]
);
if ($property->items->type === UNDEFINED) {
$key = strtolower($context->fullyQualifiedName($type));
$property->items->ref = array_key_exists($key, $refs) ? $refs[$key] : null;
}
}
$property->type = 'array';
}
if ($isNullable && $property->nullable === UNDEFINED) {
$property->nullable = true;
}
}
if ($property->description === UNDEFINED && isset($varMatches['description'])) {
$property->description = trim($varMatches['description']);
}
}
if ($property->example === UNDEFINED && preg_match('/@example\s+([ \t])?(?<example>.+)?$/im', $comment, $varMatches)) {
$property->example = $varMatches['example'];
}
if ($property->description === UNDEFINED) {
$property->description = $context->phpdocContent();
}
}
}
protected function isNullable(string $typeDescription): bool
{
return in_array('null', explode('|', strtolower($typeDescription)));
}
protected function stripNull(string $typeDescription): string
{
if (strpos($typeDescription, '|') === false) {
return $typeDescription;
}
$types = [];
foreach (explode('|', $typeDescription) as $type) {
if (strtolower($type) === 'null') {
continue;
}
$types[] = $type;
}
return implode('|', $types);
}
protected function applyType(Property $property, $type): void
{
if (is_array($type)) {
if ($property->format === UNDEFINED) {
$property->format = $type[1];
}
$type = $type[0];
}
$property->type = $type;
}
protected function applyRef(Property $property, string $ref): void
{
if ($property->nullable === true) {
$property->oneOf = [
new Schema([
'_context' => $property->_context,
'ref' => $ref,
]),
];
} else {
$property->ref = $ref;
}
}
}