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
|
|
|
} |