Repository navigation
Expand file tree
/
Copy pathXsiTypeEncoder.php
More file actions
88 lines (79 loc) · 3.46 KB
/
Copy pathXsiTypeEncoder.php
File metadata and controls
88 lines (79 loc) · 3.46 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
<?php declare(strict_types=1);
namespace Soap\Encoding\Encoder;
use Closure;
use Soap\Encoding\TypeInference\XsiTypeDetector;
use Soap\Encoding\Xml\Node\Element;
use Soap\Encoding\Xml\Node\ElementList;
use VeeWee\Reflecta\Iso\Iso;
use VeeWee\Reflecta\Iso\IsoInterface;
use function Psl\Type\non_empty_string;
/**
* @implements XmlEncoder<mixed, mixed, string, Element|string>
* @implements Feature\DecoratingEncoder<mixed, mixed, string|null, Element|ElementList|string|null>
*/
final readonly class XsiTypeEncoder implements Feature\DecoratingEncoder, Feature\ElementAware, XmlEncoder
{
/**
* @param XmlEncoder<mixed, mixed, string|null, Element|ElementList|string|null> $encoder
*/
public function __construct(
private XmlEncoder $encoder
) {
}
/**
* @return XmlEncoder<mixed, mixed, string|null, Element|ElementList|string|null>
*/
public function decoratedEncoder(): XmlEncoder
{
return $this->encoder;
}
/**
* @return Iso<mixed, mixed, string, Element|string>
*/
public function iso(Context $context): Iso
{
$innerIso = $this->encoder->iso($context);
$detectEncoder = $this->encoder instanceof Feature\DisregardXsiInformation
? null
: XsiTypeDetector::encoderDetectorForContext($context);
return new Iso(
function (mixed $value) use ($innerIso) : string {
return $this->to($innerIso, $value);
},
function (string|Element $value) use ($context, $innerIso, $detectEncoder) : mixed {
return $this->from(
$context,
$innerIso,
$detectEncoder,
($value instanceof Element ? $value : Element::fromString(non_empty_string()->assert($value)))
);
}
);
}
/**
* @param IsoInterface<mixed, mixed, string|null, Element|ElementList|string|null> $innerIso
*/
private function to(IsoInterface $innerIso, mixed $value): string
{
// There is no way to know what xsi:type to use when encoding any type.
// The type defined in the wsdl will always be used to encode the value.
// If you want more control over the encoded type, please control how to encode by using the MatchingValueEncoder.
//
// The inner XML output slot is `string|null` because it mirrors the shared aggregate encoder type,
// but element encoders always produce a (non-empty) string in the to() direction: the cast only collapses
// the type-level nullability that originates from attribute encoders elsewhere in the aggregate.
return (string) $innerIso->to($value);
}
/**
* @param IsoInterface<mixed, mixed, string|null, Element|ElementList|string|null> $innerIso
* @param (Closure(\Dom\Element): (FixedIsoEncoder<mixed, mixed, string|null, Element|ElementList|string|null>|null))|null $detectEncoder
*/
private function from(Context $context, IsoInterface $innerIso, ?Closure $detectEncoder, Element $value): mixed
{
// xsi:type is an attribute on the element itself, so an element without attributes cannot carry one.
$element = $value->element();
$iso = ($detectEncoder && $element->hasAttributes() ? $detectEncoder($element) : null)?->iso($context) ?? $innerIso;
/** @psalm-suppress ImplicitToStringCast - Encoders accept string|Element in from() */
return $iso->from($value);
}
}