Completed
Branch v4 (4e54dd)
by Pieter
03:26
created

SchemaFactory::createStringSchema()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
nc 8
nop 3
dl 0
loc 14
rs 9.9666
c 1
b 0
f 0
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) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $groups of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

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.

Loading history...
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