Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/Aescrypt.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ protected function shouldEncrypt($key)

return in_array($key, $encrypt);
}

/**
* Determine whether a model is ready for encryption.
*
* @return bool
*/
protected function isEncryptable(): bool
{
$exists = property_exists($this, 'exists');
return $exists === false || ($exists === true && $this->exists === true);
}

/**
* Determine whether a string has already been encrypted.
Expand Down Expand Up @@ -181,6 +192,19 @@ public function doDecryptAttributes($attributes)

return $attributes;
}

/**
* Decrypt encrypted data before it is processed by cast attribute.
*
* @param $key
* @param $value
*
* @return mixed
*/
protected function castAttribute($key, $value)
{
return parent::castAttribute($key, $this->doDecryptAttribute($key, $value));
}

//
// Methods below here override methods within the base Laravel/Illuminate/Eloquent
Expand Down Expand Up @@ -246,6 +270,6 @@ protected function getArrayableAttributes()
*/
public function getAttributes()
{
return $this->doDecryptAttributes(parent::getAttributes());
return $this->isEncryptable() ? $this->doDecryptAttributes(parent::getAttributes()) : parent::getAttributes();
}
}