Skip to content

Commit

Permalink
Only apply path building logic when something fails
Browse files Browse the repository at this point in the history
  • Loading branch information
veewee committed Jun 6, 2024
1 parent bcdc859 commit e0a4047
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
32 changes: 21 additions & 11 deletions src/Encoder/ErrorHandlingEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
);
Expand Down
19 changes: 16 additions & 3 deletions tests/Unit/Encoder/ObjectEncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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)
)
)
)
),
Expand All @@ -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',
Expand Down

0 comments on commit e0a4047

Please sign in to comment.