-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathAttributeGenerator.php
More file actions
82 lines (71 loc) · 3.11 KB
/
Copy pathAttributeGenerator.php
File metadata and controls
82 lines (71 loc) · 3.11 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
declare(strict_types=1);
namespace OnMoon\OpenApiServerBundle\CodeGenerator;
use OnMoon\OpenApiServerBundle\CodeGenerator\Definitions\DtoDefinition;
use OnMoon\OpenApiServerBundle\CodeGenerator\Definitions\DtoReference;
use OnMoon\OpenApiServerBundle\CodeGenerator\Definitions\GraphDefinition;
use OnMoon\OpenApiServerBundle\CodeGenerator\Definitions\PropertyDefinition;
use OnMoon\OpenApiServerBundle\Specification\Definitions\Property;
/** @psalm-suppress ClassMustBeFinal */
class AttributeGenerator
{
public function setAllAttributes(GraphDefinition $graph): void
{
foreach ($graph->getSpecifications() as $specificationDefinition) {
foreach ($specificationDefinition->getComponents() as $component) {
$this->componentsPass($component->getDto());
}
foreach ($specificationDefinition->getOperations() as $operation) {
$this->requestPass($operation->getRequest());
foreach ($operation->getResponses() as $response) {
$this->responsePass($response->getResponseBody());
}
}
}
}
public function componentsPass(?DtoReference $root): void
{
$this->treeWalk($root, static function (Property $specProperty, PropertyDefinition $property): void {
$needValue = $specProperty->isRequired() && $specProperty->getDefaultValue() === null;
$property
->setHasGetter(true)
->setHasSetter(! $needValue)
->setNullable(! $needValue || $specProperty->isNullable())
->setInConstructor($needValue);
});
}
public function requestPass(?DtoReference $root): void
{
$this->treeWalk($root, static function (Property $specProperty, PropertyDefinition $property): void {
$willExist = $specProperty->isRequired() || $specProperty->getDefaultValue() !== null;
$property
->setHasGetter(true)
->setHasSetter(false)
->setNullable(! $willExist || $specProperty->isNullable())
->setInConstructor(false);
});
}
public function responsePass(?DtoReference $root): void
{
$this->treeWalk($root, static function (Property $specProperty, PropertyDefinition $property): void {
$needValue = $specProperty->isRequired() && $specProperty->getDefaultValue() === null;
$property
->setHasGetter(true)
->setHasSetter(! $needValue)
->setNullable(! $needValue || $specProperty->isNullable())
->setInConstructor($needValue);
});
}
/** @param callable(Property, PropertyDefinition): void $action */
private function treeWalk(?DtoReference $root, callable $action): void
{
if (! $root instanceof DtoDefinition) {
return;
}
foreach ($root->getProperties() as $property) {
$specProperty = $property->getSpecProperty();
$action($specProperty, $property);
$this->treeWalk($property->getObjectTypeDefinition(), $action);
}
}
}