1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Prometee\SwaggerClientGenerator\Swagger\Helper; |
6
|
|
|
|
7
|
|
|
use Exception; |
8
|
|
|
|
9
|
|
|
class SwaggerModelHelper extends AbstractHelper implements SwaggerModelHelperInterface |
10
|
|
|
{ |
11
|
|
|
public static function isNullableBySwaggerConfiguration(string $targetedProperty, array $definition): bool |
12
|
|
|
{ |
13
|
|
|
if (!isset($definition['properties'])) { |
14
|
|
|
return true; |
15
|
|
|
} |
16
|
|
|
if (isset($definition['required']) && in_array($targetedProperty, $definition['required'])) { |
17
|
|
|
return false; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
if (!isset($definition['properties'][$targetedProperty])) { |
21
|
|
|
return true; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
return $definition['properties'][$targetedProperty]['nullable'] |
25
|
|
|
?? true |
26
|
|
|
; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* {@inheritDoc} |
31
|
|
|
*/ |
32
|
|
|
public static function getArrayEmbeddedObjectConfig(array $config): ?array |
33
|
|
|
{ |
34
|
|
|
if (!isset($config['items'])) { |
35
|
|
|
return null; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
if (!isset($config['items']['type'])) { |
39
|
|
|
return null; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if ('object' !== $config['items']['type']) { |
43
|
|
|
return null; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return $config['items']; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritDoc} |
52
|
|
|
* |
53
|
|
|
* @throws Exception |
54
|
|
|
*/ |
55
|
|
|
public static function flattenDefinitionType(string $definitionType, array $definitions, string $definitionName): array |
56
|
|
|
{ |
57
|
|
|
if (!isset($definitions[$definitionName])) { |
58
|
|
|
throw new Exception(sprintf('Unable to found definition name "%s" !', $definitionName)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$definition = $definitions[$definitionName]; |
62
|
|
|
|
63
|
|
|
if (isset($definition[$definitionType])) { |
64
|
|
|
return $definition[$definitionType]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if (!isset($definition['allOf'])) { |
68
|
|
|
return []; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$allOf = $definition['allOf']; |
72
|
|
|
$inheritedPropertyName = self::getPhpTypeFromSwaggerDefinitionName($allOf[0]['$ref']); |
73
|
|
|
|
74
|
|
|
$properties = []; |
75
|
|
|
if (isset($allOf[1][$definitionType])) { |
76
|
|
|
$properties = $allOf[1][$definitionType]; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$inheritedTypes = self::flattenDefinitionType($definitionType, $definitions, $inheritedPropertyName); |
80
|
|
|
|
81
|
|
|
return array_merge($properties, $inheritedTypes); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritDoc} |
86
|
|
|
*/ |
87
|
|
|
public static function foundNotInheritedProperties(array $definition): array |
88
|
|
|
{ |
89
|
|
|
if (isset($definition['properties'])) { |
90
|
|
|
return $definition['properties']; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if (!isset($definition['allOf'])) { |
94
|
|
|
return []; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$allOf = $definition['allOf']; |
98
|
|
|
if (!isset($allOf[1]['properties'])) { |
99
|
|
|
return []; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $allOf[1]['properties']; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|