From e57d888309760ee8a65509037dacb66bd25859ea Mon Sep 17 00:00:00 2001 From: selenaman Date: Mon, 21 Jan 2019 11:06:27 +0200 Subject: [PATCH] Update Aescrypt.php Update for Laravel Framework 5.7.21 --- src/Aescrypt.php | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/Aescrypt.php b/src/Aescrypt.php index 3243541..cd0abb6 100644 --- a/src/Aescrypt.php +++ b/src/Aescrypt.php @@ -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. @@ -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 @@ -246,6 +270,6 @@ protected function getArrayableAttributes() */ public function getAttributes() { - return $this->doDecryptAttributes(parent::getAttributes()); + return $this->isEncryptable() ? $this->doDecryptAttributes(parent::getAttributes()) : parent::getAttributes(); } }