forked from php-soap/encoding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncoderRegistry.php
More file actions
364 lines (319 loc) · 15 KB
/
EncoderRegistry.php
File metadata and controls
364 lines (319 loc) · 15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
<?php
declare(strict_types=1);
namespace Soap\Encoding;
use Psl\Collection\MutableMap;
use Soap\Encoding\ClassMap\ClassMapCollection;
use Soap\Encoding\Encoder\AnyElementEncoder;
use Soap\Encoding\Encoder\Context;
use Soap\Encoding\Encoder\EncoderDetector;
use Soap\Encoding\Encoder\ObjectEncoder;
use Soap\Encoding\Encoder\SimpleType;
use Soap\Encoding\Encoder\SoapEnc;
use Soap\Encoding\Encoder\XmlEncoder;
use Soap\Encoding\Formatter\QNameFormatter;
use Soap\Engine\Metadata\Model\XsdType;
use Soap\WsdlReader\Metadata\Detector\ApacheMapDetector;
use Soap\WsdlReader\Model\Definitions\EncodingStyle;
use Soap\Xml\Xmlns;
use stdClass;
use Stringable;
use function Psl\Dict\pull;
final class EncoderRegistry
{
/**
* @param MutableMap<string, XmlEncoder<mixed, string>> $simpleTypeMap
* @param MutableMap<string, XmlEncoder<mixed, string>> $complextTypeMap
* @param int $decoderLibXmlOptions - bitmask of LIBXML_* constants https://www.php.net/manual/en/libxml.constants.php
*/
private function __construct(
private MutableMap $simpleTypeMap,
private MutableMap $complextTypeMap,
private int $decoderLibXmlOptions = 0,
) {
}
/**
* @psalm-suppress InvalidArgument - It is not able to infer the underlying encoder generic types.
*/
public static function default(): self
{
$qNameFormatter = new QNameFormatter();
$xsd = Xmlns::xsd()->value();
$xsd1999 = Xmlns::xsd1999()->value();
return new self(
/** @var MutableMap<XmlEncoder<mixed, string>> */
new MutableMap([
// Strings:
$qNameFormatter($xsd, 'string') => new SimpleType\StringTypeEncoder(),
$qNameFormatter($xsd, 'anyURI') => new SimpleType\StringTypeEncoder(),
$qNameFormatter($xsd, 'qname') => new SimpleType\StringTypeEncoder(),
$qNameFormatter($xsd, 'NOTATION') => new SimpleType\StringTypeEncoder(),
$qNameFormatter($xsd, 'normalizedString') => new SimpleType\StringTypeEncoder(),
$qNameFormatter($xsd, 'token') => new SimpleType\StringTypeEncoder(),
$qNameFormatter($xsd, 'language') => new SimpleType\StringTypeEncoder(),
$qNameFormatter($xsd, 'NMTOKEN') => new SimpleType\StringTypeEncoder(),
$qNameFormatter($xsd, 'NMTOKENS') => new SimpleType\StringTypeEncoder(),
$qNameFormatter($xsd, 'Name') => new SimpleType\StringTypeEncoder(),
$qNameFormatter($xsd, 'NCName') => new SimpleType\StringTypeEncoder(),
$qNameFormatter($xsd, 'NCNames') => new SimpleType\StringTypeEncoder(),
$qNameFormatter($xsd, 'ID') => new SimpleType\StringTypeEncoder(),
$qNameFormatter($xsd, 'IDREF') => new SimpleType\StringTypeEncoder(),
$qNameFormatter($xsd, 'IDREFS') => new SimpleType\StringTypeEncoder(),
$qNameFormatter($xsd, 'ENTITY') => new SimpleType\StringTypeEncoder(),
$qNameFormatter($xsd, 'ENTITIES') => new SimpleType\StringTypeEncoder(),
// Dates
$qNameFormatter($xsd, 'date') => SimpleType\DateTypeEncoder::default(),
$qNameFormatter($xsd, 'dateTime') => SimpleType\DateTimeTypeEncoder::default(),
// Currently only encoding other date/time-related types as string.
// It could be possible to use DateTimeInterface or even unix epoch int-s as well in the future.
$qNameFormatter($xsd, 'time') => new SimpleType\StringTypeEncoder(),
$qNameFormatter($xsd, 'gYear') => new SimpleType\StringTypeEncoder(),
$qNameFormatter($xsd, 'gYearMonth') => new SimpleType\StringTypeEncoder(),
$qNameFormatter($xsd, 'gDay') => new SimpleType\StringTypeEncoder(),
$qNameFormatter($xsd, 'gMonthDay') => new SimpleType\StringTypeEncoder(),
$qNameFormatter($xsd, 'gMonth') => new SimpleType\StringTypeEncoder(),
$qNameFormatter($xsd, 'duration') => new SimpleType\StringTypeEncoder(),
// Encoded strings
$qNameFormatter($xsd, 'base64Binary') => new SimpleType\Base64BinaryTypeEncoder(),
$qNameFormatter($xsd, 'hexBinary') => new SimpleType\HexBinaryTypeEncoder(),
// Bools
$qNameFormatter($xsd, 'boolean') => new SimpleType\BoolTypeEncoder(),
// Integers:
$qNameFormatter($xsd, 'int') => new SimpleType\IntTypeEncoder(),
$qNameFormatter($xsd, 'long') => new SimpleType\IntTypeEncoder(),
$qNameFormatter($xsd, 'short') => new SimpleType\IntTypeEncoder(),
$qNameFormatter($xsd, 'byte') => new SimpleType\IntTypeEncoder(),
$qNameFormatter($xsd, 'nonPositiveInteger') => new SimpleType\IntTypeEncoder(),
$qNameFormatter($xsd, 'positiveInteger') => new SimpleType\IntTypeEncoder(),
$qNameFormatter($xsd, 'nonNegativeInteger') => new SimpleType\IntTypeEncoder(),
$qNameFormatter($xsd, 'negativeInteger') => new SimpleType\IntTypeEncoder(),
$qNameFormatter($xsd, 'unsignedLong') => new SimpleType\IntTypeEncoder(),
$qNameFormatter($xsd, 'unsignedByte') => new SimpleType\IntTypeEncoder(),
$qNameFormatter($xsd, 'unsignedShort') => new SimpleType\IntTypeEncoder(),
$qNameFormatter($xsd, 'unsignedInt') => new SimpleType\IntTypeEncoder(),
$qNameFormatter($xsd, 'integer') => new SimpleType\IntTypeEncoder(),
// Floats:
$qNameFormatter($xsd, 'float') => new SimpleType\FloatTypeEncoder(),
$qNameFormatter($xsd, 'double') => new SimpleType\FloatTypeEncoder(),
$qNameFormatter($xsd, 'decimal') => new SimpleType\FloatTypeEncoder(),
// Scalar:
$qNameFormatter($xsd, 'anyType') => new SimpleType\ScalarTypeEncoder(),
$qNameFormatter($xsd, 'anyXML') => new SimpleType\ScalarTypeEncoder(),
$qNameFormatter($xsd, 'anySimpleType') => new SimpleType\ScalarTypeEncoder(),
// XSD 1999 version
// @see https://www.w3.org/1999/XMLSchema-datatypes.xsd
$qNameFormatter($xsd1999, 'string') => new SimpleType\StringTypeEncoder(),
$qNameFormatter($xsd1999, 'boolean') => new SimpleType\BoolTypeEncoder(),
$qNameFormatter($xsd1999, 'float') => new SimpleType\FloatTypeEncoder(),
$qNameFormatter($xsd1999, 'double') => new SimpleType\FloatTypeEncoder(),
$qNameFormatter($xsd1999, 'decimal') => new SimpleType\FloatTypeEncoder(),
$qNameFormatter($xsd1999, 'timeInstant') => new SimpleType\StringTypeEncoder(),
$qNameFormatter($xsd1999, 'timeDuration') => new SimpleType\StringTypeEncoder(),
$qNameFormatter($xsd1999, 'recurringInstant') => new SimpleType\StringTypeEncoder(),
$qNameFormatter($xsd1999, 'binary') => new SimpleType\StringTypeEncoder(),
$qNameFormatter($xsd1999, 'uriReference') => new SimpleType\StringTypeEncoder(),
$qNameFormatter($xsd1999, 'integer') => new SimpleType\IntTypeEncoder(),
$qNameFormatter($xsd1999, 'nonNegativeInteger') => new SimpleType\IntTypeEncoder(),
$qNameFormatter($xsd1999, 'positiveInteger') => new SimpleType\IntTypeEncoder(),
$qNameFormatter($xsd1999, 'nonPositiveInteger') => new SimpleType\IntTypeEncoder(),
$qNameFormatter($xsd1999, 'negativeInteger') => new SimpleType\IntTypeEncoder(),
$qNameFormatter($xsd1999, 'byte') => new SimpleType\IntTypeEncoder(),
$qNameFormatter($xsd1999, 'int') => new SimpleType\IntTypeEncoder(),
$qNameFormatter($xsd1999, 'long') => new SimpleType\IntTypeEncoder(),
$qNameFormatter($xsd1999, 'short') => new SimpleType\IntTypeEncoder(),
$qNameFormatter($xsd1999, 'unsignedByte') => new SimpleType\IntTypeEncoder(),
$qNameFormatter($xsd1999, 'unsignedInt') => new SimpleType\IntTypeEncoder(),
$qNameFormatter($xsd1999, 'unsignedLong') => new SimpleType\IntTypeEncoder(),
$qNameFormatter($xsd1999, 'unsignedShort') => new SimpleType\IntTypeEncoder(),
$qNameFormatter($xsd1999, 'date') => SimpleType\DateTypeEncoder::default(),
$qNameFormatter($xsd1999, 'time') => new SimpleType\StringTypeEncoder(),
]),
/** @var MutableMap<XmlEncoder<mixed, string>> */
new MutableMap([
// SOAP 1.1 ENC
$qNameFormatter(EncodingStyle::SOAP_11->value, 'Array') => new SoapEnc\SoapArrayEncoder(),
$qNameFormatter(EncodingStyle::SOAP_11->value, 'Struct') => new SoapEnc\SoapObjectEncoder(),
// SOAP 1.2 ENC
...pull(
EncodingStyle::listKnownSoap12Version(),
static fn () => new SoapEnc\SoapArrayEncoder(),
static fn (string $namespace): string => $qNameFormatter($namespace, 'Array')
),
...pull(
EncodingStyle::listKnownSoap12Version(),
static fn () => new SoapEnc\SoapObjectEncoder(),
static fn (string $namespace): string => $qNameFormatter($namespace, 'Struct')
),
// Apache Map
$qNameFormatter(ApacheMapDetector::NAMESPACE, 'Map') => new SoapEnc\ApacheMapEncoder(),
// Special XSD cases
$qNameFormatter($xsd, 'any') => new AnyElementEncoder(),
])
);
}
/**
* @param non-empty-string $namespace
* @param non-empty-string $name
* @param class-string $class
*/
public function addClassMap(string $namespace, string $name, string $class): self
{
$this->complextTypeMap->add(
(new QNameFormatter())($namespace, $name),
new ObjectEncoder($class)
);
return $this;
}
public function addClassMapCollection(ClassMapCollection $classMapCollection): self
{
foreach ($classMapCollection as $classMap) {
$this->addClassMap(
$classMap->getXmlNamespace(),
$classMap->getXmlType(),
$classMap->getPhpClassName()
);
}
return $this;
}
/**
* @template T of \BackedEnum
*
* @param non-empty-string $namespace
* @param non-empty-string $name
* @param enum-string<T> $enumClass
*/
public function addBackedEnum(string $namespace, string $name, string $enumClass): self
{
$this->simpleTypeMap->add(
(new QNameFormatter())($namespace, $name),
new SimpleType\BackedEnumTypeEncoder($enumClass)
);
return $this;
}
public function addBackedEnumClassMapCollection(ClassMapCollection $classMapCollection): self
{
foreach ($classMapCollection as $classMap) {
$this->addBackedEnum(
$classMap->getXmlNamespace(),
$classMap->getXmlType(),
$classMap->getPhpClassName()
);
}
return $this;
}
/**
* @param non-empty-string $namespace
* @param non-empty-string $name
* @param XmlEncoder<mixed, string> $encoder
*/
public function addSimpleTypeConverter(string $namespace, string $name, XmlEncoder $encoder): self
{
$this->simpleTypeMap->add(
(new QNameFormatter())($namespace, $name),
$encoder
);
return $this;
}
/**
* @param non-empty-string $namespace
* @param non-empty-string $name
* @param XmlEncoder<mixed, string> $encoder
*/
public function addComplexTypeConverter(string $namespace, string $name, XmlEncoder $encoder): self
{
$this->complextTypeMap->add(
(new QNameFormatter())($namespace, $name),
$encoder
);
return $this;
}
/**
* @return XmlEncoder<mixed, string>
*/
public function findSimpleEncoderByXsdType(XsdType $type): XmlEncoder
{
return $this->findSimpleEncoderByNamespaceName(
$type->getXmlNamespace(),
$type->getXmlTypeName()
);
}
/**
* @return XmlEncoder<mixed, string>
*/
public function findSimpleEncoderByNamespaceName(string $namespace, string $name): XmlEncoder
{
$qNameFormatter = new QNameFormatter();
$found = $this->simpleTypeMap->get($qNameFormatter($namespace, $name));
if ($found) {
return $found;
}
return SimpleType\ScalarTypeEncoder::default();
}
public function hasRegisteredSimpleTypeForXsdType(XsdType $type): bool
{
return $this->hasRegisteredSimpleTypeForNamespaceName(
$type->getXmlNamespace(),
$type->getXmlTypeName()
);
}
public function hasRegisteredSimpleTypeForNamespaceName(string $namespace, string $name): bool
{
$qNameFormatter = new QNameFormatter();
return $this->simpleTypeMap->contains($qNameFormatter($namespace, $name));
}
/**
* @return XmlEncoder<mixed, string>
*/
public function findComplexEncoderByXsdType(XsdType $type): XmlEncoder
{
return $this->findComplexEncoderByNamespaceName(
$type->getXmlNamespace(),
$type->getXmlTypeName()
);
}
/**
* @return XmlEncoder<mixed, string>
*/
public function findComplexEncoderByNamespaceName(string $namespace, string $name): XmlEncoder
{
$qNameFormatter = new QNameFormatter();
$found = $this->complextTypeMap->get($qNameFormatter($namespace, $name));
if ($found) {
return $found;
}
return new ObjectEncoder(stdClass::class);
}
public function hasRegisteredComplexTypeForXsdType(XsdType $type): bool
{
return $this->hasRegisteredComplexTypeForNamespaceName(
$type->getXmlNamespace(),
$type->getXmlTypeName()
);
}
public function hasRegisteredComplexTypeForNamespaceName(string $namespace, string $name): bool
{
$qNameFormatter = new QNameFormatter();
return $this->complextTypeMap->contains($qNameFormatter($namespace, $name));
}
/**
* @return XmlEncoder<mixed, string|Stringable>
*/
public function detectEncoderForContext(Context $context): XmlEncoder
{
return EncoderDetector::default()($context);
}
/**
* @param int $libXmlOptions - bitmask of LIBXML_* constants https://www.php.net/manual/en/libxml.constants.php
*/
public function setDecoderLibXmlOptions(int $libXmlOptions): self
{
$this->decoderLibXmlOptions = $libXmlOptions;
return $this;
}
/**
* @return int - bitmask of LIBXML_* constants https://www.php.net/manual/en/libxml.constants.php
*/
public function decoderLibXmlOptions(): int
{
return $this->decoderLibXmlOptions;
}
}