-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRepeatingElementEncoder.php
75 lines (67 loc) · 2.18 KB
/
RepeatingElementEncoder.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
declare(strict_types=1);
namespace Soap\Encoding\Encoder;
use Soap\Encoding\Xml\Node\Element;
use Soap\Encoding\Xml\Node\ElementList;
use Soap\Engine\Metadata\Model\TypeMeta;
use VeeWee\Reflecta\Iso\Iso;
use function Psl\Str\join;
use function Psl\Vec\map;
/**
* @template T
* @implements XmlEncoder<iterable<array-key, T>|null, string>
*/
final class RepeatingElementEncoder implements Feature\ElementAware, Feature\ListAware, XmlEncoder
{
/**
* @param XmlEncoder<T, string> $typeEncoder
*/
public function __construct(
private readonly XmlEncoder $typeEncoder
) {
}
/**
* @return Iso<iterable<array-key, T>|null, string>
*/
public function iso(Context $context): Iso
{
$type = $context->type;
$innerIso = $this->typeEncoder->iso(
$context->withType(
$type->withMeta(static fn (TypeMeta $meta): TypeMeta => $meta->withIsList(false))
)
);
return new Iso(
/**
* @param iterable<array-key, T>|null $raw
*/
static function (iterable|null $raw) use ($innerIso): string {
return join(
map(
$raw ?? [],
/**
* @param T $item
*/
static fn (mixed $item): string => $innerIso->to($item)
),
''
);
},
/**
* @return iterable<array-key, T>
*/
static function (Element|ElementList|string $xml) use ($innerIso): iterable {
$elements = match (true) {
$xml instanceof Element => [$xml],
$xml instanceof ElementList => $xml->elements(),
default => ElementList::fromString('<list>'.$xml.'</list>')->elements()
};
/** @var Iso<T|null, Element|non-empty-string> $innerIso */
return map(
$elements,
static fn (Element $element): mixed => $innerIso->from($element)
);
}
);
}
}