Repository navigation
Expand file tree
/
Copy pathObjectEncoder.php
More file actions
142 lines (128 loc) · 4.88 KB
/
Copy pathObjectEncoder.php
File metadata and controls
142 lines (128 loc) · 4.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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
declare(strict_types=1);
namespace Soap\Encoding\Encoder;
use Soap\Encoding\Xml\Node\Element;
use Soap\Encoding\Xml\Node\ElementList;
use Soap\Encoding\Xml\Node\ElementLookup;
use Soap\Encoding\Xml\Writer\AttributeBuilder;
use Soap\Encoding\Xml\Writer\NilAttributeBuilder;
use Soap\Encoding\Xml\Writer\XsdTypeXmlElementWriter;
use Soap\Encoding\Xml\Writer\XsiAttributeBuilder;
use VeeWee\Reflecta\Iso\Iso;
use function is_object;
use function Psl\Fun\lazy;
use function VeeWee\Reflecta\Iso\object_data;
use function VeeWee\Reflecta\Reflect\property_is_initialized;
use function VeeWee\Xml\Writer\Builder\children as writeChildren;
use function VeeWee\Xml\Writer\Builder\raw;
use function VeeWee\Xml\Writer\Builder\value as buildValue;
/**
* @template TObj of object
*
* @implements XmlEncoder<TObj|array, TObj, non-empty-string, Element|non-empty-string>
*/
final class ObjectEncoder implements Feature\ElementAware, XmlEncoder
{
/**
* @param class-string<TObj> $className
*/
public function __construct(
private readonly string $className
) {
}
/**
* @return Iso<TObj|array, TObj, non-empty-string, Element|non-empty-string>
*/
public function iso(Context $context): Iso
{
$objectAccess = lazy(static fn (): ObjectAccess => ObjectAccess::forContext($context));
return new Iso(
/**
* @param TObj|array $value
* @return non-empty-string
*/
function (object|array $value) use ($context, $objectAccess) : string {
return $this->to($context, $objectAccess(), $value);
},
/**
* @param non-empty-string|Element $value
* @return TObj
*/
function (string|Element $value) use ($context, $objectAccess) : object {
return $this->from(
$context,
$objectAccess(),
($value instanceof Element ? $value : Element::fromString($value))
);
}
);
}
/**
* @param TObj|array $data
*
* @return non-empty-string
*/
private function to(Context $context, ObjectAccess $objectAccess, object|array $data): string
{
$data = is_object($data) ? $data : (object) $data;
$defaultAction = writeChildren([]);
$children = [
XsiAttributeBuilder::forEncodedValue(
$context,
$this,
$data,
forceIncludeXsiTargetNamespace: !$objectAccess->isAnyPropertyQualified,
),
];
foreach ($objectAccess->properties as $normalizePropertyName => $property) {
/** @var mixed $value */
$value = $objectAccess->encodeReaders[$normalizePropertyName]($data);
if ($value === null && match ($objectAccess->onNull[$normalizePropertyName]) {
NullValueHandling::Write => false,
NullValueHandling::Skip => true,
NullValueHandling::SkipWhenAbsent => !property_is_initialized($data, $normalizePropertyName),
}) {
continue;
}
$iso = $objectAccess->isos[$normalizePropertyName];
$children[] = match(true) {
$objectAccess->isAttribute[$normalizePropertyName] => $value !== null ? (new AttributeBuilder(
$property->getType(),
(string) $iso->to($value)
))(...) : $defaultAction,
$property->getName() === '_' => $value !== null
? buildValue((string) $iso->to($value))
: (new NilAttributeBuilder())(...),
default => raw((string) $iso->to($value))
};
}
return (new XsdTypeXmlElementWriter())(
$context,
writeChildren($children)
);
}
/**
* @return TObj
*/
private function from(Context $context, ObjectAccess $objectAccess, Element $data): object
{
$lookup = ElementLookup::fromElement($data);
/** @var Iso<TObj, TObj, array<string, mixed>, array<string, mixed>> $objectData */
$objectData = object_data($this->className);
$result = [];
foreach ($objectAccess->isos as $normalizePropertyName => $iso) {
/** @var Element|ElementList|string|null $value */
$value = $objectAccess->decodeReaders[$normalizePropertyName]($lookup);
/**
* @var mixed
* @psalm-suppress PossiblyNullArgument
*/
$result[$normalizePropertyName] = match(true) {
$objectAccess->isAttribute[$normalizePropertyName] => $iso->from($value),
$value !== null => $iso->from($value),
default => $objectAccess->decodeDefaultValues[$normalizePropertyName],
};
}
return $objectData->from($result);
}
}