From 200a9ed0d9b2acb4b366d825ab0cd76a10f47574 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= Date: Sun, 1 May 2022 00:00:16 +0200 Subject: [PATCH] Entity: cast scalar types when setting MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This way Entity stores its attributes using types as specified in the $casts. This results in: 1. Cleaner logic: data is always stored as expected. Not only read. 2. Fixing reliability of hasChanged() Without this change what seemed like setting already set (identical to read) value could actually change type and result in hasChanged() unexpectedly returning true. Ref: https://github.com/codeigniter4/CodeIgniter4/issues/5905 Signed-off-by: Rafał Miłecki --- system/Entity/Cast/BooleanCast.php | 8 ++++++++ system/Entity/Cast/FloatCast.php | 8 ++++++++ system/Entity/Cast/IntegerCast.php | 8 ++++++++ system/Entity/Cast/StringCast.php | 8 ++++++++ 4 files changed, 32 insertions(+) diff --git a/system/Entity/Cast/BooleanCast.php b/system/Entity/Cast/BooleanCast.php index f46f5f5326bb..f6851506492c 100644 --- a/system/Entity/Cast/BooleanCast.php +++ b/system/Entity/Cast/BooleanCast.php @@ -23,4 +23,12 @@ public static function get($value, array $params = []): bool { return (bool) $value; } + + /** + * {@inheritDoc} + */ + public static function set($value, array $params = []): bool + { + return (bool) $value; + } } diff --git a/system/Entity/Cast/FloatCast.php b/system/Entity/Cast/FloatCast.php index 78f87570f32c..43a166f308a5 100644 --- a/system/Entity/Cast/FloatCast.php +++ b/system/Entity/Cast/FloatCast.php @@ -23,4 +23,12 @@ public static function get($value, array $params = []): float { return (float) $value; } + + /** + * {@inheritDoc} + */ + public static function set($value, array $params = []): float + { + return (float) $value; + } } diff --git a/system/Entity/Cast/IntegerCast.php b/system/Entity/Cast/IntegerCast.php index b9357d7ed49b..a47242ce7958 100644 --- a/system/Entity/Cast/IntegerCast.php +++ b/system/Entity/Cast/IntegerCast.php @@ -23,4 +23,12 @@ public static function get($value, array $params = []): int { return (int) $value; } + + /** + * {@inheritDoc} + */ + public static function set($value, array $params = []): int + { + return (int) $value; + } } diff --git a/system/Entity/Cast/StringCast.php b/system/Entity/Cast/StringCast.php index 974567ccb62a..915158eaa32d 100644 --- a/system/Entity/Cast/StringCast.php +++ b/system/Entity/Cast/StringCast.php @@ -23,4 +23,12 @@ public static function get($value, array $params = []): string { return (string) $value; } + + /** + * {@inheritDoc} + */ + public static function set($value, array $params = []): string + { + return (string) $value; + } }