forked from php-soap/encoding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathElementValueBuilder.php
More file actions
77 lines (68 loc) · 2.2 KB
/
ElementValueBuilder.php
File metadata and controls
77 lines (68 loc) · 2.2 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
<?php
declare(strict_types=1);
namespace Soap\Encoding\Xml\Writer;
use Closure;
use Generator;
use Soap\Encoding\Encoder\Context;
use Soap\Encoding\Encoder\Feature;
use Soap\Encoding\Encoder\XmlEncoder;
use VeeWee\Reflecta\Iso\Iso;
use XMLWriter;
use function VeeWee\Xml\Writer\Builder\cdata;
use function VeeWee\Xml\Writer\Builder\children;
use function VeeWee\Xml\Writer\Builder\value;
final class ElementValueBuilder
{
/**
* @param list<Closure(XMLWriter): Generator<bool>> $builders
*/
private function __construct(
private readonly array $builders,
) {
}
/**
* @param XmlEncoder<mixed, string> $encoder
* @psalm-param mixed $value
*/
public static function fromEncoder(Context $context, XmlEncoder $encoder, mixed $value): self
{
return new self([
XsiAttributeBuilder::forEncodedValue($context, $encoder, $value),
self::valueWriter($encoder, static fn (): string => $encoder->iso($context)->to($value)),
]);
}
/**
* @param XmlEncoder<mixed, string> $encoder
* @param Iso<mixed, string> $iso
* @psalm-param mixed $value
*/
public static function fromIso(Context $context, XmlEncoder $encoder, Iso $iso, mixed $value): self
{
return new self([
XsiAttributeBuilder::forEncodedValue($context, $encoder, $value),
self::valueWriter($encoder, static fn (): string => $iso->to($value)),
]);
}
/**
* @return Generator<bool>
*/
public function __invoke(XMLWriter $writer): Generator
{
yield from children($this->builders)($writer);
}
/**
* @param XmlEncoder<mixed, string> $encoder
* @param Closure(): string $valueProvider
*
* @return Closure(XMLWriter): Generator<bool>
*/
private static function valueWriter(XmlEncoder $encoder, Closure $valueProvider): Closure
{
$isCData = $encoder instanceof Feature\CData;
return static function (XMLWriter $writer) use ($isCData, $valueProvider): Generator {
$encoded = $valueProvider();
$builder = $isCData ? cdata(value($encoded)) : value($encoded);
yield from $builder($writer);
};
}
}