Skip to content

Commit

Permalink
Don't break with Model::shouldBeStrict()` enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-potts committed Apr 6, 2024
1 parent 7371aac commit 4a80312
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
composer.lock
.phpunit.result.cache
.php-cs-fixer.cache
.idea/
9 changes: 8 additions & 1 deletion src/VirtualColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Stancl\VirtualColumn;

use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Database\Eloquent\MissingAttributeException;
use Illuminate\Support\Facades\Crypt;

/**
Expand Down Expand Up @@ -42,7 +43,13 @@ protected function decodeVirtualColumn(): void
['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)) {
Expand Down
14 changes: 14 additions & 0 deletions tests/VirtualColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,20 @@ public function models_extending_a_parent_model_using_virtualcolumn_get_encoded_
$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 */
Expand Down

0 comments on commit 4a80312

Please sign in to comment.