Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support for encrypted and parameterized casts #1439

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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, ':');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking through the framework, it appears to be explicitly looking for decimal:, date:, datetime: etc for the built-in parameterized casts. That said, I think for the interest maintainibility here, we can reasonably assume that everything following the : is parameters and not part of the underlying cast 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);