From b337c8dc9a52ad0b2a8b7d090ec87dcf9838e215 Mon Sep 17 00:00:00 2001 From: Priyadi Iman Nurcahyo <1102197+priyadi@users.noreply.github.com> Date: Fri, 9 Feb 2024 00:41:42 +0700 Subject: [PATCH] feat(`ObjectToObjectMetadata`): Store the reason if the object cannot use proxy. --- CHANGELOG.md | 2 ++ .../ObjectToObjectMetadataFactory.php | 3 +- .../ObjectToObjectMetadata.php | 30 ++++++++++++++++++- .../Exception/ProxyNotSupportedException.php | 11 ++++++- 4 files changed, 43 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 16455243..8324c962 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,8 @@ ## 0.7.3 * test: Add tests for read-only targets. +* feat(`ObjectToObjectMetadata`): Store the reason if the object cannot use + proxy. ## 0.7.2 diff --git a/src/Transformer/ObjectToObjectMetadata/Implementation/ObjectToObjectMetadataFactory.php b/src/Transformer/ObjectToObjectMetadata/Implementation/ObjectToObjectMetadataFactory.php index f62bdcbd..b88dd153 100644 --- a/src/Transformer/ObjectToObjectMetadata/Implementation/ObjectToObjectMetadataFactory.php +++ b/src/Transformer/ObjectToObjectMetadata/Implementation/ObjectToObjectMetadataFactory.php @@ -347,7 +347,8 @@ public function createObjectToObjectMetadata( $objectToObjectMetadata = $objectToObjectMetadata ->withTargetProxy($proxySpecification, $skippedProperties); } catch (ProxyNotSupportedException $e) { - // do nothing + $objectToObjectMetadata = $objectToObjectMetadata + ->withReasonCannotUseProxy($e->getReason()); } return $objectToObjectMetadata; diff --git a/src/Transformer/ObjectToObjectMetadata/ObjectToObjectMetadata.php b/src/Transformer/ObjectToObjectMetadata/ObjectToObjectMetadata.php index d7f2fc95..71baaf1c 100644 --- a/src/Transformer/ObjectToObjectMetadata/ObjectToObjectMetadata.php +++ b/src/Transformer/ObjectToObjectMetadata/ObjectToObjectMetadata.php @@ -68,6 +68,7 @@ public function __construct( private ?string $targetProxyClass = null, private ?string $targetProxyCode = null, private array $targetProxySkippedProperties = [], + private ?string $cannotUseProxyReason = null, ) { $constructorPropertyMappings = []; $lazyPropertyMappings = []; @@ -116,7 +117,29 @@ public function withTargetProxy( $this->targetReadOnly, $proxySpecification->getClass(), $proxySpecification->getCode(), - $targetProxySkippedProperties + $targetProxySkippedProperties, + cannotUseProxyReason: null + ); + } + + public function withReasonCannotUseProxy( + string $reason + ): self { + return new self( + $this->sourceClass, + $this->targetClass, + $this->providedTargetClass, + $this->allPropertyMappings, + $this->instantiable, + $this->cloneable, + $this->initializableTargetPropertiesNotInSource, + $this->sourceModifiedTime, + $this->targetModifiedTime, + $this->targetReadOnly, + null, + null, + [], + cannotUseProxyReason: $reason, ); } @@ -259,4 +282,9 @@ public function isTargetReadOnly(): bool { return $this->targetReadOnly; } + + public function getCannotUseProxyReason(): ?string + { + return $this->cannotUseProxyReason; + } } diff --git a/src/Transformer/Proxy/Exception/ProxyNotSupportedException.php b/src/Transformer/Proxy/Exception/ProxyNotSupportedException.php index 99a39e01..31f8e858 100644 --- a/src/Transformer/Proxy/Exception/ProxyNotSupportedException.php +++ b/src/Transformer/Proxy/Exception/ProxyNotSupportedException.php @@ -18,7 +18,9 @@ class ProxyNotSupportedException extends RuntimeException { - public function __construct(ObjectToObjectMetadata $metadata, \Throwable $previous = null) + private string $reason; + + public function __construct(ObjectToObjectMetadata $metadata, \Throwable $previous) { parent::__construct( sprintf( @@ -28,5 +30,12 @@ public function __construct(ObjectToObjectMetadata $metadata, \Throwable $previo ), previous: $previous ); + + $this->reason = $previous->getMessage(); + } + + public function getReason(): string + { + return $this->reason; } }