Skip to content

Commit 15bd102

Browse files
committed
Introduce a class-map concept (similar to ext-soap-engine)
1 parent 71eab03 commit 15bd102

File tree

6 files changed

+165
-1
lines changed

6 files changed

+165
-1
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,18 @@ However, you can configure how the encoding should be done by adding your own en
7777
Some examples:
7878

7979
```php
80-
use Soap\Encoding\Encoder\SimpleType\DateTimeTypeEncoder;use Soap\Encoding\EncoderRegistry;
80+
use Soap\Encoding\ClassMap\ClassMap;
81+
use Soap\Encoding\ClassMap\ClassMapCollection;
82+
use Soap\Encoding\Encoder\SimpleType\DateTimeTypeEncoder;
83+
use Soap\Encoding\EncoderRegistry;
8184
use Soap\Xml\Xmlns;
8285

8386
EncoderRegistry::default()
8487
->addClassMap('urn:namespace', 'TypeA', TypeA::class)
8588
->addClassMap('urn:namespace', 'TypeB', TypeB::class)
89+
->addClassMapCollection(new ClassMapCollection(
90+
new ClassMap('urn:namespace', 'TypeC', TypeC::class),
91+
))
8692
->addBackedEnum('urn:namespace', 'EnumA', EnumA::class)
8793
->addSimpleTypeConverter(Xmlns::xsd()->value(), 'dateTime', new DateTimeTypeEncoder('Y-m-d\TH:i:s'))
8894
->addComplexTypeConverter('urn:namespace', 'TypeC', MySpecificTypeCEncoder::class);

src/ClassMap/ClassMap.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Soap\Encoding\ClassMap;
5+
6+
final class ClassMap
7+
{
8+
/**
9+
* @param non-empty-string $xmlNamespace
10+
* @param non-empty-string $wsdlType
11+
* @param class-string $phpClassName
12+
*/
13+
public function __construct(
14+
private readonly string $xmlNamespace,
15+
private readonly string $wsdlType,
16+
private readonly string $phpClassName
17+
)
18+
{}
19+
20+
/**
21+
* @return class-string
22+
*/
23+
public function getPhpClassName(): string
24+
{
25+
return $this->phpClassName;
26+
}
27+
28+
/**
29+
* @return non-empty-string
30+
*/
31+
public function getXmlType(): string
32+
{
33+
return $this->wsdlType;
34+
}
35+
36+
/**
37+
* @return non-empty-string
38+
*/
39+
public function getXmlNamespace(): string
40+
{
41+
return $this->xmlNamespace;
42+
}
43+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Soap\Encoding\ClassMap;
4+
5+
use ArrayIterator;
6+
use IteratorAggregate;
7+
use Soap\Encoding\Formatter\QNameFormatter;
8+
9+
/**
10+
* @implements IteratorAggregate<string, ClassMap>
11+
*/
12+
final class ClassMapCollection implements IteratorAggregate
13+
{
14+
/**
15+
* @var array<string, ClassMap>
16+
*/
17+
private array $classMaps = [];
18+
19+
public function __construct(ClassMap ... $classMaps)
20+
{
21+
foreach ($classMaps as $classMap) {
22+
$this->set($classMap);
23+
}
24+
}
25+
26+
public function set(ClassMap $classMap): self
27+
{
28+
$qname = (new QNameFormatter())($classMap->getXmlNamespace(), $classMap->getXmlType());
29+
$this->classMaps[$qname] = $classMap;
30+
31+
return $this;
32+
}
33+
34+
/**
35+
* @return ArrayIterator<string, ClassMap>
36+
*/
37+
public function getIterator(): ArrayIterator
38+
{
39+
return new ArrayIterator($this->classMaps);
40+
}
41+
}

src/EncoderRegistry.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace Soap\Encoding;
55

66
use Psl\Collection\MutableMap;
7+
use Soap\Encoding\ClassMap\ClassMapCollection;
78
use Soap\Encoding\Encoder\Context;
89
use Soap\Encoding\Encoder\ElementEncoder;
910
use Soap\Encoding\Encoder\EncoderDetector;
@@ -176,6 +177,19 @@ public function addClassMap(string $namespace, string $name, string $class): sel
176177
return $this;
177178
}
178179

180+
public function addClassMapCollection(ClassMapCollection $classMapCollection): self
181+
{
182+
foreach ($classMapCollection as $classMap) {
183+
$this->addClassMap(
184+
$classMap->getXmlNamespace(),
185+
$classMap->getXmlType(),
186+
$classMap->getPhpClassName()
187+
);
188+
}
189+
190+
return $this;
191+
}
192+
179193
/**
180194
* @template T of \BackedEnum
181195
*
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Soap\Encoding\Test\Unit\ClassMap;
5+
6+
use PHPUnit\Framework\Attributes\CoversClass;
7+
use PHPUnit\Framework\TestCase;
8+
use Soap\Encoding\ClassMap\ClassMap;
9+
use Soap\Encoding\ClassMap\ClassMapCollection;
10+
11+
#[CoversClass(ClassMapCollection::class)]
12+
final class ClassMapCollectionTest extends TestCase
13+
{
14+
public function test_it_tests_class_maps(): void
15+
{
16+
$classMap = new ClassMapCollection(
17+
$item1 = new ClassMap('uri:xx', 'wsdlType', 'phpType'),
18+
new ClassMap('uri:xx', 'double', 'double'),
19+
$item2 = new ClassMap('uri:xx', 'double', 'double'),
20+
);
21+
22+
static::assertCount(2, $classMap);
23+
static::assertSame([
24+
'{uri:xx:wsdltype}' => $item1,
25+
'{uri:xx:double}' => $item2,
26+
], iterator_to_array($classMap));
27+
}
28+
29+
public function test_it_can_add_types(): void
30+
{
31+
$classMap = new ClassMapCollection();
32+
$classMap->set($item1 = new ClassMap('uri:xx', 'wsdlType', 'phpType'));
33+
$classMap->set($item2 = new ClassMap('uri:xx', 'wsdlType', 'phpType'));
34+
35+
static::assertSame([
36+
'{uri:xx:wsdltype}' => $item2,
37+
], iterator_to_array($classMap));
38+
}
39+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Soap\Encoding\Test\Unit\ClassMap;
5+
6+
use PHPUnit\Framework\Attributes\CoversClass;
7+
use PHPUnit\Framework\TestCase;
8+
use Soap\Encoding\ClassMap\ClassMap;
9+
10+
#[CoversClass(ClassMap::class)]
11+
final class ClassMapTest extends TestCase
12+
{
13+
public function test_it_tests_class_maps(): void
14+
{
15+
$classMap = new ClassMap('uri://xx', 'wsdlType', 'phpType');
16+
17+
static::assertSame('uri://xx', $classMap->getXmlNamespace());
18+
static::assertSame('wsdlType', $classMap->getXmlType());
19+
static::assertSame('phpType', $classMap->getPhpClassName());
20+
}
21+
}

0 commit comments

Comments
 (0)