|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +require_once \dirname(__DIR__, 3) . '/vendor/autoload.php'; |
| 4 | + |
| 5 | +use Soap\Encoding\Encoder; |
| 6 | +use Soap\Encoding\EncoderRegistry; |
| 7 | +use Soap\Encoding\Test\PhpCompatibility\Implied\ImpliedSchema015A; |
| 8 | +use Soap\Encoding\Test\PhpCompatibility\Implied\ImpliedSchema015B; |
| 9 | + |
| 10 | +/** |
| 11 | + * Sometimes, your XSD schema tell you to use any implementation for a certain base type. |
| 12 | + * When building the SOAP payload, you want control over what child object you want to use. |
| 13 | + * This encoder can be customized to encoder and context to use for any PHP. |
| 14 | + * This encoder works together with the XsiTypeEncoder to decode back from xsi:type attributes. |
| 15 | + * |
| 16 | + * Example: |
| 17 | + * <complexType name="A"> |
| 18 | + * <sequence> |
| 19 | + * <element name="foo" type="xsd:string" /> |
| 20 | + * </sequence> |
| 21 | + * </complexType> |
| 22 | + * <complexType name="B"> |
| 23 | + * <complexContent> |
| 24 | + * <extension base="tns:A"> |
| 25 | + * <sequence> |
| 26 | + * <element name="bar" type="xsd:string" /> |
| 27 | + * </sequence> |
| 28 | + * </extension> |
| 29 | + * </complexContent> |
| 30 | + * </complexType> |
| 31 | + * <element name="return"> |
| 32 | + * <complexType> |
| 33 | + * <sequence> |
| 34 | + * <element name="responses" type="tns:A" minOccurs="0" maxOccurs="unbounded" /> |
| 35 | + * </sequence> |
| 36 | + * </complexType> |
| 37 | + * </element> |
| 38 | + * |
| 39 | + * |
| 40 | + * The result looks like this: |
| 41 | + * |
| 42 | + * <testParam xsi:type="tns:return"> |
| 43 | + * <responses xsi:type="tns:A"> |
| 44 | + * <foo xsi:type="xsd:string">abc</foo> |
| 45 | + * </responses> |
| 46 | + * <responses xsi:type="tns:B"> |
| 47 | + * <foo xsi:type="xsd:string">def</foo> |
| 48 | + * <bar xsi:type="xsd:string">ghi</bar> |
| 49 | + * </responses> |
| 50 | + * </testParam> |
| 51 | + * |
| 52 | + * <=> |
| 53 | + * |
| 54 | + * ^ {#2507 |
| 55 | + * +"responses": array:2 [ |
| 56 | + * 0 => A^ {#2501 |
| 57 | + * +foo: "abc" |
| 58 | + * } |
| 59 | + * 1 => B {#2504 |
| 60 | + * +foo: "def" |
| 61 | + * +bar: "ghi" |
| 62 | + * } |
| 63 | + * ] |
| 64 | + * } |
| 65 | + */ |
| 66 | + |
| 67 | +EncoderRegistry::default() |
| 68 | + ->addClassMap('http://test-uri/', 'B', ImpliedSchema015B::class) |
| 69 | + ->addComplexTypeConverter('http://test-uri/', 'A', new Encoder\MatchingValueEncoder( |
| 70 | + encoderDetector: static fn (Encoder\Context $context, mixed $value): array => |
| 71 | + $value instanceof ImpliedSchema015B |
| 72 | + ? [ |
| 73 | + $context->withType($context->type->copy('B')->withXmlTypeName('B')), |
| 74 | + new Encoder\ObjectEncoder(ImpliedSchema015B::class), |
| 75 | + ] |
| 76 | + : [$context], |
| 77 | + defaultEncoder: new Encoder\ObjectEncoder(ImpliedSchema015A::class) |
| 78 | + )) |
| 79 | + // Alternative for using stdObjects only: |
| 80 | + ->addComplexTypeConverter('http://test-uri/', 'A', new Encoder\MatchingValueEncoder( |
| 81 | + encoderDetector: static fn (Encoder\Context $context, mixed $value): Encoder\Context => $context->withType( |
| 82 | + property_exists($value, 'bar') |
| 83 | + ? $context->type->copy('B')->withXmlTypeName('B') |
| 84 | + : $context->type |
| 85 | + ), |
| 86 | + defaultEncoder: new Encoder\ObjectEncoder(stdClass::class) |
| 87 | + )); |
0 commit comments