From 0dfdc88664ba6ea0882838d390462136c8df5826 Mon Sep 17 00:00:00 2001 From: jdjfisher <43887886+jdjfisher@users.noreply.github.com> Date: Sun, 6 Nov 2022 11:08:33 +0000 Subject: [PATCH 1/2] feat: added support for encrypted eloquent casts --- src/Properties/ModelPropertyExtension.php | 5 +++++ tests/Application/app/User.php | 1 + .../migrations/2020_01_30_000000_create_users_table.php | 1 + tests/Features/Properties/ModelPropertyExtension.php | 6 ++++++ tests/Type/data/model-properties.php | 1 + 5 files changed, 14 insertions(+) diff --git a/src/Properties/ModelPropertyExtension.php b/src/Properties/ModelPropertyExtension.php index 8ab15f600..afaa03093 100644 --- a/src/Properties/ModelPropertyExtension.php +++ b/src/Properties/ModelPropertyExtension.php @@ -232,6 +232,11 @@ 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:'); + } + switch ($type) { case 'boolean': case 'bool': diff --git a/tests/Application/app/User.php b/tests/Application/app/User.php index 8eda4d443..7cd006159 100644 --- a/tests/Application/app/User.php +++ b/tests/Application/app/User.php @@ -46,6 +46,7 @@ class User extends Authenticatable 'meta' => 'array', 'blocked' => 'boolean', 'email_verified_at' => 'date', + 'allowed_ips' => 'encrypted:array', '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..ce1be28ee 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,7 @@ public function up(): void $table->timestamp('email_verified_at')->nullable(); $table->string('password'); $table->string('stringButInt'); + $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..423767266 100644 --- a/tests/Features/Properties/ModelPropertyExtension.php +++ b/tests/Features/Properties/ModelPropertyExtension.php @@ -158,6 +158,12 @@ public function testNullablePropertyWithCast(User $user): void $user->email_verified_at = null; } + /** @return array */ + public function testEncryptedArrayCast(User $user): array + { + return $user->allowed_ips; + } + /** @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..79c2a6b2a 100644 --- a/tests/Type/data/model-properties.php +++ b/tests/Type/data/model-properties.php @@ -9,3 +9,4 @@ assertType('int', $user->newStyleAttribute); assertType('int', $user->stringButInt); assertType('string', $user->email); +assertType('array', $user->allowed_ips); From 1a220a547ff0fec0c224d345bc3552ab2fcbf4c7 Mon Sep 17 00:00:00 2001 From: jdjfisher <43887886+jdjfisher@users.noreply.github.com> Date: Sun, 6 Nov 2022 11:11:15 +0000 Subject: [PATCH 2/2] feat: added support for parameterized eloquent casts --- CHANGELOG.md | 1 + src/Properties/ModelPropertyExtension.php | 4 ++++ tests/Application/app/User.php | 1 + .../migrations/2020_01_30_000000_create_users_table.php | 1 + tests/Features/Properties/ModelPropertyExtension.php | 5 +++++ tests/Type/data/model-properties.php | 1 + 6 files changed, 13 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 afaa03093..678a87bc1 100644 --- a/src/Properties/ModelPropertyExtension.php +++ b/src/Properties/ModelPropertyExtension.php @@ -237,12 +237,16 @@ private function castPropertiesType(Model $modelInstance): void $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 7cd006159..f4fcd25ed 100644 --- a/tests/Application/app/User.php +++ b/tests/Application/app/User.php @@ -47,6 +47,7 @@ class User extends Authenticatable '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 ce1be28ee..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,7 @@ 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'); diff --git a/tests/Features/Properties/ModelPropertyExtension.php b/tests/Features/Properties/ModelPropertyExtension.php index 423767266..cd7772b47 100644 --- a/tests/Features/Properties/ModelPropertyExtension.php +++ b/tests/Features/Properties/ModelPropertyExtension.php @@ -164,6 +164,11 @@ 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 79c2a6b2a..9d3739939 100644 --- a/tests/Type/data/model-properties.php +++ b/tests/Type/data/model-properties.php @@ -10,3 +10,4 @@ assertType('int', $user->stringButInt); assertType('string', $user->email); assertType('array', $user->allowed_ips); +assertType('string', $user->floatButRoundedDecimalString);