mirror of
https://github.com/archtechx/virtualcolumn.git
synced 2025-12-12 23:14:04 +00:00
Use $dataEncoded bool instead of $dataEncodingStatus string
This commit is contained in:
parent
93ec843c4c
commit
2760126de7
2 changed files with 14 additions and 16 deletions
|
|
@ -23,14 +23,12 @@ trait VirtualColumn
|
||||||
* We need this property, because both created & saved event listeners
|
* We need this property, because both created & saved event listeners
|
||||||
* decode the data (to take precedence before other created & saved)
|
* decode the data (to take precedence before other created & saved)
|
||||||
* listeners, but we don't want the data to be decoded twice.
|
* listeners, but we don't want the data to be decoded twice.
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
*/
|
*/
|
||||||
public $dataEncodingStatus = 'decoded';
|
public bool $dataEncoded = false;
|
||||||
|
|
||||||
protected static function decodeVirtualColumn(self $model): void
|
protected static function decodeVirtualColumn(self $model): void
|
||||||
{
|
{
|
||||||
if ($model->dataEncodingStatus === 'decoded') {
|
if (! $model->dataEncoded) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -53,12 +51,12 @@ trait VirtualColumn
|
||||||
|
|
||||||
$model->setAttribute(static::getDataColumn(), null);
|
$model->setAttribute(static::getDataColumn(), null);
|
||||||
|
|
||||||
$model->dataEncodingStatus = 'decoded';
|
$model->dataEncoded = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static function encodeAttributes(self $model): void
|
protected static function encodeAttributes(self $model): void
|
||||||
{
|
{
|
||||||
if ($model->dataEncodingStatus === 'encoded') {
|
if ($model->dataEncoded) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -75,7 +73,7 @@ trait VirtualColumn
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$model->dataEncodingStatus = 'encoded';
|
$model->dataEncoded = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function valueEncrypted(string $value): bool
|
public static function valueEncrypted(string $value): bool
|
||||||
|
|
@ -93,7 +91,7 @@ trait VirtualColumn
|
||||||
{
|
{
|
||||||
static::registerAfterListener('retrieved', function ($model) {
|
static::registerAfterListener('retrieved', function ($model) {
|
||||||
// We always decode after model retrieval.
|
// We always decode after model retrieval.
|
||||||
$model->dataEncodingStatus = 'encoded';
|
$model->dataEncoded = true;
|
||||||
|
|
||||||
static::decodeVirtualColumn($model);
|
static::decodeVirtualColumn($model);
|
||||||
});
|
});
|
||||||
|
|
@ -106,7 +104,7 @@ trait VirtualColumn
|
||||||
|
|
||||||
protected function decodeIfEncoded()
|
protected function decodeIfEncoded()
|
||||||
{
|
{
|
||||||
if ($this->dataEncodingStatus === 'encoded') {
|
if ($this->dataEncoded) {
|
||||||
static::decodeVirtualColumn($this);
|
static::decodeVirtualColumn($this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -63,25 +63,25 @@ class VirtualColumnTest extends TestCase
|
||||||
public function model_is_always_decoded_when_accessed_by_user_event()
|
public function model_is_always_decoded_when_accessed_by_user_event()
|
||||||
{
|
{
|
||||||
MyModel::retrieved(function (MyModel $model) {
|
MyModel::retrieved(function (MyModel $model) {
|
||||||
$this->assertSame('decoded', $model->dataEncodingStatus);
|
$this->assertFalse($model->dataEncoded);
|
||||||
});
|
});
|
||||||
MyModel::saving(function (MyModel $model) {
|
MyModel::saving(function (MyModel $model) {
|
||||||
$this->assertSame('decoded', $model->dataEncodingStatus);
|
$this->assertFalse($model->dataEncoded);
|
||||||
});
|
});
|
||||||
MyModel::updating(function (MyModel $model) {
|
MyModel::updating(function (MyModel $model) {
|
||||||
$this->assertSame('decoded', $model->dataEncodingStatus);
|
$this->assertFalse($model->dataEncoded);
|
||||||
});
|
});
|
||||||
MyModel::creating(function (MyModel $model) {
|
MyModel::creating(function (MyModel $model) {
|
||||||
$this->assertSame('decoded', $model->dataEncodingStatus);
|
$this->assertFalse($model->dataEncoded);
|
||||||
});
|
});
|
||||||
MyModel::saved(function (MyModel $model) {
|
MyModel::saved(function (MyModel $model) {
|
||||||
$this->assertSame('decoded', $model->dataEncodingStatus);
|
$this->assertFalse($model->dataEncoded);
|
||||||
});
|
});
|
||||||
MyModel::updated(function (MyModel $model) {
|
MyModel::updated(function (MyModel $model) {
|
||||||
$this->assertSame('decoded', $model->dataEncodingStatus);
|
$this->assertFalse($model->dataEncoded);
|
||||||
});
|
});
|
||||||
MyModel::created(function (MyModel $model) {
|
MyModel::created(function (MyModel $model) {
|
||||||
$this->assertSame('decoded', $model->dataEncodingStatus);
|
$this->assertFalse($model->dataEncoded);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue