5
5
6
6
use Generator ;
7
7
use Soap \Encoding \Encoder \Context ;
8
- use Soap \Encoding \Encoder \Feature \ CData ;
8
+ use Soap \Encoding \Encoder \Feature ;
9
9
use Soap \Encoding \Encoder \XmlEncoder ;
10
10
use Soap \Encoding \TypeInference \XsiTypeDetector ;
11
11
use Soap \WsdlReader \Model \Definitions \BindingUse ;
@@ -48,16 +48,45 @@ private function buildXsiType(XMLWriter $writer): Generator
48
48
}
49
49
50
50
$ context = $ this ->context ;
51
- $ type = $ context ->type ;
51
+ [$ xsiType , $ includeXsiTargetNamespace ] = match (true ) {
52
+ $ this ->encoder instanceof Feature \XsiTypeCalculator => [
53
+ $ this ->encoder ->resolveXsiTypeForValue ($ context , $ this ->value ),
54
+ $ this ->encoder ->shouldIncludeXsiTargetNamespace ($ context ),
55
+ ],
56
+ default => [
57
+ self ::resolveXsiTypeForValue ($ context , $ this ->value ),
58
+ self ::shouldIncludeXsiTargetNamespace ($ context ),
59
+ ],
60
+ };
52
61
53
62
yield from (new XsiAttributeBuilder (
54
63
$ this ->context ,
55
- XsiTypeDetector::detectFromValue ($ context , $ this ->value ),
56
- includeXsiTargetNamespace: $ type ->getXmlTargetNamespace () !== $ type ->getXmlNamespace ()
57
- || !$ type ->getMeta ()->isQualified ()->unwrapOr (false )
64
+ $ xsiType ,
65
+ $ includeXsiTargetNamespace ,
58
66
))($ writer );
59
67
}
60
68
69
+ /**
70
+ * Can be used as a default fallback function when implementing the XsiTypeCalculator interface.
71
+ * Tells the XsiAttributeBuilder what xsi:type attribute should be set to for a given value.
72
+ */
73
+ public static function resolveXsiTypeForValue (Context $ context , mixed $ value ): string
74
+ {
75
+ return XsiTypeDetector::detectFromValue ($ context , $ value );
76
+ }
77
+
78
+ /**
79
+ * Can be used as a default fallback function when implementing the XsiTypeCalculator interface.
80
+ * Tells the XsiAttributeBuilder that the prefix of the xsi:type should be imported as a xmlns namespace.
81
+ */
82
+ public static function shouldIncludeXsiTargetNamespace (Context $ context ): bool
83
+ {
84
+ $ type = $ context ->type ;
85
+
86
+ return $ type ->getXmlTargetNamespace () !== $ type ->getXmlNamespace ()
87
+ || !$ type ->getMeta ()->isQualified ()->unwrapOr (false );
88
+ }
89
+
61
90
/**
62
91
* @return Generator<bool>
63
92
*/
@@ -66,7 +95,7 @@ private function buildValue(XMLWriter $writer): Generator
66
95
$ encoded = $ this ->encoder ->iso ($ this ->context )->to ($ this ->value );
67
96
68
97
$ builder = match (true ) {
69
- $ this ->encoder instanceof CData => cdata (value ($ encoded )),
98
+ $ this ->encoder instanceof Feature \ CData => cdata (value ($ encoded )),
70
99
default => value ($ encoded )
71
100
};
72
101
0 commit comments