1
0
Fork 0
mirror of https://github.com/archtechx/virtualcolumn.git synced 2025-12-12 09:24:02 +00:00

Don't break with Model::shouldBeStrict()` enabled

This commit is contained in:
Nick Potts 2024-04-06 20:46:01 +08:00
parent 7371aac2ab
commit 4a8031266f
3 changed files with 23 additions and 1 deletions

1
.gitignore vendored
View file

@ -2,3 +2,4 @@
composer.lock
.phpunit.result.cache
.php-cs-fixer.cache
.idea/

View file

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Stancl\VirtualColumn;
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Database\Eloquent\MissingAttributeException;
use Illuminate\Support\Facades\Crypt;
/**
@ -42,7 +43,13 @@ trait VirtualColumn
['encrypted', 'encrypted:array', 'encrypted:collection', 'encrypted:json', 'encrypted:object'], // Default encrypted castables
);
foreach ($this->getAttribute(static::getDataColumn()) ?? [] as $key => $value) {
try {
$data = $this->getAttribute(static::getDataColumn()) ?? [];
} catch (MissingAttributeException) {
return;
}
foreach ($data as $key => $value) {
$attributeHasEncryptedCastable = in_array(data_get($this->getCasts(), $key), $encryptedCastables);
if ($attributeHasEncryptedCastable && $this->valueEncrypted($value)) {

View file

@ -128,6 +128,20 @@ class VirtualColumnTest extends TestCase
$this->assertSame($encodedBar->bar, 'bar');
}
/** @test */
public function decoding_works_with_strict_mode_enabled() {
FooModel::shouldBeStrict();
FooModel::create([
'id' => 1,
'foo' => 'bar'
]);
$id = FooModel::query()->pluck('id')->first();
$this->assertSame(1, $id);
}
// maybe add an explicit test that the saving() and updating() listeners don't run twice?
/** @test */