-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathXsiTypeEncoder.php
More file actions
59 lines (52 loc) · 1.88 KB
/
XsiTypeEncoder.php
File metadata and controls
59 lines (52 loc) · 1.88 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
<?php declare(strict_types=1);
namespace Soap\Encoding\Encoder;
use Soap\Encoding\TypeInference\XsiTypeDetector;
use Soap\Encoding\Xml\Node\Element;
use VeeWee\Reflecta\Iso\Iso;
use function Psl\Type\non_empty_string;
/**
* @implements XmlEncoder<mixed, string>
*/
final readonly class XsiTypeEncoder implements Feature\ElementAware, XmlEncoder
{
/**
* @param XmlEncoder<mixed, string> $encoder
*/
public function __construct(
private XmlEncoder $encoder
) {
}
/**
* @return Iso<mixed, string>
*/
public function iso(Context $context): Iso
{
return new Iso(
function (mixed $value) use ($context) : string {
return $this->to($context, $value);
},
function (string|Element $value) use ($context) : mixed {
return $this->from(
$context,
($value instanceof Element ? $value : Element::fromString(non_empty_string()->assert($value)))
);
}
);
}
private function to(Context $context, 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.
return $this->encoder->iso($context)->to($value);
}
private function from(Context $context, Element $value): mixed
{
/** @var XmlEncoder<string, mixed> $encoder */
$encoder = match (true) {
$this->encoder instanceof Feature\DisregardXsiInformation => $this->encoder,
default => XsiTypeDetector::detectEncoderFromXmlElement($context, $value->element())->unwrapOr($this->encoder)
};
return $encoder->iso($context)->from($value);
}
}