-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathElementEncoder.php
54 lines (49 loc) · 1.52 KB
/
ElementEncoder.php
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
<?php
declare(strict_types=1);
namespace Soap\Encoding\Encoder;
use Soap\Encoding\Xml\Node\Element;
use Soap\Encoding\Xml\Reader\ElementValueReader;
use Soap\Encoding\Xml\Writer\ElementValueBuilder;
use Soap\Encoding\Xml\Writer\XsdTypeXmlElementWriter;
use VeeWee\Reflecta\Iso\Iso;
/**
* @implements XmlEncoder<mixed, string>
*/
final class ElementEncoder implements Feature\ElementAware, XmlEncoder
{
/**
* @param XmlEncoder<mixed, string> $typeEncoder
*/
public function __construct(
private readonly XmlEncoder $typeEncoder
) {
}
/**
* @return Iso<mixed, string>
*/
public function iso(Context $context): Iso
{
$typeEncoder = $this->typeEncoder;
$context = $this->typeEncoder instanceof Feature\ElementContextEnhancer
? $this->typeEncoder->enhanceElementContext($context)
: $context;
return new Iso(
/**
* @psalm-param mixed $raw
*/
static fn (mixed $raw): string => (new XsdTypeXmlElementWriter())(
$context,
(new ElementValueBuilder($context, $typeEncoder, $raw))
),
/**
* @psalm-param non-empty-string|Element $xml
* @psalm-return mixed
*/
static fn (Element|string $xml): mixed => (new ElementValueReader())(
$context,
$typeEncoder,
($xml instanceof Element ? $xml : Element::fromString($xml))->element()
)
);
}
}