1
|
|
|
<?php |
2
|
|
|
namespace GoetasWebservices\Xsd\XsdToPhp; |
3
|
|
|
|
4
|
|
|
use GoetasWebservices\XML\XSDReader\Schema\Element\ElementSingle; |
5
|
|
|
use GoetasWebservices\XML\XSDReader\Schema\Schema; |
6
|
|
|
use GoetasWebservices\XML\XSDReader\Schema\Type\ComplexType; |
7
|
|
|
use GoetasWebservices\XML\XSDReader\Schema\Type\SimpleType; |
8
|
|
|
use GoetasWebservices\XML\XSDReader\Schema\Type\Type; |
9
|
|
|
use GoetasWebservices\Xsd\XsdToPhp\Naming\NamingStrategy; |
10
|
|
|
use Psr\Log\LoggerAwareTrait; |
11
|
|
|
use Psr\Log\LoggerInterface; |
12
|
|
|
use Psr\Log\NullLogger; |
13
|
|
|
|
14
|
|
|
abstract class AbstractConverter |
15
|
|
|
{ |
16
|
|
|
use LoggerAwareTrait; |
17
|
|
|
|
18
|
|
|
protected $baseSchemas = array( |
19
|
|
|
'http://www.w3.org/2001/XMLSchema', |
20
|
|
|
'http://www.w3.org/XML/1998/namespace' |
21
|
|
|
); |
22
|
|
|
|
23
|
|
|
protected $namespaces = array( |
24
|
|
|
'http://www.w3.org/2001/XMLSchema' => '', |
25
|
|
|
'http://www.w3.org/XML/1998/namespace' => '' |
26
|
|
|
); |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var \GoetasWebservices\Xsd\XsdToPhp\Naming\NamingStrategy |
30
|
|
|
*/ |
31
|
|
|
private $namingStrategy; |
32
|
|
|
|
33
|
|
|
public abstract function convert(array $schemas); |
|
|
|
|
34
|
|
|
|
35
|
|
|
protected $typeAliases = array(); |
36
|
|
|
|
37
|
|
|
protected $aliasCache = array(); |
38
|
|
|
|
39
|
53 |
|
public function addAliasMap($ns, $name, callable $handler) |
40
|
|
|
{ |
41
|
53 |
|
$this->logger->info("Added map $ns $name"); |
42
|
53 |
|
$this->typeAliases[$ns][$name] = $handler; |
43
|
53 |
|
} |
44
|
|
|
|
45
|
2 |
|
public function addAliasMapType($ns, $name, $type) |
46
|
|
|
{ |
47
|
|
|
$this->addAliasMap($ns, $name, function () use ($type) { |
48
|
2 |
|
return $type; |
49
|
2 |
|
}); |
50
|
2 |
|
} |
51
|
|
|
|
52
|
48 |
|
public function getTypeAlias($type, Schema $schemapos = null) |
53
|
|
|
{ |
54
|
48 |
|
$schema = $schemapos ?: $type->getSchema(); |
55
|
|
|
|
56
|
48 |
|
$cid = $schema->getTargetNamespace() . "|" . $type->getName(); |
57
|
48 |
|
if (isset($this->aliasCache[$cid])) { |
58
|
10 |
|
return $this->aliasCache[$cid]; |
59
|
|
|
} |
60
|
48 |
|
if (isset($this->typeAliases[$schema->getTargetNamespace()][$type->getName()])) { |
61
|
48 |
|
return $this->aliasCache[$cid] = call_user_func($this->typeAliases[$schema->getTargetNamespace()][$type->getName()], $type); |
62
|
|
|
} |
63
|
27 |
|
} |
64
|
|
|
|
65
|
53 |
|
public function __construct(NamingStrategy $namingStrategy, LoggerInterface $logger = null) |
66
|
|
|
{ |
67
|
53 |
|
$this->namingStrategy = $namingStrategy; |
68
|
53 |
|
$this->logger = $logger ?: new NullLogger(); |
69
|
|
|
|
70
|
|
|
$this->addAliasMap("http://www.w3.org/2001/XMLSchema", "gYearMonth", function (Type $type) { |
|
|
|
|
71
|
|
|
return "integer"; |
72
|
53 |
|
}); |
73
|
|
|
$this->addAliasMap("http://www.w3.org/2001/XMLSchema", "gMonthDay", function (Type $type) { |
|
|
|
|
74
|
|
|
return "integer"; |
75
|
53 |
|
}); |
76
|
|
|
$this->addAliasMap("http://www.w3.org/2001/XMLSchema", "gMonth", function (Type $type) { |
|
|
|
|
77
|
|
|
return "integer"; |
78
|
53 |
|
}); |
79
|
|
|
$this->addAliasMap("http://www.w3.org/2001/XMLSchema", "gYear", function (Type $type) { |
|
|
|
|
80
|
|
|
return "integer"; |
81
|
53 |
|
}); |
82
|
|
|
$this->addAliasMap("http://www.w3.org/2001/XMLSchema", "NMTOKEN", function (Type $type) { |
|
|
|
|
83
|
|
|
return "string"; |
84
|
53 |
|
}); |
85
|
|
|
$this->addAliasMap("http://www.w3.org/2001/XMLSchema", "NMTOKENS", function (Type $type) { |
|
|
|
|
86
|
|
|
return "string"; |
87
|
53 |
|
}); |
88
|
|
|
$this->addAliasMap("http://www.w3.org/2001/XMLSchema", "QName", function (Type $type) { |
|
|
|
|
89
|
|
|
return "string"; |
90
|
53 |
|
}); |
91
|
|
|
$this->addAliasMap("http://www.w3.org/2001/XMLSchema", "NCName", function (Type $type) { |
|
|
|
|
92
|
|
|
return "string"; |
93
|
53 |
|
}); |
94
|
|
|
$this->addAliasMap("http://www.w3.org/2001/XMLSchema", "decimal", function (Type $type) { |
|
|
|
|
95
|
5 |
|
return "float"; |
96
|
53 |
|
}); |
97
|
|
|
$this->addAliasMap("http://www.w3.org/2001/XMLSchema", "float", function (Type $type) { |
|
|
|
|
98
|
|
|
return "float"; |
99
|
53 |
|
}); |
100
|
|
|
$this->addAliasMap("http://www.w3.org/2001/XMLSchema", "double", function (Type $type) { |
|
|
|
|
101
|
|
|
return "float"; |
102
|
53 |
|
}); |
103
|
|
|
$this->addAliasMap("http://www.w3.org/2001/XMLSchema", "string", function (Type $type) { |
|
|
|
|
104
|
23 |
|
return "string"; |
105
|
53 |
|
}); |
106
|
|
|
$this->addAliasMap("http://www.w3.org/2001/XMLSchema", "normalizedString", function (Type $type) { |
|
|
|
|
107
|
|
|
return "string"; |
108
|
53 |
|
}); |
109
|
|
|
$this->addAliasMap("http://www.w3.org/2001/XMLSchema", "integer", function (Type $type) { |
|
|
|
|
110
|
5 |
|
return "integer"; |
111
|
53 |
|
}); |
112
|
|
|
$this->addAliasMap("http://www.w3.org/2001/XMLSchema", "int", function (Type $type) { |
|
|
|
|
113
|
5 |
|
return "integer"; |
114
|
53 |
|
}); |
115
|
|
|
$this->addAliasMap("http://www.w3.org/2001/XMLSchema", "unsignedInt", function (Type $type) { |
|
|
|
|
116
|
|
|
return "integer"; |
117
|
53 |
|
}); |
118
|
|
|
$this->addAliasMap("http://www.w3.org/2001/XMLSchema", "negativeInteger", function (Type $type) { |
|
|
|
|
119
|
|
|
return "integer"; |
120
|
53 |
|
}); |
121
|
|
|
$this->addAliasMap("http://www.w3.org/2001/XMLSchema", "positiveInteger", function (Type $type) { |
|
|
|
|
122
|
|
|
return "integer"; |
123
|
53 |
|
}); |
124
|
|
|
$this->addAliasMap("http://www.w3.org/2001/XMLSchema", "nonNegativeInteger", function (Type $type) { |
|
|
|
|
125
|
|
|
return "integer"; |
126
|
53 |
|
}); |
127
|
|
|
$this->addAliasMap("http://www.w3.org/2001/XMLSchema", "nonPositiveInteger", function (Type $type) { |
|
|
|
|
128
|
|
|
return "integer"; |
129
|
53 |
|
}); |
130
|
|
|
$this->addAliasMap("http://www.w3.org/2001/XMLSchema", "long", function (Type $type) { |
|
|
|
|
131
|
4 |
|
return "integer"; |
132
|
53 |
|
}); |
133
|
|
|
$this->addAliasMap("http://www.w3.org/2001/XMLSchema", "unsignedLong", function (Type $type) { |
|
|
|
|
134
|
|
|
return "integer"; |
135
|
53 |
|
}); |
136
|
|
|
$this->addAliasMap("http://www.w3.org/2001/XMLSchema", "short", function (Type $type) { |
|
|
|
|
137
|
|
|
return "integer"; |
138
|
53 |
|
}); |
139
|
|
|
$this->addAliasMap("http://www.w3.org/2001/XMLSchema", "boolean", function (Type $type) { |
|
|
|
|
140
|
|
|
return "boolean"; |
141
|
53 |
|
}); |
142
|
|
|
$this->addAliasMap("http://www.w3.org/2001/XMLSchema", "nonNegativeInteger", function (Type $type) { |
|
|
|
|
143
|
|
|
return "integer"; |
144
|
53 |
|
}); |
145
|
|
|
$this->addAliasMap("http://www.w3.org/2001/XMLSchema", "positiveInteger", function (Type $type) { |
|
|
|
|
146
|
|
|
return "integer"; |
147
|
53 |
|
}); |
148
|
|
|
$this->addAliasMap("http://www.w3.org/2001/XMLSchema", "language", function (Type $type) { |
|
|
|
|
149
|
1 |
|
return "string"; |
150
|
53 |
|
}); |
151
|
|
|
$this->addAliasMap("http://www.w3.org/2001/XMLSchema", "token", function (Type $type) { |
|
|
|
|
152
|
|
|
return "string"; |
153
|
53 |
|
}); |
154
|
|
|
$this->addAliasMap("http://www.w3.org/2001/XMLSchema", "anyURI", function (Type $type) { |
|
|
|
|
155
|
|
|
return "string"; |
156
|
53 |
|
}); |
157
|
|
|
$this->addAliasMap("http://www.w3.org/2001/XMLSchema", "byte", function (Type $type) { |
|
|
|
|
158
|
|
|
return "string"; |
159
|
53 |
|
}); |
160
|
|
|
$this->addAliasMap("http://www.w3.org/2001/XMLSchema", "duration", function (Type $type) { |
|
|
|
|
161
|
|
|
return "DateInterval"; |
162
|
53 |
|
}); |
163
|
|
|
|
164
|
|
|
$this->addAliasMap("http://www.w3.org/2001/XMLSchema", "ID", function (Type $type) { |
|
|
|
|
165
|
|
|
return "string"; |
166
|
53 |
|
}); |
167
|
|
|
$this->addAliasMap("http://www.w3.org/2001/XMLSchema", "IDREF", function (Type $type) { |
|
|
|
|
168
|
|
|
return "string"; |
169
|
53 |
|
}); |
170
|
|
|
$this->addAliasMap("http://www.w3.org/2001/XMLSchema", "IDREFS", function (Type $type) { |
|
|
|
|
171
|
|
|
return "string"; |
172
|
53 |
|
}); |
173
|
|
|
$this->addAliasMap("http://www.w3.org/2001/XMLSchema", "Name", function (Type $type) { |
|
|
|
|
174
|
1 |
|
return "string"; |
175
|
53 |
|
}); |
176
|
|
|
|
177
|
53 |
|
$this->addAliasMap("http://www.w3.org/2001/XMLSchema", "NCName", function (Type $type) { |
|
|
|
|
178
|
|
|
return "string"; |
179
|
53 |
|
}); |
180
|
53 |
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @return \GoetasWebservices\Xsd\XsdToPhp\Naming\NamingStrategy |
184
|
|
|
*/ |
185
|
48 |
|
protected function getNamingStrategy() |
186
|
|
|
{ |
187
|
48 |
|
return $this->namingStrategy; |
188
|
|
|
} |
189
|
|
|
|
190
|
51 |
|
public function addNamespace($ns, $phpNamespace) |
191
|
|
|
{ |
192
|
51 |
|
$this->logger->info("Added ns mapping $ns, $phpNamespace"); |
193
|
51 |
|
$this->namespaces[$ns] = $phpNamespace; |
194
|
51 |
|
return $this; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
protected function cleanName($name) |
198
|
|
|
{ |
199
|
|
|
return preg_replace("/<.*>/", "", $name); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param Type $type |
204
|
|
|
* @return \GoetasWebservices\XML\XSDReader\Schema\Type\Type|null |
205
|
|
|
*/ |
206
|
28 |
|
protected function isArrayType(Type $type) |
207
|
|
|
{ |
208
|
28 |
|
if ($type instanceof SimpleType) { |
209
|
27 |
|
return $type->getList(); |
210
|
|
|
} |
211
|
27 |
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @param Type $type |
215
|
|
|
* @return \GoetasWebservices\XML\XSDReader\Schema\Element\ElementSingle|null |
216
|
|
|
*/ |
217
|
28 |
|
protected function isArrayNestedElement(Type $type) |
218
|
|
|
{ |
219
|
28 |
|
if ($type instanceof ComplexType && !$type->getParent() && !$type->getAttributes() && count($type->getElements()) === 1) { |
220
|
20 |
|
$elements = $type->getElements(); |
221
|
20 |
|
return $this->isArrayElement(reset($elements)); |
222
|
|
|
} |
223
|
27 |
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @param mixed $element |
227
|
|
|
* @return \GoetasWebservices\XML\XSDReader\Schema\Element\ElementSingle|null |
228
|
|
|
*/ |
229
|
26 |
|
protected function isArrayElement($element) |
230
|
|
|
{ |
231
|
26 |
|
if ($element instanceof ElementSingle && ($element->getMax() > 1 || $element->getMax() === -1)) { |
232
|
9 |
|
return $element; |
233
|
|
|
} |
234
|
24 |
|
} |
235
|
|
|
|
236
|
|
|
public function getNamespaces() |
237
|
|
|
{ |
238
|
|
|
return $this->namespaces; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
} |
242
|
|
|
|