Skip to content

Commit 65f9000

Browse files
authored
Handle encrypted columns which are set to null (#19)
* πŸ§ͺ Add a test for a null value * πŸ› Don't consider null types for decryption
1 parent 7371aac commit 65f9000

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

β€Žsrc/VirtualColumn.phpβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ protected function decodeVirtualColumn(): void
4545
foreach ($this->getAttribute(static::getDataColumn()) ?? [] as $key => $value) {
4646
$attributeHasEncryptedCastable = in_array(data_get($this->getCasts(), $key), $encryptedCastables);
4747

48-
if ($attributeHasEncryptedCastable && $this->valueEncrypted($value)) {
48+
if ($value && $attributeHasEncryptedCastable && $this->valueEncrypted($value)) {
4949
$this->attributes[$key] = $value;
5050
} else {
5151
$this->setAttribute($key, $value);

β€Žtests/VirtualColumnTest.phpβ€Ž

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,16 @@ public function encrypted_casts_work_with_virtual_column() {
143143
'json' => json_encode(['foo', 'bar']), // 'encrypted:json'
144144
'object' => (object) json_encode(['foo', 'bar']), // 'encrypted:object'
145145
'custom' => 'foo', // Custom castable – 'EncryptedCast::class'
146+
'null_value' => null, // 'encrypted'
146147
]);
147148

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

151-
$this->assertTrue($model->valueEncrypted($savedValue));
152-
$this->assertNotEquals($expectedValue, $savedValue);
152+
if ($savedValue !== null) {
153+
$this->assertTrue($model->valueEncrypted($savedValue));
154+
$this->assertNotEquals($expectedValue, $savedValue);
155+
}
153156

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

@@ -178,6 +181,7 @@ class MyModel extends ParentModel
178181
'json' => 'encrypted:json',
179182
'object' => 'encrypted:object',
180183
'custom' => EncryptedCast::class,
184+
'null_value' => 'encrypted',
181185
];
182186
}
183187

0 commit comments

Comments
Β (0)