From e0a4047f2d42ff4a8ac29c39efaf822b9a21e20d Mon Sep 17 00:00:00 2001 From: Toon Verwerft Date: Thu, 6 Jun 2024 09:26:26 +0200 Subject: [PATCH] Only apply path building logic when something fails --- src/Encoder/ErrorHandlingEncoder.php | 32 ++++++++++++++++-------- tests/Unit/Encoder/ObjectEncoderTest.php | 19 +++++++++++--- 2 files changed, 37 insertions(+), 14 deletions(-) diff --git a/src/Encoder/ErrorHandlingEncoder.php b/src/Encoder/ErrorHandlingEncoder.php index bf1a198..8a6d9df 100644 --- a/src/Encoder/ErrorHandlingEncoder.php +++ b/src/Encoder/ErrorHandlingEncoder.php @@ -26,28 +26,38 @@ public function __construct( public function iso(Context $context): Iso { - $meta = $context->type->getMeta(); - $path = $meta->isSimple() - ->filter(static fn (bool $isSimple): bool => !$isSimple) - ->unwrapOr($context->type->getXmlTargetNodeName()); - $path = $meta->isAttribute() - ->map(static fn (bool $isAttribute): string => '@' . $path) - ->unwrapOr($path); $innerIso = $this->encoder->iso($context); + $buildPath = static function() use ($context): ?string { + $meta = $context->type->getMeta(); + $isElement = $meta->isElement()->unwrapOr(false); + $isAttribute = $meta->isAttribute()->unwrapOr(false); + + if (!$isElement && !$isAttribute) { + dd($context->type); + return null; + } + + $path = $context->type->getXmlTargetNodeName(); + if ($isAttribute) { + return '@' . $path; + } + + return $path; + }; return new Iso( - static function (mixed $value) use ($innerIso, $context, $path): mixed { + static function (mixed $value) use ($innerIso, $context, $buildPath): mixed { try { return $innerIso->to($value); } catch (Throwable $exception) { - throw EncodingException::encodingValue($value, $context->type, $exception, $path); + throw EncodingException::encodingValue($value, $context->type, $exception, $buildPath()); } }, - static function (mixed $value) use ($innerIso, $context, $path): mixed { + static function (mixed $value) use ($innerIso, $context, $buildPath): mixed { try { return $innerIso->from($value); } catch (Throwable $exception) { - throw EncodingException::decodingValue($value, $context->type, $exception, $path); + throw EncodingException::decodingValue($value, $context->type, $exception, $buildPath()); } } ); diff --git a/tests/Unit/Encoder/ObjectEncoderTest.php b/tests/Unit/Encoder/ObjectEncoderTest.php index 197d154..d0660e2 100644 --- a/tests/Unit/Encoder/ObjectEncoderTest.php +++ b/tests/Unit/Encoder/ObjectEncoderTest.php @@ -137,7 +137,12 @@ public static function buildTypes( ->withXmlTypeName('user') ->withXmlNamespace("https://test") ->withXmlNamespaceName('test') - ->withXmlTargetNodeName('user'), + ->withXmlTargetNodeName('user') + ->withMeta( + static fn (TypeMeta $meta): TypeMeta => $meta + ->withIsQualified(true) + ->withIsElement(true) + ), new PropertyCollection( new Property( 'active', @@ -162,7 +167,11 @@ public static function buildTypes( ->withXmlTargetNodeName('hat') ->withXmlNamespace('https://test') ->withXmlNamespaceName('test') - ->withMeta(static fn (TypeMeta $meta): TypeMeta => $meta->withIsQualified(true)) + ->withMeta( + static fn (TypeMeta $meta): TypeMeta => $meta + ->withIsQualified(true) + ->withIsElement(true) + ) ) ) ), @@ -172,7 +181,11 @@ public static function buildTypes( ->withXmlNamespace("https://test") ->withXmlNamespaceName('test') ->withXmlTargetNodeName('hat') - ->withMeta(static fn (TypeMeta $meta): TypeMeta => $meta->withIsQualified(true)), + ->withMeta( + static fn (TypeMeta $meta): TypeMeta => $meta + ->withIsQualified(true) + ->withIsElement(true) + ), new PropertyCollection( new Property( 'color',