1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Swaggest\PhpCodeBuilder\JsonSchema; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Swaggest\JsonSchema\Constraint\Type; |
7
|
|
|
use Swaggest\JsonSchema\Schema; |
8
|
|
|
use Swaggest\PhpCodeBuilder\PhpAnyType; |
9
|
|
|
use Swaggest\PhpCodeBuilder\PhpStdType; |
10
|
|
|
use Swaggest\PhpCodeBuilder\Types\ArrayOf; |
11
|
|
|
use Swaggest\PhpCodeBuilder\Types\OrType; |
12
|
|
|
|
13
|
|
|
class TypeBuilder |
14
|
|
|
{ |
15
|
|
|
/** @var Schema */ |
16
|
|
|
private $schema; |
17
|
|
|
/** @var string */ |
18
|
|
|
private $path; |
19
|
|
|
/** @var PhpBuilder */ |
20
|
|
|
private $phpBuilder; |
21
|
|
|
/** @var OrType */ |
22
|
|
|
private $result; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* TypeBuilder constructor. |
26
|
|
|
* @param Schema $schema |
27
|
|
|
* @param string $path |
28
|
|
|
* @param PhpBuilder $phpBuilder |
29
|
|
|
*/ |
30
|
5 |
|
public function __construct($schema, $path, PhpBuilder $phpBuilder) |
31
|
|
|
{ |
32
|
5 |
|
$this->schema = $schema; |
33
|
5 |
|
$this->path = $path; |
34
|
5 |
|
$this->phpBuilder = $phpBuilder; |
35
|
5 |
|
} |
36
|
|
|
|
37
|
5 |
|
private function processLogicType() |
38
|
|
|
{ |
39
|
5 |
|
$orSchemas = null; |
40
|
5 |
|
if ($this->schema->allOf !== null) { |
41
|
2 |
|
$orSchemas = $this->schema->allOf; |
42
|
5 |
|
} elseif ($this->schema->anyOf !== null) { |
43
|
3 |
|
$orSchemas = $this->schema->anyOf; |
44
|
5 |
|
} elseif ($this->schema->oneOf !== null) { |
45
|
2 |
|
$orSchemas = $this->schema->oneOf; |
46
|
|
|
} |
47
|
|
|
|
48
|
5 |
|
if ($orSchemas !== null) { |
49
|
3 |
|
foreach ($orSchemas as $item) { |
50
|
3 |
|
$this->result->add($this->phpBuilder->getType($item, $this->path)); |
51
|
|
|
} |
52
|
|
|
} |
53
|
5 |
|
} |
54
|
|
|
|
55
|
5 |
|
private function processArrayType() |
56
|
|
|
{ |
57
|
5 |
|
$schema = $this->schema; |
58
|
|
|
|
59
|
5 |
|
$pathItems = (string)Schema::names()->items; |
60
|
5 |
|
if ($this->isSchema($schema->items)) { |
61
|
2 |
|
$items = array(); |
62
|
2 |
|
$additionalItems = $schema->items; |
63
|
5 |
|
} elseif ($schema->items === null) { // items defaults to empty schema so everything is valid |
64
|
5 |
|
$items = array(); |
65
|
5 |
|
$additionalItems = true; |
66
|
|
|
} else { // listed items |
67
|
|
|
$items = $schema->items; |
68
|
|
|
$additionalItems = $schema->additionalItems; |
69
|
|
|
$pathItems = (string)Schema::names()->additionalItems; |
70
|
|
|
} |
71
|
|
|
|
72
|
5 |
|
if ($items !== null || $additionalItems !== null) { |
73
|
5 |
|
$itemsLen = is_array($items) ? count($items) : 0; |
74
|
5 |
|
$index = 0; |
75
|
5 |
|
if ($index < $itemsLen) { |
|
|
|
|
76
|
|
|
} else { |
77
|
5 |
|
if ($additionalItems instanceof Schema) { |
78
|
2 |
|
$this->result->add(new ArrayOf($this->phpBuilder->getType($additionalItems, $this->path . '->' . $pathItems))); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
5 |
|
} |
83
|
|
|
|
84
|
5 |
|
private function isSchema($var) { |
85
|
5 |
|
return $var instanceof Schema; |
86
|
|
|
} |
87
|
|
|
|
88
|
5 |
|
private function processObjectType() |
89
|
|
|
{ |
90
|
5 |
|
if ($this->schema->patternProperties !== null) { |
91
|
2 |
|
foreach ($this->schema->patternProperties as $pattern => $schema) { |
92
|
2 |
|
$this->result->add(new ArrayOf($this->phpBuilder->getType($schema, $this->path . '->' . $pattern))); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
5 |
|
if ($this->schema->additionalProperties instanceof Schema) { |
97
|
2 |
|
$this->result->add(new ArrayOf($this->phpBuilder->getType( |
98
|
2 |
|
$this->schema->additionalProperties, |
99
|
2 |
|
$this->path . '->' . (string)Schema::names()->additionalProperties) |
100
|
|
|
)); |
101
|
|
|
} |
102
|
5 |
|
} |
103
|
|
|
|
104
|
5 |
|
private function typeSwitch($type) |
105
|
|
|
{ |
106
|
5 |
|
switch ($type) { |
107
|
|
|
case Type::INTEGER: |
108
|
4 |
|
return PhpStdType::int(); |
109
|
|
|
|
110
|
|
|
case Type::NUMBER: |
111
|
5 |
|
return PhpStdType::float(); |
112
|
|
|
|
113
|
|
|
case TYPE::BOOLEAN: |
114
|
4 |
|
return PhpStdType::bool(); |
115
|
|
|
|
116
|
|
|
case Type::STRING: |
117
|
4 |
|
return PhpStdType::string(); |
118
|
|
|
|
119
|
|
|
/* |
|
|
|
|
120
|
|
|
case Type::OBJECT: |
121
|
|
|
return PhpStdType::object(); |
122
|
|
|
*/ |
123
|
|
|
|
124
|
|
|
case Type::ARR: |
125
|
3 |
|
return PhpStdType::arr(); |
126
|
|
|
|
127
|
|
|
case Type::NULL: |
128
|
1 |
|
return PhpStdType::null(); |
129
|
|
|
|
130
|
|
|
default: |
131
|
4 |
|
return null; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param Schema $schema |
137
|
|
|
* @param string $path |
138
|
|
|
* @throws Exception |
139
|
|
|
* @throws \Swaggest\PhpCodeBuilder\Exception |
140
|
|
|
*/ |
141
|
5 |
|
private function processNamedClass($schema, $path) |
142
|
|
|
{ |
143
|
5 |
|
if ($schema->properties !== null) { |
144
|
4 |
|
$class = $this->phpBuilder->getClass($schema, $path); |
145
|
4 |
|
$this->result->add($class); |
146
|
|
|
} |
147
|
5 |
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return PhpAnyType |
151
|
|
|
* @throws Exception |
152
|
|
|
* @throws \Swaggest\PhpCodeBuilder\Exception |
153
|
|
|
*/ |
154
|
5 |
|
public function build() |
155
|
|
|
{ |
156
|
5 |
|
$this->result = new OrType(); |
157
|
5 |
|
if ($this->schema === null) { |
158
|
|
|
throw new Exception('Null schema'); |
159
|
|
|
} |
160
|
|
|
|
161
|
5 |
|
if ($fromRefs = $this->schema->getFromRefs()) { |
162
|
3 |
|
$this->path = $fromRefs[count($fromRefs) - 1]; |
|
|
|
|
163
|
|
|
//$this->result->add($this->phpBuilder->getType($this->schema->ref->getData(), $this->schema->ref->ref)); |
|
|
|
|
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
|
167
|
5 |
|
$this->processNamedClass($this->schema, $this->path); |
168
|
5 |
|
$this->processLogicType(); |
169
|
5 |
|
$this->processArrayType(); |
170
|
5 |
|
$this->processObjectType(); |
171
|
|
|
|
172
|
5 |
|
if (is_array($this->schema->type)) { |
173
|
|
|
foreach ($this->schema->type as $type) { |
174
|
|
|
$this->result->add($this->typeSwitch($type)); |
175
|
|
|
} |
176
|
5 |
|
} elseif ($this->schema->type) { |
177
|
5 |
|
$this->result->add($this->typeSwitch($this->schema->type)); |
178
|
|
|
} |
179
|
|
|
|
180
|
5 |
|
return $this->result->simplify(); |
181
|
|
|
|
182
|
|
|
} |
183
|
|
|
} |
This check looks for the bodies of
if
statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
if
bodies can be removed. If you have an empty if but statements in theelse
branch, consider inverting the condition.could be turned into
This is much more concise to read.