From e65d0fff63501bc102854d44a8f2ccb20e93e3a6 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Mon, 5 Aug 2024 09:38:20 +0400 Subject: [PATCH] feat(core): Added `Cast::toNullable()`. --- packages/core/src/Utils/Cast.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/packages/core/src/Utils/Cast.php b/packages/core/src/Utils/Cast.php index 66a5fbf22..72a1b386b 100644 --- a/packages/core/src/Utils/Cast.php +++ b/packages/core/src/Utils/Cast.php @@ -112,4 +112,17 @@ public static function to(string $class, mixed $value): object { return $value; } + + /** + * @template T of object + * + * @param class-string $class + * + * @return ?T + */ + public static function toNullable(string $class, mixed $value): ?object { + assert($value === null || $value instanceof $class); + + return $value; + } }