Completed
Push — master ( e0ffea...b38ea1 )
by Francis
02:07
created

AbstractGenerator::getPhpDefaultValue()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 22
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 18
c 0
b 0
f 0
dl 0
loc 22
rs 9.0444
cc 6
nc 6
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Prometee\SwaggerClientGenerator\Base\Generator;
6
7
use Prometee\SwaggerClientGenerator\Base\View\ViewInterface;
8
9
abstract class AbstractGenerator implements GeneratorInterface
10
{
11
    /** @var ViewInterface */
12
    protected $view;
13
14
    /**
15
     * {@inheritDoc}
16
     */
17
    public function getView(): ViewInterface
18
    {
19
        return $this->view;
20
    }
21
22
    /**
23
     * {@inheritDoc}
24
     */
25
    public function setView(ViewInterface $view): void
26
    {
27
        $view->setGenerator($this);
28
        $this->view = $view;
29
    }
30
31
    /**
32
     * {@inheritDoc}
33
     */
34
    public function generate(string $indent = null, string $eol = null): ?string
35
    {
36
        return $this->view->build($indent, $eol);
37
    }
38
39
    public function __clone()
40
    {
41
        $this->setView(clone $this->view);
42
    }
43
44
    /**
45
     * @param string|null $value
46
     *
47
     * @return string|null
48
     */
49
    public static function getValueType(?string $value): ?string
50
    {
51
        if (null === $value) {
52
            return null;
53
        }
54
55
        if ($value === '[]') {
56
            return 'array';
57
        }
58
59
        if (preg_match('#^[\'"].*[\'"]$#', $value)) {
60
            return 'string';
61
        }
62
63
        return self::getValueNumericType($value);
64
    }
65
66
    /**
67
     * @param string $value
68
     *
69
     * @return string|null
70
     */
71
    public static function getValueNumericType(string $value): ?string
72
    {
73
        if (in_array($value, ['true', 'false'])) {
74
            return 'bool';
75
        }
76
77
        if (preg_match('#^[0-9]+$#', $value)) {
78
            return 'int';
79
        }
80
81
        if (preg_match('#^[0-9\.]+$#', $value)) {
82
            return 'float';
83
        }
84
85
        return null;
86
    }
87
88
    /**
89
     * @param string[] $types
90
     *
91
     * @return string|null
92
     */
93
    public static function getPhpType(array $types): ?string
94
    {
95
        if (empty($types)) {
96
            return null;
97
        }
98
99
        $phpType = '';
100
        if (in_array('null', $types)) {
101
            $phpType = '?';
102
        }
103
        foreach ($types as $type) {
104
            if (preg_match('#\[\]$#', $type)) {
105
                $phpType .= 'array';
106
                break;
107
            }
108
            if ($type !== 'null') {
109
                $phpType .= $type;
110
                break;
111
            }
112
        }
113
114
        return $phpType;
115
    }
116
117
    /**
118
     * @param array $types
119
     *
120
     * @return string|null
121
     */
122
    public static function getPhpDefaultValue(array $types): ?string
123
    {
124
        $defaultValue = null;
125
        switch (self::getPhpType($types)) {
126
            case 'array':
127
                $defaultValue = '[]';
128
                break;
129
            case 'string':
130
                $defaultValue = '\'\'';
131
                break;
132
            case 'bool':
133
                $defaultValue = 'true';
134
                break;
135
            case 'int':
136
                $defaultValue = '0';
137
                break;
138
            case 'float':
139
                $defaultValue = '.0';
140
                break;
141
        }
142
143
        return $defaultValue;
144
    }
145
}