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:
parent
7371aac2ab
commit
4a8031266f
3 changed files with 23 additions and 1 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -2,3 +2,4 @@
|
||||||
composer.lock
|
composer.lock
|
||||||
.phpunit.result.cache
|
.phpunit.result.cache
|
||||||
.php-cs-fixer.cache
|
.php-cs-fixer.cache
|
||||||
|
.idea/
|
||||||
|
|
@ -5,6 +5,7 @@ declare(strict_types=1);
|
||||||
namespace Stancl\VirtualColumn;
|
namespace Stancl\VirtualColumn;
|
||||||
|
|
||||||
use Illuminate\Contracts\Encryption\DecryptException;
|
use Illuminate\Contracts\Encryption\DecryptException;
|
||||||
|
use Illuminate\Database\Eloquent\MissingAttributeException;
|
||||||
use Illuminate\Support\Facades\Crypt;
|
use Illuminate\Support\Facades\Crypt;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -42,7 +43,13 @@ trait VirtualColumn
|
||||||
['encrypted', 'encrypted:array', 'encrypted:collection', 'encrypted:json', 'encrypted:object'], // Default encrypted castables
|
['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);
|
$attributeHasEncryptedCastable = in_array(data_get($this->getCasts(), $key), $encryptedCastables);
|
||||||
|
|
||||||
if ($attributeHasEncryptedCastable && $this->valueEncrypted($value)) {
|
if ($attributeHasEncryptedCastable && $this->valueEncrypted($value)) {
|
||||||
|
|
|
||||||
|
|
@ -128,6 +128,20 @@ class VirtualColumnTest extends TestCase
|
||||||
$this->assertSame($encodedBar->bar, 'bar');
|
$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?
|
// maybe add an explicit test that the saving() and updating() listeners don't run twice?
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue