From 638e8ca9c271dd28761bfff3efeed705f57b75c1 Mon Sep 17 00:00:00 2001 From: Priyadi Iman Nurcahyo <1102197+priyadi@users.noreply.github.com> Date: Sun, 14 Jan 2024 14:15:45 +0700 Subject: [PATCH] feat: Main transformer's exception now accepts `$context` variable. --- CHANGELOG.md | 1 + .../Exception/CannotFindTransformerException.php | 8 ++++++-- .../TransformerReturnsUnexpectedValueException.php | 5 +++-- src/MainTransformer/MainTransformer.php | 4 ++-- 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e9541c04..bbd3967f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ error messages. * feat: Context methods now accept `$context` variable, and pass it to exceptions. +* feat: Main transformer's exception now accepts `$context` variable. ## 0.5.8 diff --git a/src/MainTransformer/Exception/CannotFindTransformerException.php b/src/MainTransformer/Exception/CannotFindTransformerException.php index af0d7497..24420284 100644 --- a/src/MainTransformer/Exception/CannotFindTransformerException.php +++ b/src/MainTransformer/Exception/CannotFindTransformerException.php @@ -13,6 +13,7 @@ namespace Rekalogika\Mapper\MainTransformer\Exception; +use Rekalogika\Mapper\Context\Context; use Rekalogika\Mapper\Exception\UnexpectedValueException; use Rekalogika\Mapper\Transformer\Contracts\MixedType; use Rekalogika\Mapper\Util\TypeUtil; @@ -24,11 +25,14 @@ class CannotFindTransformerException extends UnexpectedValueException * @param array $sourceTypes * @param array $targetTypes */ - public function __construct(array $sourceTypes, array $targetTypes) + public function __construct(array $sourceTypes, array $targetTypes, Context $context) { $sourceTypes = TypeUtil::getDebugType($sourceTypes); $targetTypes = TypeUtil::getDebugType($targetTypes); - parent::__construct(sprintf('Cannot find a matching transformer for mapping the source types "%s" to the target types "%s"', $sourceTypes, $sourceTypes)); + parent::__construct( + sprintf('Cannot find a matching transformer for mapping the source types "%s" to the target types "%s"', $sourceTypes, $sourceTypes), + context: $context + ); } } diff --git a/src/MainTransformer/Exception/TransformerReturnsUnexpectedValueException.php b/src/MainTransformer/Exception/TransformerReturnsUnexpectedValueException.php index 6f0241d5..6e3a2071 100644 --- a/src/MainTransformer/Exception/TransformerReturnsUnexpectedValueException.php +++ b/src/MainTransformer/Exception/TransformerReturnsUnexpectedValueException.php @@ -13,6 +13,7 @@ namespace Rekalogika\Mapper\MainTransformer\Exception; +use Rekalogika\Mapper\Context\Context; use Rekalogika\Mapper\Exception\UnexpectedValueException; use Rekalogika\Mapper\Transformer\Contracts\MixedType; use Rekalogika\Mapper\Util\TypeUtil; @@ -20,7 +21,7 @@ class TransformerReturnsUnexpectedValueException extends UnexpectedValueException { - public function __construct(Type|MixedType $type, mixed $target) + public function __construct(Type|MixedType $type, mixed $target, Context $context) { $message = sprintf( 'Mapper returns unexpected value. Expected type "%s", but got "%s"', @@ -28,6 +29,6 @@ public function __construct(Type|MixedType $type, mixed $target) get_debug_type($target), ); - parent::__construct($message); + parent::__construct($message, context: $context); } } diff --git a/src/MainTransformer/MainTransformer.php b/src/MainTransformer/MainTransformer.php index f326192d..1673be5d 100644 --- a/src/MainTransformer/MainTransformer.php +++ b/src/MainTransformer/MainTransformer.php @@ -173,7 +173,7 @@ public function transform( ); if (!TypeCheck::isVariableInstanceOf($result, $targetType)) { - throw new TransformerReturnsUnexpectedValueException($targetType, $result); + throw new TransformerReturnsUnexpectedValueException($targetType, $result, context: $context); } // if the target type is not null, cache it @@ -191,6 +191,6 @@ public function transform( return $result; } - throw new CannotFindTransformerException($sourceTypes, $targetTypes); + throw new CannotFindTransformerException($sourceTypes, $targetTypes, context: $context); } }