-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathObjectAccess.php
More file actions
161 lines (139 loc) · 5.36 KB
/
ObjectAccess.php
File metadata and controls
161 lines (139 loc) · 5.36 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
declare(strict_types=1);
namespace Soap\Encoding\Encoder;
use Soap\Encoding\Cache\ScopedCache;
use Soap\Encoding\EncoderRegistry;
use Soap\Encoding\Normalizer\PhpPropertyNameNormalizer;
use Soap\Encoding\TypeInference\ComplexTypeBuilder;
use Soap\Engine\Metadata\Model\Property;
use Soap\Engine\Metadata\Model\Type;
use Soap\Engine\Metadata\Model\TypeMeta;
use VeeWee\Reflecta\Iso\Iso;
use VeeWee\Reflecta\Lens\Lens;
use function Psl\Vec\sort_by;
use function VeeWee\Reflecta\Lens\index;
use function VeeWee\Reflecta\Lens\optional;
use function VeeWee\Reflecta\Lens\property;
final class ObjectAccess
{
/**
* @param array<string, Property> $properties
* @param array<string, Lens<object, mixed>> $encoderLenses
* @param array<string, Lens<array, mixed>> $decoderLenses
* @param array<string, Iso<mixed, string>> $isos
*/
public function __construct(
public readonly array $properties,
public readonly array $encoderLenses,
public readonly array $decoderLenses,
public readonly array $isos,
public readonly bool $isAnyPropertyQualified
) {
}
/**
* @return ScopedCache<EncoderRegistry, self>
*
* @psalm-suppress LessSpecificReturnStatement, MoreSpecificReturnType, MixedReturnStatement
*/
private static function cache(): ScopedCache
{
static $cache = new ScopedCache();
return $cache;
}
private static function cacheKey(Context $context): string
{
return $context->type->getXmlNamespace() . '|' . $context->type->getName()
. '|' . $context->bindingUse->value;
}
public static function forContext(Context $context): self
{
return self::cache()->lookup(
$context->registry,
self::cacheKey($context),
static fn (): self => self::build($context)
);
}
private static function build(Context $context): self
{
$type = ComplexTypeBuilder::default()($context);
$sortedProperties = sort_by(
$type->getProperties(),
static fn (Property $property): bool => !$property->getType()->getMeta()->isAttribute()->unwrapOr(false),
);
$normalizedProperties = [];
$encoderLenses = [];
$decoderLenses = [];
$isos = [];
$isAnyPropertyQualified = false;
foreach ($sortedProperties as $property) {
$propertyType = $property->getType();
$propertyTypeMeta = $propertyType->getMeta();
$propertyContext = $context->withType($propertyType);
$name = $property->getName();
$normalizedName = PhpPropertyNameNormalizer::normalize($name);
$encoder = $context->registry->detectEncoderForContext($propertyContext);
$shouldLensBeOptional = self::shouldLensBeOptional($propertyTypeMeta);
$normalizedProperties[$normalizedName] = $property;
$encoderLenses[$normalizedName] = self::createEncoderLensForType($shouldLensBeOptional, $normalizedName, $encoder, $type, $property);
$decoderLenses[$normalizedName] = self::createDecoderLensForType($shouldLensBeOptional, $name, $encoder, $type, $property);
$isos[$normalizedName] = $encoder->iso($propertyContext);
$isAnyPropertyQualified = $isAnyPropertyQualified || $propertyTypeMeta->isQualified()->unwrapOr(false);
}
return new self(
$normalizedProperties,
$encoderLenses,
$decoderLenses,
$isos,
$isAnyPropertyQualified
);
}
/**
* @return Lens<object, mixed>
*/
private static function createEncoderLensForType(
bool $shouldLensBeOptional,
string $normalizedName,
XmlEncoder $encoder,
Type $type,
Property $property,
): Lens {
$lens = match (true) {
$encoder instanceof Feature\DecoratingEncoder => self::createEncoderLensForType($shouldLensBeOptional, $normalizedName, $encoder->decoratedEncoder(), $type, $property),
$encoder instanceof Feature\ProvidesObjectEncoderLens => $encoder::createObjectEncoderLens($type, $property),
default => property($normalizedName)
};
/** @var Lens<object, mixed> */
return $shouldLensBeOptional ? optional($lens) : $lens;
}
/**
* @return Lens<array, mixed>
*/
private static function createDecoderLensForType(
bool $shouldLensBeOptional,
string $name,
XmlEncoder $encoder,
Type $type,
Property $property,
): Lens {
$lens = match(true) {
$encoder instanceof Feature\DecoratingEncoder => self::createDecoderLensForType($shouldLensBeOptional, $name, $encoder->decoratedEncoder(), $type, $property),
$encoder instanceof Feature\ProvidesObjectDecoderLens => $encoder::createObjectDecoderLens($type, $property),
default => index($name),
};
/** @var Lens<array, mixed> */
return $shouldLensBeOptional ? optional($lens) : $lens;
}
private static function shouldLensBeOptional(TypeMeta $meta): bool
{
if ($meta->isNullable()->unwrapOr(false)) {
return true;
}
if (
$meta->isAttribute()->unwrapOr(false) &&
$meta->use()->unwrapOr('optional') === 'optional'
) {
return true;
}
return false;
}
}