From 055d68970f7533513e922d95dc0cc7aa2e3ac49b Mon Sep 17 00:00:00 2001 From: Jordan Fisher <43887886+jdjfisher@users.noreply.github.com> Date: Sun, 6 Nov 2022 20:57:39 +0000 Subject: [PATCH] feat: support for encrypted and parameterized casts (#1439) --- CHANGELOG.md | 1 + src/Properties/ModelPropertyExtension.php | 9 +++++++++ tests/Application/app/User.php | 2 ++ .../2020_01_30_000000_create_users_table.php | 2 ++ tests/Features/Properties/ModelPropertyExtension.php | 11 +++++++++++ tests/Type/data/model-properties.php | 2 ++ 6 files changed, 27 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 13a833154..28ab918db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - feat: Added stub for the DB::transaction method by @jdjfisher - feat: conditional return types for `Eloquent\Collection::find()` by @sebdesign - feat: add support for generic paginators by @erikgaal +- feat: add support for encrypted and parameterized eloquent casts by @jdjfisher ## [2.2.0] - 2022-08-31 diff --git a/src/Properties/ModelPropertyExtension.php b/src/Properties/ModelPropertyExtension.php index 8ab15f600..678a87bc1 100644 --- a/src/Properties/ModelPropertyExtension.php +++ b/src/Properties/ModelPropertyExtension.php @@ -232,12 +232,21 @@ private function castPropertiesType(Model $modelInstance): void continue; } + // Reduce encrypted castable types + if (in_array($type, ['encrypted', 'encrypted:array', 'encrypted:collection', 'encrypted:json', 'encrypted:object'], true)) { + $type = Str::after($type, 'encrypted:'); + } + + // Truncate cast parameters + $type = Str::before($type, ':'); + switch ($type) { case 'boolean': case 'bool': $realType = 'boolean'; break; case 'string': + case 'decimal': $realType = 'string'; break; case 'array': diff --git a/tests/Application/app/User.php b/tests/Application/app/User.php index 8eda4d443..f4fcd25ed 100644 --- a/tests/Application/app/User.php +++ b/tests/Application/app/User.php @@ -46,6 +46,8 @@ class User extends Authenticatable 'meta' => 'array', 'blocked' => 'boolean', 'email_verified_at' => 'date', + 'allowed_ips' => 'encrypted:array', + 'floatButRoundedDecimalString' => 'decimal:1', 'options' => AsArrayObject::class, 'properties' => AsCollection::class, ]; diff --git a/tests/Application/database/migrations/2020_01_30_000000_create_users_table.php b/tests/Application/database/migrations/2020_01_30_000000_create_users_table.php index 6517db5f0..0f3be9eea 100644 --- a/tests/Application/database/migrations/2020_01_30_000000_create_users_table.php +++ b/tests/Application/database/migrations/2020_01_30_000000_create_users_table.php @@ -22,6 +22,8 @@ public function up(): void $table->timestamp('email_verified_at')->nullable(); $table->string('password'); $table->string('stringButInt'); + $table->float('floatButRoundedDecimalString'); + $table->json('allowed_ips'); $table->json('meta'); $table->json('options'); $table->json('properties'); diff --git a/tests/Features/Properties/ModelPropertyExtension.php b/tests/Features/Properties/ModelPropertyExtension.php index 01d1d446f..cd7772b47 100644 --- a/tests/Features/Properties/ModelPropertyExtension.php +++ b/tests/Features/Properties/ModelPropertyExtension.php @@ -158,6 +158,17 @@ public function testNullablePropertyWithCast(User $user): void $user->email_verified_at = null; } + /** @return array */ + public function testEncryptedArrayCast(User $user): array + { + return $user->allowed_ips; + } + + public function testPrecisionDecimalCast(User $user): string + { + return $user->floatButRoundedDecimalString; + } + /** @return ArrayObject */ public function testAsArrayObjectCast(User $user): ArrayObject { diff --git a/tests/Type/data/model-properties.php b/tests/Type/data/model-properties.php index 892a96bb5..9d3739939 100644 --- a/tests/Type/data/model-properties.php +++ b/tests/Type/data/model-properties.php @@ -9,3 +9,5 @@ assertType('int', $user->newStyleAttribute); assertType('int', $user->stringButInt); assertType('string', $user->email); +assertType('array', $user->allowed_ips); +assertType('string', $user->floatButRoundedDecimalString);