forked from zircote/swagger-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSchemaQueryParameter.php
More file actions
82 lines (72 loc) · 2.4 KB
/
SchemaQueryParameter.php
File metadata and controls
82 lines (72 loc) · 2.4 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
<?php
namespace SchemaQueryParameterProcessor;
use OpenApi\Analysis;
use OpenApi\Annotations\Components;
use OpenApi\Annotations\Operation;
use OpenApi\Annotations\Parameter;
use OpenApi\Annotations\Schema;
use OpenApi\Generator;
/**
* Custom processor to translate the vendor tag `query-args-$ref` into query parameter annotations.
*
* Details for the parameters are taken from the referenced schema.
*/
class SchemaQueryParameter
{
public const X_QUERY_AGS_REF = 'query-args-$ref';
public function __invoke(Analysis $analysis)
{
/** @var Schema[] $schemas */
$schemas = $analysis->getAnnotationsOfType(Schema::class, true);
/** @var Operation[] $operations */
$operations = $analysis->getAnnotationsOfType(Operation::class);
foreach ($operations as $operation) {
if ($operation->x !== Generator::UNDEFINED && array_key_exists(self::X_QUERY_AGS_REF, $operation->x)) {
if ($schema = $this->schemaForRef($schemas, $operation->x[self::X_QUERY_AGS_REF])) {
$this->expandQueryArgs($operation, $schema);
$this->cleanUp($operation);
}
}
}
}
/**
* Find schema for the given ref.
*/
protected function schemaForRef(array $schemas, string $ref)
{
foreach ($schemas as $schema) {
if (Components::ref($schema) === $ref) {
return $schema;
}
}
return null;
}
/**
* Expand the given operation by injecting parameters for all properties of the given schema.
*/
protected function expandQueryArgs(Operation $operation, Schema $schema)
{
if ($schema->properties == Generator::UNDEFINED || !$schema->properties) {
return;
}
$operation->parameters = $operation->parameters == Generator::UNDEFINED ? [] : $operation->parameters;
foreach ($schema->properties as $property) {
$parameter = new Parameter([
'name' => $property->property,
'in' => 'query',
'required' => false,
]);
$operation->parameters[] = $parameter;
}
}
/**
* Clean up.
*/
protected function cleanUp($operation)
{
unset($operation->x[self::X_QUERY_AGS_REF]);
if (!$operation->x) {
$operation->x = Generator::UNDEFINED;
}
}
}