1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Apie\OpenapiSchema\Factories; |
5
|
|
|
|
6
|
|
|
use Apie\OpenapiSchema\Contract\SchemaContract; |
7
|
|
|
use Apie\OpenapiSchema\Spec\Schema; |
8
|
|
|
use ReflectionClass; |
9
|
|
|
|
10
|
|
|
class SchemaFactory |
11
|
|
|
{ |
12
|
|
|
public static function createObjectSchemaWithoutProperties( |
13
|
|
|
ReflectionClass $class, |
14
|
|
|
string $operation = 'get', |
15
|
|
|
array $groups = [] |
16
|
|
|
): SchemaContract { |
17
|
|
|
$description = $class->getShortName() . ' ' . $operation; |
18
|
|
|
if ($groups) { |
|
|
|
|
19
|
|
|
$description .= ' for groups ' . implode(', ', $groups); |
20
|
|
|
} |
21
|
|
|
return Schema::fromNative([ |
22
|
|
|
'type' => 'object', |
23
|
|
|
'properties' => [], |
24
|
|
|
'title' => $class->getShortName(), |
25
|
|
|
'description' => $description, |
26
|
|
|
]); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public static function createAnyTypeSchema(): Schema |
30
|
|
|
{ |
31
|
|
|
return Schema::fromNative([]); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public static function createStringSchema(?string $format = null, ?string $defaultValue = null, bool $nullable = false): Schema |
35
|
|
|
{ |
36
|
|
|
$data = ['type' => 'string']; |
37
|
|
|
if ($format !== null) { |
38
|
|
|
$data['format'] = $format; |
39
|
|
|
} |
40
|
|
|
if ($nullable) { |
41
|
|
|
$data['nullable'] = $nullable; |
42
|
|
|
} |
43
|
|
|
if ($defaultValue !== null) { |
44
|
|
|
$data['example'] = $defaultValue; |
45
|
|
|
$data['default'] = $defaultValue; |
46
|
|
|
} |
47
|
|
|
return Schema::fromNative($data); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public static function createBooleanSchema(): Schema |
51
|
|
|
{ |
52
|
|
|
return Schema::fromNative(['type' => 'boolean']); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public static function createNumberSchema(?string $format = null): Schema |
56
|
|
|
{ |
57
|
|
|
return Schema::fromNative(['type' => 'number', 'format' => $format]); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public static function createFloatSchema(): Schema |
61
|
|
|
{ |
62
|
|
|
return self::createNumberSchema('double'); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
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.