Completed
Push — master ( b38ea1...abff32 )
by Francis
02:25
created

getPhpTypeFromSwaggerConfiguration()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 18
rs 9.6111
c 0
b 0
f 0
cc 5
nc 4
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Prometee\SwaggerClientGenerator\Swagger\Helper;
6
7
use Exception;
8
9
abstract class AbstractHelper implements HelperInterface
10
{
11
    /**
12
     * {@inheritdoc}
13
     *
14
     * @throws Exception
15
     */
16
    public static function getPhpTypeFromSwaggerConfiguration(array $config): ?string
17
    {
18
        if (isset($config['type'])) {
19
            $type = $config['type'];
20
            $items = $config['items'] ?? null;
21
            $format = $config['format'] ?? null;
22
            return self::getPhpTypeFromSwaggerConfigurationType($type, $items, $format);
23
        }
24
25
        if (isset($config['$ref'])) {
26
            return static::getPhpTypeFromSwaggerDefinitionName($config['$ref']);
27
        }
28
29
        if ($config['schema'] && isset($config['schema']['$ref'])) {
30
            return static::getPhpTypeFromSwaggerDefinitionName($config['schema']['$ref']);
31
        }
32
33
        return null;
34
    }
35
36
    /**
37
     * {@inheritDoc}
38
     *
39
     * @throws Exception
40
     */
41
    public static function getPhpTypeFromSwaggerConfigurationType(string $type, ?array $items = null, ?string $format = null): ?string
42
    {
43
        switch ($type) {
44
            case 'boolean':
45
                return 'bool';
46
            case 'integer':
47
                return 'int';
48
            case 'number':
49
                return 'float';
50
            case 'array':
51
                return static::getPhpTypeFromSwaggerArrayType($items) . '[]';
52
            case 'object':
53
                return '\\stdClass';
54
            case 'string':
55
                return static::getPhpTypeFromSwaggerStringType($format);
56
        }
57
58
        return null;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public static function getPhpTypeFromSwaggerStringType(?string $format): string
65
    {
66
        switch ($format) {
67
            case 'date-time':
68
            case 'date':
69
                return '\\DateTimeInterface';
70
        }
71
72
        return 'string';
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     *
78
     * @throws Exception
79
     */
80
    public static function getPhpTypeFromSwaggerArrayType(?array $items): string
81
    {
82
        $phpType = 'array';
83
84
        if ($items !== null) {
0 ignored issues
show
introduced by
The condition $items !== null is always true.
Loading history...
85
            $phpType = static::getPhpTypeFromSwaggerConfiguration($items);
86
        }
87
88
        if (null === $phpType) {
89
            throw new Exception(sprintf('Unable to found the corresponding php type of "%s"', print_r($items)));
90
        }
91
92
        return $phpType;
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public static function getPhpTypeFromSwaggerDefinitionName(string $ref): string
99
    {
100
        return preg_replace('$#/definitions/$', '', $ref);
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106
    public static function getPhpTypeFromSwaggerTypeAndFormat(string $type, ?array $items): string
107
    {
108
        switch ($type) {
109
            case 'boolean':
110
                return 'bool';
111
            case 'integer':
112
                return 'int';
113
            case 'number':
114
                return 'float';
115
            case 'array':
116
                return static::getPhpTypeFromSwaggerDefinitionName($items['$ref']) . '[]';
117
            default:
118
                return $type;
119
        }
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    public static function cleanStr(string $str): string
126
    {
127
        return preg_replace('#[^a-z_0-9/]#i', '_', $str);
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133
    public static function camelize(string $str): string
134
    {
135
        $underscored = preg_replace('#-#', '_', $str);
136
        $underscored = self::cleanStr($underscored);
137
        $underscored = trim($underscored, '_');
138
        $words = explode('_', $underscored);
139
        $words = array_map('ucfirst', $words);
140
141
        return implode('', $words);
142
    }
143
}
144