Skip to content

Commit

Permalink
feat: support for encrypted and parameterized casts (#1439)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdjfisher committed Nov 6, 2022
1 parent 333e791 commit 055d689
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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

Expand Down
9 changes: 9 additions & 0 deletions src/Properties/ModelPropertyExtension.php
Expand Up @@ -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':
Expand Down
2 changes: 2 additions & 0 deletions tests/Application/app/User.php
Expand Up @@ -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,
];
Expand Down
Expand Up @@ -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');
Expand Down
11 changes: 11 additions & 0 deletions tests/Features/Properties/ModelPropertyExtension.php
Expand Up @@ -158,6 +158,17 @@ public function testNullablePropertyWithCast(User $user): void
$user->email_verified_at = null;
}

/** @return array<string> */
public function testEncryptedArrayCast(User $user): array
{
return $user->allowed_ips;
}

public function testPrecisionDecimalCast(User $user): string
{
return $user->floatButRoundedDecimalString;
}

/** @return ArrayObject<array-key, mixed> */
public function testAsArrayObjectCast(User $user): ArrayObject
{
Expand Down
2 changes: 2 additions & 0 deletions tests/Type/data/model-properties.php
Expand Up @@ -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);

0 comments on commit 055d689

Please sign in to comment.