forked from php-soap/encoding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSoapArrayEncoder.php
More file actions
112 lines (102 loc) · 3.52 KB
/
SoapArrayEncoder.php
File metadata and controls
112 lines (102 loc) · 3.52 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
declare(strict_types=1);
namespace Soap\Encoding\Encoder\SoapEnc;
use Closure;
use DOMElement;
use Soap\Encoding\Encoder\Context;
use Soap\Encoding\Encoder\Feature\ListAware;
use Soap\Encoding\Encoder\XmlEncoder;
use Soap\Encoding\Xml\Node\Element;
use Soap\Encoding\Xml\Writer\XsdTypeXmlElementWriter;
use Soap\Encoding\Xml\Writer\XsiAttributeBuilder;
use Soap\WsdlReader\Model\Definitions\BindingUse;
use VeeWee\Reflecta\Iso\Iso;
use function count;
use function Psl\Fun\lazy;
use function Psl\Vec\map;
use function VeeWee\Xml\Dom\Locator\Element\children as readChildren;
use function VeeWee\Xml\Writer\Builder\children;
use function VeeWee\Xml\Writer\Builder\prefixed_attribute;
use function VeeWee\Xml\Writer\Builder\raw as buildRaw;
/**
* @implements XmlEncoder<list<mixed>, non-empty-string>
*/
final class SoapArrayEncoder implements ListAware, XmlEncoder
{
/**
* @return Iso<list<mixed>, non-empty-string>
*/
public function iso(Context $context): Iso
{
$arrayAccess = lazy(static fn (): SoapArrayAccess => SoapArrayAccess::forContext($context));
return (new Iso(
/**
* @param list<mixed> $value
* @return non-empty-string
*/
fn (array $value): string => $this->encodeArray($context, $arrayAccess(), $value),
/**
* @param non-empty-string|Element $value
* @return list<mixed>
*/
fn (string|Element $value): array => $this->decodeArray(
$context,
$arrayAccess(),
$value instanceof Element ? $value : Element::fromString($value)
),
));
}
/**
* @param list<mixed> $data
*
* @return non-empty-string
*/
private function encodeArray(Context $context, SoapArrayAccess $arrayAccess, array $data): string
{
$iso = $arrayAccess->itemEncoder->iso($arrayAccess->itemContext);
return (new XsdTypeXmlElementWriter())(
$context,
children([
...(
$context->bindingUse === BindingUse::ENCODED
? [
new XsiAttributeBuilder(
$context,
XsiAttributeBuilder::resolveXsiTypeForValue($context, [])
),
prefixed_attribute(
'SOAP-ENC',
'arrayType',
$arrayAccess->xsiType . '['.count($data).']'
),
]
: []
),
...map(
$data,
static fn (mixed $value): Closure => buildRaw((string) $iso->to($value))
)
])
);
}
/**
* @return list<mixed>
*/
private function decodeArray(Context $context, SoapArrayAccess $arrayAccess, Element $value): array
{
$element = $value->element();
$iso = $arrayAccess->itemEncoder->iso($arrayAccess->itemContext);
return readChildren($element)->reduce(
/**
* @param list<mixed> $list
* @return list<mixed>
*/
static function (array $list, DOMElement $item) use ($iso): array {
/** @var mixed $value */
$value = $iso->from(Element::fromDOMElement($item));
return [...$list, $value];
},
[],
);
}
}