Skip to content

Commit a9610b6

Browse files
authored
Merge pull request #18 from veewee/object-null-values
Accept nillable values in object encoder
2 parents 39e438c + a737a26 commit a9610b6

File tree

6 files changed

+170
-20
lines changed

6 files changed

+170
-20
lines changed

src/Encoder/ObjectEncoder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,10 @@ static function (string $normalizePropertyName, Property $property) use ($object
106106
$type,
107107
$iso->to($value)
108108
))(...) : $defaultAction,
109-
$property->getName() === '_' => $value
109+
$property->getName() === '_' => $value !== null
110110
? buildValue($iso->to($value))
111111
: (new NilAttributeBuilder())(...),
112-
default => $value ? raw($iso->to($value)) : $defaultAction
112+
default => raw($iso->to($value))
113113
};
114114
}
115115
)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Soap\Encoding\Test\PhpCompatibility\Implied;
5+
6+
use PHPUnit\Framework\Attributes\CoversClass;
7+
use Soap\Encoding\Decoder;
8+
use Soap\Encoding\Driver;
9+
use Soap\Encoding\Encoder;
10+
use Soap\Encoding\Test\PhpCompatibility\AbstractCompatibilityTests;
11+
12+
#[CoversClass(Driver::class)]
13+
#[CoversClass(Encoder::class)]
14+
#[CoversClass(Decoder::class)]
15+
final class ImpliedSchema001Test extends AbstractCompatibilityTests
16+
{
17+
protected string $schema = <<<EOXML
18+
<element name="testType">
19+
<complexType>
20+
<sequence>
21+
<element name="customerId" type="xsd:string" />
22+
<element name="countryCode" type="xsd:string" nillable="true" />
23+
</sequence>
24+
</complexType>
25+
</element>
26+
EOXML;
27+
protected string $type = 'type="tns:testType"';
28+
29+
protected function calculateParam(): mixed
30+
{
31+
return (object)[
32+
'customerId' => '123',
33+
'countryCode' => null,
34+
];
35+
}
36+
37+
protected function expectXml(): string
38+
{
39+
return <<<XML
40+
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="http://test-uri/"
41+
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
42+
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
43+
<SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
44+
<tns:test>
45+
<testParam xsi:type="tns:testType">
46+
<customerId xsi:type="xsd:string">123</customerId>
47+
<countryCode xsi:nil="true" />
48+
</testParam>
49+
</tns:test>
50+
</SOAP-ENV:Body>
51+
</SOAP-ENV:Envelope>
52+
XML;
53+
}
54+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Soap\Encoding\Test\PhpCompatibility\Implied;
5+
6+
use PHPUnit\Framework\Attributes\CoversClass;
7+
use Soap\Encoding\Decoder;
8+
use Soap\Encoding\Driver;
9+
use Soap\Encoding\Encoder;
10+
use Soap\Encoding\Test\PhpCompatibility\AbstractCompatibilityTests;
11+
12+
#[CoversClass(Driver::class)]
13+
#[CoversClass(Encoder::class)]
14+
#[CoversClass(Decoder::class)]
15+
final class ImpliedSchema002Test extends AbstractCompatibilityTests
16+
{
17+
protected string $schema = <<<EOXML
18+
<element name="testType">
19+
<complexType>
20+
<sequence>
21+
<element name="customerId" type="xsd:string" />
22+
<element name="countryCode" type="xsd:string" nillable="true" />
23+
</sequence>
24+
</complexType>
25+
</element>
26+
EOXML;
27+
protected string $type = 'type="tns:testType"';
28+
29+
protected function calculateParam(): mixed
30+
{
31+
return (object)[
32+
'customerId' => '123',
33+
'countryCode' => 'BE',
34+
];
35+
}
36+
37+
protected function expectXml(): string
38+
{
39+
return <<<XML
40+
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="http://test-uri/"
41+
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
42+
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
43+
<SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
44+
<tns:test>
45+
<testParam xsi:type="tns:testType">
46+
<customerId xsi:type="xsd:string">123</customerId>
47+
<countryCode xsi:type="xsd:string">BE</countryCode>
48+
</testParam>
49+
</tns:test>
50+
</SOAP-ENV:Body>
51+
</SOAP-ENV:Envelope>
52+
XML;
53+
}
54+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Soap\Encoding\Test\PhpCompatibility\Implied;
5+
6+
use PHPUnit\Framework\Attributes\CoversClass;
7+
use Soap\Encoding\Decoder;
8+
use Soap\Encoding\Driver;
9+
use Soap\Encoding\Encoder;
10+
use Soap\Encoding\Test\PhpCompatibility\AbstractCompatibilityTests;
11+
12+
#[CoversClass(Driver::class)]
13+
#[CoversClass(Encoder::class)]
14+
#[CoversClass(Decoder::class)]
15+
final class ImpliedSchema003Test extends AbstractCompatibilityTests
16+
{
17+
protected string $schema = <<<EOXML
18+
<element name="testType">
19+
<complexType>
20+
<sequence>
21+
<element name="customerId" type="xsd:string" />
22+
<element name="countryCode" type="xsd:string" minOccurs="0" maxOccurs="1" />
23+
</sequence>
24+
</complexType>
25+
</element>
26+
EOXML;
27+
protected string $type = 'type="tns:testType"';
28+
29+
protected function calculateParam(): mixed
30+
{
31+
return (object)[
32+
'customerId' => '123',
33+
'countryCode' => null,
34+
];
35+
}
36+
37+
protected function expectXml(): string
38+
{
39+
return <<<XML
40+
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="http://test-uri/"
41+
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
42+
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
43+
<SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
44+
<tns:test>
45+
<testParam xsi:type="tns:testType">
46+
<customerId xsi:type="xsd:string">123</customerId>
47+
</testParam>
48+
</tns:test>
49+
</SOAP-ENV:Body>
50+
</SOAP-ENV:Envelope>
51+
XML;
52+
}
53+
}

tests/PhpCompatibility/Schema031Test.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ protected function calculateParam(): mixed
2727
{
2828
return (object)[
2929
"int" => 123,
30+
"str" => "str",
3031
];
3132
}
3233

@@ -40,18 +41,11 @@ protected function expectXml(): string
4041
<tns:test>
4142
<testParam xsi:type="tns:testType">
4243
<int xsi:type="xsd:int">123</int>
44+
<str xsi:type="xsd:string">str</str>
4345
</testParam>
4446
</tns:test>
4547
</SOAP-ENV:Body>
4648
</SOAP-ENV:Envelope>
4749
XML;
4850
}
49-
50-
protected function expectDecoded(): mixed
51-
{
52-
return (object)[
53-
"int" => 123,
54-
"str" => null,
55-
];
56-
}
5751
}

