4
4
namespace Soap \Encoding \Encoder ;
5
5
6
6
use Closure ;
7
+ use Exception ;
7
8
use Soap \Encoding \Normalizer \PhpPropertyNameNormalizer ;
8
9
use Soap \Encoding \TypeInference \ComplexTypeBuilder ;
9
10
use Soap \Encoding \TypeInference \XsiTypeDetector ;
13
14
use Soap \Encoding \Xml \Writer \XsdTypeXmlElementWriter ;
14
15
use Soap \Encoding \Xml \Writer \XsiAttributeBuilder ;
15
16
use Soap \Engine \Metadata \Model \Property ;
16
- use Soap \Engine \Metadata \Model \XsdType ;
17
+ use Soap \Engine \Metadata \Model \TypeMeta ;
17
18
use VeeWee \Reflecta \Iso \Iso ;
18
19
use VeeWee \Reflecta \Lens \Lens ;
19
20
use function is_array ;
@@ -100,30 +101,27 @@ private function to(Context $context, array $properties, object|array $data): st
100
101
$ properties ,
101
102
function (Property $ property ) use ($ context , $ data , $ defaultAction ) : Closure {
102
103
$ type = $ property ->getType ();
103
- $ lens = $ this ->decorateLensForType (
104
+ $ meta = $ type ->getMeta ();
105
+ $ isAttribute = $ meta ->isAttribute ()->unwrapOr (false );
106
+
107
+ /** @var mixed $value */
108
+ $ value = $ this ->runLens (
104
109
property (PhpPropertyNameNormalizer::normalize ($ property ->getName ())),
105
- $ type
110
+ $ meta ,
111
+ $ data ,
112
+ null
106
113
);
107
- /**
108
- * @psalm-var mixed $value
109
- * @psalm-suppress PossiblyInvalidArgument - Psalm gets lost in the lens.
110
- */
111
- $ value = $ lens
112
- ->tryGet ($ data )
113
- ->catch (static fn () => null )
114
- ->getResult ();
115
-
116
- return $ this ->handleProperty (
117
- $ property ,
118
- onAttribute: fn (): Closure => $ value ? (new AttributeBuilder (
114
+
115
+ return match (true ) {
116
+ $ isAttribute => $ value ? (new AttributeBuilder (
119
117
$ type ,
120
118
$ this ->grabIsoForProperty ($ context , $ property )->to ($ value )
121
119
))(...) : $ defaultAction ,
122
- onValue: fn (): Closure => $ value
120
+ $ property -> getName () === ' _ ' => $ value
123
121
? buildValue ($ this ->grabIsoForProperty ($ context , $ property )->to ($ value ))
124
122
: (new NilAttributeBuilder ())(...),
125
- onElements: fn (): Closure => $ value ? raw ($ this ->grabIsoForProperty ($ context , $ property )->to ($ value )) : $ defaultAction,
126
- ) ;
123
+ default => $ value ? raw ($ this ->grabIsoForProperty ($ context , $ property )->to ($ value )) : $ defaultAction
124
+ } ;
127
125
}
128
126
)
129
127
]
@@ -149,23 +147,21 @@ private function from(Context $context, array $properties, string $data): object
149
147
function (Property $ property ) use ($ context , $ nodes ): mixed {
150
148
$ type = $ property ->getType ();
151
149
$ meta = $ type ->getMeta ();
152
- $ isList = $ meta -> isList ()-> unwrapOr ( false );
153
- /** @psalm- var string|null $value */
154
- $ value = $ this ->decorateLensForType (
150
+
151
+ /** @var string|null $value */
152
+ $ value = $ this ->runLens (
155
153
index ($ property ->getName ()),
156
- $ type
157
- )
158
- ->tryGet ($ nodes )
159
- ->catch (static fn () => null )
160
- ->getResult ();
161
- $ defaultValue = $ isList ? [] : null ;
162
-
163
- return $ this ->handleProperty (
164
- $ property ,
165
- onAttribute: fn (): mixed => /** @psalm-suppress PossiblyNullArgument */ $ this ->grabIsoForProperty ($ context , $ property )->from ($ value ),
166
- onValue: fn (): mixed => $ value !== null ? $ this ->grabIsoForProperty ($ context , $ property )->from ($ value ) : $ defaultValue ,
167
- onElements: fn (): mixed => $ value !== null ? $ this ->grabIsoForProperty ($ context , $ property )->from ($ value ) : $ defaultValue ,
154
+ $ meta ,
155
+ $ nodes ,
156
+ null
168
157
);
158
+ $ defaultValue = $ meta ->isList ()->unwrapOr (false ) ? [] : null ;
159
+
160
+ /** @psalm-suppress PossiblyNullArgument */
161
+ return match (true ) {
162
+ $ meta ->isAttribute ()->unwrapOr (false ) => $ this ->grabIsoForProperty ($ context , $ property )->from ($ value ),
163
+ default => $ value !== null ? $ this ->grabIsoForProperty ($ context , $ property )->from ($ value ) : $ defaultValue ,
164
+ };
169
165
},
170
166
static fn (Property $ property ) => PhpPropertyNameNormalizer::normalize ($ property ->getName ()),
171
167
)
@@ -183,27 +179,14 @@ private function grabIsoForProperty(Context $context, Property $property): Iso
183
179
return $ encoder ->iso ($ propertyContext );
184
180
}
185
181
186
- /**
187
- * @template X
188
- *
189
- * @param Closure(): X $onAttribute
190
- * @param Closure(): X $onValue
191
- * @param Closure(): X $onElements
192
- * @return X
193
- */
194
- private function handleProperty (
195
- Property $ property ,
196
- Closure $ onAttribute ,
197
- Closure $ onValue ,
198
- Closure $ onElements ,
199
- ) {
200
- $ meta = $ property ->getType ()->getMeta ();
201
-
202
- return match (true ) {
203
- $ meta ->isAttribute ()->unwrapOr (false ) => $ onAttribute (),
204
- $ property ->getName () === '_ ' => $ onValue (),
205
- default => $ onElements ()
206
- };
182
+ private function runLens (Lens $ lens , TypeMeta $ meta , mixed $ data , mixed $ default ): mixed
183
+ {
184
+ try {
185
+ /** @var mixed */
186
+ return $ this ->decorateLensForType ($ lens , $ meta )->get ($ data );
187
+ } catch (Exception $ e ) {
188
+ return $ default ;
189
+ }
207
190
}
208
191
209
192
/**
@@ -214,9 +197,8 @@ private function handleProperty(
214
197
*
215
198
* @return Lens<S, A>
216
199
*/
217
- private function decorateLensForType (Lens $ lens , XsdType $ type ): Lens
200
+ private function decorateLensForType (Lens $ lens , TypeMeta $ meta ): Lens
218
201
{
219
- $ meta = $ type ->getMeta ();
220
202
if ($ meta ->isNullable ()->unwrapOr (false )) {
221
203
return optional ($ lens );
222
204
}
@@ -237,14 +219,13 @@ private function decorateLensForType(Lens $lens, XsdType $type): Lens
237
219
private function detectProperties (Context $ context ): array
238
220
{
239
221
$ type = (new ComplexTypeBuilder ())($ context );
240
- $ properties = reindex (
222
+
223
+ return reindex (
241
224
sort_by (
242
225
$ type ->getProperties (),
243
226
static fn (Property $ property ): bool => !$ property ->getType ()->getMeta ()->isAttribute ()->unwrapOr (false ),
244
227
),
245
228
static fn (Property $ property ): string => $ property ->getName (),
246
229
);
247
-
248
- return $ properties ;
249
230
}
250
231
}
0 commit comments