-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathParametersBuilder.php
More file actions
72 lines (59 loc) · 2.86 KB
/
Copy pathParametersBuilder.php
File metadata and controls
72 lines (59 loc) · 2.86 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
<?php
namespace Vyuldashev\LaravelOpenApi\Builders\Paths\Operation;
use GoldSpecDigital\ObjectOrientedOAS\Objects\Parameter;
use GoldSpecDigital\ObjectOrientedOAS\Objects\Schema;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use phpDocumentor\Reflection\DocBlock\Tags\Param;
use ReflectionParameter;
use Vyuldashev\LaravelOpenApi\Attributes\Parameters;
use Vyuldashev\LaravelOpenApi\Contracts\ParametersFactoryInterface;
use Vyuldashev\LaravelOpenApi\RouteInformation;
use Vyuldashev\LaravelOpenApi\SchemaHelpers;
class ParametersBuilder
{
public function build(RouteInformation $route): array
{
$pathParameters = $this->buildPath($route);
$attributedParameters = $this->buildAttribute($route);
return $pathParameters->merge($attributedParameters)->toArray();
}
protected function buildPath(RouteInformation $route): Collection
{
return collect($route->parameters)
->map(static function (array $parameter) use ($route) {
$schema = Schema::string();
/** @var ReflectionParameter|null $reflectionParameter */
$reflectionParameter = collect($route->actionParameters)
->first(static fn (ReflectionParameter $reflectionParameter) => $reflectionParameter->name === $parameter['name']);
if ($reflectionParameter) {
// The reflected param has no type, so ignore (should be defined in a ParametersFactory instead)
if ($reflectionParameter->getType() === null) {
return null;
}
$schema = SchemaHelpers::guessFromReflectionType($reflectionParameter->getType());
}
/** @var Param $description */
$description = collect($route->actionDocBlock->getTagsByName('param'))
->first(static fn (Param $param) => Str::snake($param->getVariableName()) === Str::snake($parameter['name']));
return Parameter::path()->name($parameter['name'])
->required()
->description(optional(optional($description)->getDescription())->render())
->schema($schema);
})
->filter();
}
protected function buildAttribute(RouteInformation $route): Collection
{
/** @var Parameters|null $parameters */
$parameters = $route->actionAttributes->first(static fn ($attribute) => $attribute instanceof Parameters, []);
if ($parameters) {
/** @var ParametersFactoryInterface $parametersFactory */
$parametersFactory = app($parameters->factory);
// little bit magic, add custom data into factory
$parametersFactory->data = $parameters->data;
$parameters = $parametersFactory->build();
}
return collect($parameters);
}
}