tests/PhpCompatibility/Schema049Test.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ final class Schema049Test extends AbstractCompatibilityTests
2424
<complexContent>
2525
<restriction base="tns:testType2">
2626
<sequence>
27+
<element name="int" type="int"/>
2728
<element name="int2" type="int"/>
2829
</sequence>
2930
</restriction>
@@ -35,7 +36,8 @@ final class Schema049Test extends AbstractCompatibilityTests
3536
protected function calculateParam(): mixed
3637
{
3738
return (object)[
38-
"int2" => 123,
39+
"int" => 123,
40+
"int2" => 456,
3941
];
4042
}
4143

@@ -48,19 +50,12 @@ protected function expectXml(): string
4850
<SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
4951
<tns:test>
5052
<testParam xsi:type="tns:testType">
51-
<int2 xsi:type="xsd:int">123</int2>
53+
<int xsi:type="xsd:int">123</int>
54+
<int2 xsi:type="xsd:int">456</int2>
5255
</testParam>
5356
</tns:test>
5457
</SOAP-ENV:Body>
5558
</SOAP-ENV:Envelope>
5659
XML;
5760
}
58-
59-
protected function expectDecoded(): mixed
60-
{
61-
return (object)[
62-
"int2" => 123,
63-
"int" => null,
64-
];
65-
}
6661
}

0 commit comments

Comments
 (0)