-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathObjectEncoder.php
165 lines (151 loc) · 5.78 KB
/
ObjectEncoder.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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
declare(strict_types=1);
namespace Soap\Encoding\Encoder;
use Closure;
use Exception;
use Soap\Encoding\TypeInference\XsiTypeDetector;
use Soap\Encoding\Xml\Node\Element;
use Soap\Encoding\Xml\Reader\DocumentToLookupArrayReader;
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 Soap\Engine\Metadata\Model\Property;
use VeeWee\Reflecta\Iso\Iso;
use VeeWee\Reflecta\Lens\Lens;
use function is_array;
use function Psl\Dict\map_with_key;
use function Psl\Fun\lazy;
use function VeeWee\Reflecta\Iso\object_data;
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 extends object
*
* @implements XmlEncoder<TObj|array, 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, 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
{
if (is_array($data)) {
$data = (object) $data;
}
$defaultAction = writeChildren([]);
return (new XsdTypeXmlElementWriter())(
$context,
writeChildren(
[
(new XsiAttributeBuilder(
$context,
XsiTypeDetector::detectFromValue($context, []),
includeXsiTargetNamespace: !$objectAccess->isAnyPropertyQualified,
)),
...map_with_key(
$objectAccess->properties,
static function (string $normalizePropertyName, Property $property) use ($objectAccess, $data, $defaultAction) : Closure {
$type = $property->getType();
$meta = $type->getMeta();
$isAttribute = $meta->isAttribute()->unwrapOr(false);
/** @var mixed $value */
$value = self::runLens(
$objectAccess->encoderLenses[$normalizePropertyName],
$data
);
$iso = $objectAccess->isos[$normalizePropertyName];
return match(true) {
$isAttribute => $value ? (new AttributeBuilder(
$type,
$iso->to($value)
))(...) : $defaultAction,
$property->getName() === '_' => $value !== null
? buildValue($iso->to($value))
: (new NilAttributeBuilder())(...),
default => raw($iso->to($value))
};
}
)
]
)
);
}
/**
* @return TObj
*/
private function from(Context $context, ObjectAccess $objectAccess, Element $data): object
{
$nodes = (new DocumentToLookupArrayReader())($data);
/** @var Iso<TObj, array<string, mixed>> $objectData */
$objectData = object_data($this->className);
return $objectData->from(
map_with_key(
$objectAccess->properties,
static function (string $normalizePropertyName, Property $property) use ($objectAccess, $nodes): mixed {
$type = $property->getType();
$meta = $type->getMeta();
/** @var string|null $value */
$value = self::runLens(
$objectAccess->decoderLenses[$normalizePropertyName],
$nodes,
);
$iso = $objectAccess->isos[$normalizePropertyName];
$defaultValue = $meta->isList()->unwrapOr(false) ? [] : null;
/** @psalm-suppress PossiblyNullArgument */
return match(true) {
$meta->isAttribute()->unwrapOr(false) => $iso->from($value),
default => $value !== null ? $iso->from($value) : $defaultValue,
};
},
)
);
}
private static function runLens(Lens $lens, mixed $data, mixed $default = null): mixed
{
try {
/** @var mixed */
return $lens->get($data);
} catch (Exception $e) {
return $default;
}
}
}