From a737a263bd4957cb0cd3910f6f461e448ad8d866 Mon Sep 17 00:00:00 2001 From: Toon Verwerft Date: Wed, 24 Jul 2024 14:23:35 +0200 Subject: [PATCH] Accept nillable values in object encoder --- src/Encoder/ObjectEncoder.php | 4 +- .../Implied/ImpliedSchema001Test.php | 54 +++++++++++++++++++ .../Implied/ImpliedSchema002Test.php | 54 +++++++++++++++++++ .../Implied/ImpliedSchema003Test.php | 53 ++++++++++++++++++ tests/PhpCompatibility/Schema031Test.php | 10 +--- tests/PhpCompatibility/Schema049Test.php | 15 ++---- 6 files changed, 170 insertions(+), 20 deletions(-) create mode 100644 tests/PhpCompatibility/Implied/ImpliedSchema001Test.php create mode 100644 tests/PhpCompatibility/Implied/ImpliedSchema002Test.php create mode 100644 tests/PhpCompatibility/Implied/ImpliedSchema003Test.php diff --git a/src/Encoder/ObjectEncoder.php b/src/Encoder/ObjectEncoder.php index f1e0599..8a7f62b 100644 --- a/src/Encoder/ObjectEncoder.php +++ b/src/Encoder/ObjectEncoder.php @@ -106,10 +106,10 @@ static function (string $normalizePropertyName, Property $property) use ($object $type, $iso->to($value) ))(...) : $defaultAction, - $property->getName() === '_' => $value + $property->getName() === '_' => $value !== null ? buildValue($iso->to($value)) : (new NilAttributeBuilder())(...), - default => $value ? raw($iso->to($value)) : $defaultAction + default => raw($iso->to($value)) }; } ) diff --git a/tests/PhpCompatibility/Implied/ImpliedSchema001Test.php b/tests/PhpCompatibility/Implied/ImpliedSchema001Test.php new file mode 100644 index 0000000..e6c39df --- /dev/null +++ b/tests/PhpCompatibility/Implied/ImpliedSchema001Test.php @@ -0,0 +1,54 @@ + + + + + + + + + EOXML; + protected string $type = 'type="tns:testType"'; + + protected function calculateParam(): mixed + { + return (object)[ + 'customerId' => '123', + 'countryCode' => null, + ]; + } + + protected function expectXml(): string + { + return << + + + + 123 + + + + + + XML; + } +} diff --git a/tests/PhpCompatibility/Implied/ImpliedSchema002Test.php b/tests/PhpCompatibility/Implied/ImpliedSchema002Test.php new file mode 100644 index 0000000..c072b7d --- /dev/null +++ b/tests/PhpCompatibility/Implied/ImpliedSchema002Test.php @@ -0,0 +1,54 @@ + + + + + + + + + EOXML; + protected string $type = 'type="tns:testType"'; + + protected function calculateParam(): mixed + { + return (object)[ + 'customerId' => '123', + 'countryCode' => 'BE', + ]; + } + + protected function expectXml(): string + { + return << + + + + 123 + BE + + + + + XML; + } +} diff --git a/tests/PhpCompatibility/Implied/ImpliedSchema003Test.php b/tests/PhpCompatibility/Implied/ImpliedSchema003Test.php new file mode 100644 index 0000000..534a15e --- /dev/null +++ b/tests/PhpCompatibility/Implied/ImpliedSchema003Test.php @@ -0,0 +1,53 @@ + + + + + + + + + EOXML; + protected string $type = 'type="tns:testType"'; + + protected function calculateParam(): mixed + { + return (object)[ + 'customerId' => '123', + 'countryCode' => null, + ]; + } + + protected function expectXml(): string + { + return << + + + + 123 + + + + + XML; + } +} diff --git a/tests/PhpCompatibility/Schema031Test.php b/tests/PhpCompatibility/Schema031Test.php index a7634d0..d3d3b4c 100644 --- a/tests/PhpCompatibility/Schema031Test.php +++ b/tests/PhpCompatibility/Schema031Test.php @@ -27,6 +27,7 @@ protected function calculateParam(): mixed { return (object)[ "int" => 123, + "str" => "str", ]; } @@ -40,18 +41,11 @@ protected function expectXml(): string 123 + str XML; } - - protected function expectDecoded(): mixed - { - return (object)[ - "int" => 123, - "str" => null, - ]; - } } diff --git a/tests/PhpCompatibility/Schema049Test.php b/tests/PhpCompatibility/Schema049Test.php index 49558b3..1b6a0b4 100644 --- a/tests/PhpCompatibility/Schema049Test.php +++ b/tests/PhpCompatibility/Schema049Test.php @@ -24,6 +24,7 @@ final class Schema049Test extends AbstractCompatibilityTests + @@ -35,7 +36,8 @@ final class Schema049Test extends AbstractCompatibilityTests protected function calculateParam(): mixed { return (object)[ - "int2" => 123, + "int" => 123, + "int2" => 456, ]; } @@ -48,19 +50,12 @@ protected function expectXml(): string - 123 + 123 + 456 XML; } - - protected function expectDecoded(): mixed - { - return (object)[ - "int2" => 123, - "int" => null, - ]; - } }