From 446b328137adf4121a0b34187c3a2d275db20ca5 Mon Sep 17 00:00:00 2001 From: Serene Rajkarnikar Date: Tue, 8 Sep 2015 15:02:25 +0100 Subject: [PATCH 1/3] Removing redundant variable from update method --- src/Dtisgodsson/Elocrypt/ElocryptTrait.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Dtisgodsson/Elocrypt/ElocryptTrait.php b/src/Dtisgodsson/Elocrypt/ElocryptTrait.php index c163fd4..54b0e4c 100644 --- a/src/Dtisgodsson/Elocrypt/ElocryptTrait.php +++ b/src/Dtisgodsson/Elocrypt/ElocryptTrait.php @@ -70,7 +70,7 @@ public function update(Array $attributes = []) { if(!isset($this->encryptable)) { - return parent::update($id, $attributes); + return parent::update($attributes); } foreach ($attributes as $key => $value) { From b6d296ae6f88f959678c690bf559ea1cc9bf4878 Mon Sep 17 00:00:00 2001 From: Serene Rajkarnikar Date: Tue, 8 Sep 2015 15:05:35 +0100 Subject: [PATCH 2/3] Adding method newInstance to Elocrypt --- src/Dtisgodsson/Elocrypt/ElocryptTrait.php | 49 +++++++++++++++++++++- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/src/Dtisgodsson/Elocrypt/ElocryptTrait.php b/src/Dtisgodsson/Elocrypt/ElocryptTrait.php index 54b0e4c..349b14f 100644 --- a/src/Dtisgodsson/Elocrypt/ElocryptTrait.php +++ b/src/Dtisgodsson/Elocrypt/ElocryptTrait.php @@ -39,7 +39,13 @@ public function __get($key) return $value; } - public static function create(Array $attributes = []) + /** + * Save a new model and return the instance. + * + * @param array $attributes + * @return static + */ + public static function create(array $attributes) { $model = new static; @@ -66,7 +72,13 @@ public static function create(Array $attributes = []) return parent::create($attributes); } - public function update(Array $attributes = []) + /** + * Update the model in the database. + * + * @param array $attributes + * @return bool|int + */ + public function update(array $attributes = array()) { if(!isset($this->encryptable)) { @@ -91,4 +103,37 @@ public function update(Array $attributes = []) return parent::update($attributes); } + + /** + * Create a new instance of the given model. + * + * @param array $attributes + * @param bool $exists + * @return static + */ + public function newInstance($attributes = array(), $exists = false) + { + if(!isset($this->encryptable)) + { + return parent::newInstance($attributes, $exists); + } + + foreach ($attributes as $key => $value) { + + if(in_array($key, $this->encryptable)) + { + try + { + $decrypted = Crypt::decrypt($value); + $attributes[$key] = $value; + } + catch(DecryptException $exception) + { + $attributes[$key] = Crypt::encrypt($value); + } + } + } + + return parent::newInstance($attributes, $exists); + } } \ No newline at end of file From 899e4d85df1548c2a8a8965ddbe0237e8868b1d8 Mon Sep 17 00:00:00 2001 From: Serene Rajkarnikar Date: Tue, 8 Sep 2015 15:58:07 +0100 Subject: [PATCH 3/3] Extend attributesToArray method to decrypt any encrypted fields --- src/Dtisgodsson/Elocrypt/ElocryptTrait.php | 31 ++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/Dtisgodsson/Elocrypt/ElocryptTrait.php b/src/Dtisgodsson/Elocrypt/ElocryptTrait.php index 349b14f..4a9066e 100644 --- a/src/Dtisgodsson/Elocrypt/ElocryptTrait.php +++ b/src/Dtisgodsson/Elocrypt/ElocryptTrait.php @@ -136,4 +136,35 @@ public function newInstance($attributes = array(), $exists = false) return parent::newInstance($attributes, $exists); } + + /** + * Convert the model's attributes to an array. + * + * @return array + */ + public function attributesToArray() + { + $attributes = parent::attributesToArray(); + + if(!isset($this->encryptable)) + { + return $attributes; + } + + foreach ($this->encryptable as $key) + { + if ( ! isset($attributes[$key])) continue; + + try + { + $attributes[$key] = Crypt::decrypt($attributes[$key]); + } + catch(DecryptException $exception) + { + //Do nothing, attribute already exists + } + } + + return $attributes; + } } \ No newline at end of file