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

Handle encrypted columns which are set to null #19

Merged
merged 2 commits into from
Apr 7, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/VirtualColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected function decodeVirtualColumn(): void
foreach ($this->getAttribute(static::getDataColumn()) ?? [] as $key => $value) {
$attributeHasEncryptedCastable = in_array(data_get($this->getCasts(), $key), $encryptedCastables);

if ($attributeHasEncryptedCastable && $this->valueEncrypted($value)) {
if ($value && $attributeHasEncryptedCastable && $this->valueEncrypted($value)) {
$this->attributes[$key] = $value;
} else {
$this->setAttribute($key, $value);
Expand Down
8 changes: 6 additions & 2 deletions tests/VirtualColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,16 @@ public function encrypted_casts_work_with_virtual_column() {
'json' => json_encode(['foo', 'bar']), // 'encrypted:json'
'object' => (object) json_encode(['foo', 'bar']), // 'encrypted:object'
'custom' => 'foo', // Custom castable – 'EncryptedCast::class'
'null_value' => null, // 'encrypted'
]);

foreach($encryptedAttributes as $key => $expectedValue) {
$savedValue = $model->getAttributes()[$key]; // Encrypted

$this->assertTrue($model->valueEncrypted($savedValue));
$this->assertNotEquals($expectedValue, $savedValue);
if ($savedValue !== null) {
$this->assertTrue($model->valueEncrypted($savedValue));
$this->assertNotEquals($expectedValue, $savedValue);
}

$retrievedValue = $model->$key; // Decrypted

Expand Down Expand Up @@ -178,6 +181,7 @@ class MyModel extends ParentModel
'json' => 'encrypted:json',
'object' => 'encrypted:object',
'custom' => EncryptedCast::class,
'null_value' => 'encrypted',
];
}

Expand Down
Loading