forked from nelmio/NelmioApiDocBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSymfonyMapQueryParameterDescriber.php
More file actions
167 lines (134 loc) · 5.66 KB
/
SymfonyMapQueryParameterDescriber.php
File metadata and controls
167 lines (134 loc) · 5.66 KB
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
<?php
declare(strict_types=1);
/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\RouteDescriber\RouteArgumentDescriber;
use Nelmio\ApiDocBundle\OpenApiPhp\Util;
use OpenApi\Annotations as OA;
use OpenApi\Generator;
use Symfony\Component\HttpKernel\Attribute\MapQueryParameter;
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
final class SymfonyMapQueryParameterDescriber implements RouteArgumentDescriberInterface
{
public function describe(ArgumentMetadata $argumentMetadata, OA\Operation $operation): void
{
if (!$attribute = $argumentMetadata->getAttributes(MapQueryParameter::class, ArgumentMetadata::IS_INSTANCEOF)[0] ?? null) {
return;
}
$name = $attribute->name ?? $argumentMetadata->getName();
$name = 'array' === $argumentMetadata->getType()
? $name.'[]'
: $name;
$operationParameter = Util::getOperationParameter($operation, $name, 'query');
Util::modifyAnnotationValue($operationParameter, 'required', !($argumentMetadata->hasDefaultValue() || $argumentMetadata->isNullable()));
/** @var OA\Schema $schema */
$schema = Util::getChild($operationParameter, OA\Schema::class);
if ($argumentMetadata->hasDefaultValue()) {
Util::modifyAnnotationValue($schema, 'default', $argumentMetadata->getDefaultValue());
}
if (Generator::UNDEFINED === $schema->nullable && $argumentMetadata->isNullable()) {
Util::modifyAnnotationValue($schema, 'nullable', true);
}
$defaultFilter = match ($argumentMetadata->getType()) {
'array' => null,
'string' => \FILTER_DEFAULT,
'int' => \FILTER_VALIDATE_INT,
'float' => \FILTER_VALIDATE_FLOAT,
'bool' => \FILTER_VALIDATE_BOOL,
default => null,
};
$properties = $this->describeValidateFilter($attribute->filter ?? $defaultFilter, $attribute->flags, $attribute->options);
if ('array' === $argumentMetadata->getType()) {
$schema->type = 'array';
Util::getChild($schema, OA\Items::class, $properties);
} else {
foreach ($properties as $key => $value) {
Util::modifyAnnotationValue($schema, $key, $value);
}
}
}
/**
* @param mixed[] $options
*
* @return array<string, mixed>
*
* @see https://www.php.net/manual/en/filter.filters.validate.php
*/
private function describeValidateFilter(?int $filter, int $flags, array $options): array
{
if (null === $filter) {
return [];
}
if (FILTER_VALIDATE_BOOLEAN === $filter) {
return ['type' => 'boolean'];
}
if (FILTER_VALIDATE_DOMAIN === $filter) {
return ['type' => 'string', 'format' => 'hostname'];
}
if (FILTER_VALIDATE_EMAIL === $filter) {
return ['type' => 'string', 'format' => 'email'];
}
if (FILTER_VALIDATE_FLOAT === $filter) {
return ['type' => 'number', 'format' => 'float'];
}
if (FILTER_VALIDATE_INT === $filter) {
$props = [];
if (array_key_exists('min_range', $options)) {
$props['minimum'] = $options['min_range'];
}
if (array_key_exists('max_range', $options)) {
$props['maximum'] = $options['max_range'];
}
return ['type' => 'integer', ...$props];
}
if (FILTER_VALIDATE_IP === $filter) {
$format = match ($flags) {
FILTER_FLAG_IPV4 => 'ipv4',
FILTER_FLAG_IPV6 => 'ipv6',
default => 'ip',
};
return ['type' => 'string', 'format' => $format];
}
if (FILTER_VALIDATE_MAC === $filter) {
return ['type' => 'string', 'format' => 'mac'];
}
if (FILTER_VALIDATE_REGEXP === $filter) {
return ['type' => 'string', 'pattern' => $this->getEcmaRegexpFromPCRE($options['regexp'])];
}
if (FILTER_VALIDATE_URL === $filter) {
return ['type' => 'string', 'format' => 'uri'];
}
if (FILTER_DEFAULT === $filter) {
return ['type' => 'string'];
}
return [];
}
private function getEcmaRegexpFromPCRE(string $pcreRegex): string
{
// Check if PCRE regex has delimiters
if (preg_match('/^(.)(.*)\1([a-zA-Z]*)$/s', $pcreRegex, $matches)) {
[$fullMatch, $delimiter, $pattern, $flags] = $matches;
// Remove escaped delimiters in the pattern
$pattern = str_replace('\\'.$delimiter, $delimiter, $pattern);
} else {
// No delimiter has been found, let's consider it's a valid regex without delimiter. Happens when regexp has been takend from "requirements" in route.
$pattern = $pcreRegex;
}
$pattern = str_replace(['\A', '\z'], ['^', '$'], $pattern); // Supported features but different syntax
// Check for unsupported PCRE specific constructs
$unsupportedFeatures = [
'\Z', // End of string before newline (not supported in JavaScript)
'\R', // Any Unicode newline sequence (not supported in JavaScript)
'\K', // Resets the start of the current match (not supported in JavaScript)
];
$pattern = str_replace($unsupportedFeatures, '', $pattern);
// Return only the pattern (without flags or delimiters)
return $pattern;
}
}