Skip to content

Commit

Permalink
Merge pull request #4 from veewee/class-map
Browse files Browse the repository at this point in the history
Introduce a class-map concept (similar to ext-soap-engine)
  • Loading branch information
veewee authored Jun 10, 2024
2 parents 71eab03 + 984ca63 commit 1cb9521
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 1 deletion.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
43 changes: 43 additions & 0 deletions src/ClassMap/ClassMap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
declare(strict_types=1);

namespace Soap\Encoding\ClassMap;

final class ClassMap
{
/**
* @param non-empty-string $xmlNamespace
* @param non-empty-string $wsdlType
* @param class-string $phpClassName
*/
public function __construct(
private readonly string $xmlNamespace,
private readonly string $wsdlType,
private readonly string $phpClassName
) {
}

/**
* @return class-string
*/
public function getPhpClassName(): string
{
return $this->phpClassName;
}

/**
* @return non-empty-string
*/
public function getXmlType(): string
{
return $this->wsdlType;
}

/**
* @return non-empty-string
*/
public function getXmlNamespace(): string
{
return $this->xmlNamespace;
}
}
41 changes: 41 additions & 0 deletions src/ClassMap/ClassMapCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php declare(strict_types=1);

namespace Soap\Encoding\ClassMap;

use ArrayIterator;
use IteratorAggregate;
use Soap\Encoding\Formatter\QNameFormatter;

/**
* @implements IteratorAggregate<string, ClassMap>
*/
final class ClassMapCollection implements IteratorAggregate
{
/**
* @var array<string, ClassMap>
*/
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<string, ClassMap>
*/
public function getIterator(): ArrayIterator
{
return new ArrayIterator($this->classMaps);
}
}
14 changes: 14 additions & 0 deletions src/EncoderRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*
Expand Down
39 changes: 39 additions & 0 deletions tests/Unit/ClassMap/ClassMapCollectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
declare(strict_types=1);

namespace Soap\Encoding\Test\Unit\ClassMap;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Soap\Encoding\ClassMap\ClassMap;
use Soap\Encoding\ClassMap\ClassMapCollection;

#[CoversClass(ClassMapCollection::class)]
final class ClassMapCollectionTest extends TestCase
{
public function test_it_tests_class_maps(): void
{
$classMap = new ClassMapCollection(
$item1 = new ClassMap('uri:xx', 'wsdlType', 'phpType'),
new ClassMap('uri:xx', 'double', 'double'),
$item2 = new ClassMap('uri:xx', 'double', 'double'),
);

static::assertCount(2, $classMap);
static::assertSame([
'{uri:xx:wsdltype}' => $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));
}
}
21 changes: 21 additions & 0 deletions tests/Unit/ClassMap/ClassMapTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
declare(strict_types=1);

namespace Soap\Encoding\Test\Unit\ClassMap;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Soap\Encoding\ClassMap\ClassMap;

#[CoversClass(ClassMap::class)]
final class ClassMapTest extends TestCase
{
public function test_it_tests_class_maps(): void
{
$classMap = new ClassMap('uri://xx', 'wsdlType', 'phpType');

static::assertSame('uri://xx', $classMap->getXmlNamespace());
static::assertSame('wsdlType', $classMap->getXmlType());
static::assertSame('phpType', $classMap->getPhpClassName());
}
}

0 comments on commit 1cb9521

Please sign in to comment.