diff --git a/README.md b/README.md index 30ff400..d5ef4ff 100644 --- a/README.md +++ b/README.md @@ -77,12 +77,18 @@ However, you can configure how the encoding should be done by adding your own en Some examples: ```php -use Soap\Encoding\Encoder\SimpleType\DateTimeTypeEncoder;use Soap\Encoding\EncoderRegistry; +use Soap\Encoding\ClassMap\ClassMap; +use Soap\Encoding\ClassMap\ClassMapCollection; +use Soap\Encoding\Encoder\SimpleType\DateTimeTypeEncoder; +use Soap\Encoding\EncoderRegistry; use Soap\Xml\Xmlns; EncoderRegistry::default() ->addClassMap('urn:namespace', 'TypeA', TypeA::class) ->addClassMap('urn:namespace', 'TypeB', TypeB::class) + ->addClassMapCollection(new ClassMapCollection( + new ClassMap('urn:namespace', 'TypeC', TypeC::class), + )) ->addBackedEnum('urn:namespace', 'EnumA', EnumA::class) ->addSimpleTypeConverter(Xmlns::xsd()->value(), 'dateTime', new DateTimeTypeEncoder('Y-m-d\TH:i:s')) ->addComplexTypeConverter('urn:namespace', 'TypeC', MySpecificTypeCEncoder::class); diff --git a/src/ClassMap/ClassMap.php b/src/ClassMap/ClassMap.php new file mode 100644 index 0000000..9a5a226 --- /dev/null +++ b/src/ClassMap/ClassMap.php @@ -0,0 +1,43 @@ +phpClassName; + } + + /** + * @return non-empty-string + */ + public function getXmlType(): string + { + return $this->wsdlType; + } + + /** + * @return non-empty-string + */ + public function getXmlNamespace(): string + { + return $this->xmlNamespace; + } +} diff --git a/src/ClassMap/ClassMapCollection.php b/src/ClassMap/ClassMapCollection.php new file mode 100644 index 0000000..ed2c891 --- /dev/null +++ b/src/ClassMap/ClassMapCollection.php @@ -0,0 +1,41 @@ + + */ +final class ClassMapCollection implements IteratorAggregate +{ + /** + * @var array + */ + private array $classMaps = []; + + public function __construct(ClassMap ... $classMaps) + { + foreach ($classMaps as $classMap) { + $this->set($classMap); + } + } + + public function set(ClassMap $classMap): self + { + $qname = (new QNameFormatter())($classMap->getXmlNamespace(), $classMap->getXmlType()); + $this->classMaps[$qname] = $classMap; + + return $this; + } + + /** + * @return ArrayIterator + */ + public function getIterator(): ArrayIterator + { + return new ArrayIterator($this->classMaps); + } +} diff --git a/src/EncoderRegistry.php b/src/EncoderRegistry.php index 12fa48c..28d5382 100644 --- a/src/EncoderRegistry.php +++ b/src/EncoderRegistry.php @@ -4,6 +4,7 @@ namespace Soap\Encoding; use Psl\Collection\MutableMap; +use Soap\Encoding\ClassMap\ClassMapCollection; use Soap\Encoding\Encoder\Context; use Soap\Encoding\Encoder\ElementEncoder; use Soap\Encoding\Encoder\EncoderDetector; @@ -176,6 +177,19 @@ public function addClassMap(string $namespace, string $name, string $class): sel return $this; } + public function addClassMapCollection(ClassMapCollection $classMapCollection): self + { + foreach ($classMapCollection as $classMap) { + $this->addClassMap( + $classMap->getXmlNamespace(), + $classMap->getXmlType(), + $classMap->getPhpClassName() + ); + } + + return $this; + } + /** * @template T of \BackedEnum * diff --git a/tests/Unit/ClassMap/ClassMapCollectionTest.php b/tests/Unit/ClassMap/ClassMapCollectionTest.php new file mode 100644 index 0000000..7faaf7a --- /dev/null +++ b/tests/Unit/ClassMap/ClassMapCollectionTest.php @@ -0,0 +1,39 @@ + $item1, + '{uri:xx:double}' => $item2, + ], iterator_to_array($classMap)); + } + + public function test_it_can_add_types(): void + { + $classMap = new ClassMapCollection(); + $classMap->set($item1 = new ClassMap('uri:xx', 'wsdlType', 'phpType')); + $classMap->set($item2 = new ClassMap('uri:xx', 'wsdlType', 'phpType')); + + static::assertSame([ + '{uri:xx:wsdltype}' => $item2, + ], iterator_to_array($classMap)); + } +} diff --git a/tests/Unit/ClassMap/ClassMapTest.php b/tests/Unit/ClassMap/ClassMapTest.php new file mode 100644 index 0000000..ee48d67 --- /dev/null +++ b/tests/Unit/ClassMap/ClassMapTest.php @@ -0,0 +1,21 @@ +getXmlNamespace()); + static::assertSame('wsdlType', $classMap->getXmlType()); + static::assertSame('phpType', $classMap->getPhpClassName()); + } +}