1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace W2w\Lib\Apie\OpenApiSchema\Factories; |
4
|
|
|
|
5
|
|
|
use erasys\OpenApi\Spec\v3\Schema; |
6
|
|
|
use ReflectionClass; |
7
|
|
|
|
8
|
|
|
final class SchemaFactory |
9
|
|
|
{ |
10
|
|
|
public static function createObjectSchemaWithoutProperties( |
11
|
|
|
ReflectionClass $class, |
12
|
|
|
string $operation = 'get', |
13
|
|
|
array $groups = [] |
14
|
|
|
): Schema { |
15
|
|
|
$description = $class->getShortName() . ' ' . $operation; |
16
|
|
|
if ($groups) { |
|
|
|
|
17
|
|
|
$description .= ' for groups ' . implode(', ', $groups); |
18
|
|
|
} |
19
|
|
|
return new Schema([ |
20
|
|
|
'type' => 'object', |
21
|
|
|
'properties' => [], |
22
|
|
|
'title' => $class->getShortName(), |
23
|
|
|
'description' => $description, |
24
|
|
|
]); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public static function createAnyTypeSchema(): Schema |
28
|
|
|
{ |
29
|
|
|
return new Schema([]); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public static function createStringSchema(?string $format = null, ?string $defaultValue = null, bool $nullable = false): Schema |
33
|
|
|
{ |
34
|
|
|
$data = ['type' => 'string']; |
35
|
|
|
if ($format !== null) { |
36
|
|
|
$data['format'] = $format; |
37
|
|
|
} |
38
|
|
|
if ($nullable) { |
39
|
|
|
$data['nullable'] = $nullable; |
40
|
|
|
} |
41
|
|
|
if ($defaultValue !== null) { |
42
|
|
|
$data['example'] = $defaultValue; |
43
|
|
|
$data['default'] = $defaultValue; |
44
|
|
|
} |
45
|
|
|
return new Schema($data); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public static function createBooleanSchema(): Schema |
49
|
|
|
{ |
50
|
|
|
return new Schema(['type' => 'boolean']); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public static function createNumberSchema(?string $format = null): Schema |
54
|
|
|
{ |
55
|
|
|
return new Schema(['type' => 'number', 'format' => $format]); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public static function createFloatSchema(): Schema |
59
|
|
|
{ |
60
|
|
|
return self::createNumberSchema('double'); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.