Created
October 22, 2020 11:18
-
-
Save Sammyjo20/183191c0d5d6ad8818177bc759df074e to your computer and use it in GitHub Desktop.
EncryptedJson Cast
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Casts; | |
use Illuminate\Contracts\Database\Eloquent\CastsAttributes; | |
class EncryptedJson implements CastsAttributes | |
{ | |
/** | |
* Cast the given value. | |
* | |
* @param \Illuminate\Database\Eloquent\Model $model | |
* @param string $key | |
* @param mixed $value | |
* @param array $attributes | |
* @return mixed | |
*/ | |
public function get($model, $key, $value, $attributes) | |
{ | |
$decrypted = rescue(function () use ($value) { | |
return decrypt($value); | |
}); | |
return json_decode($decrypted, true); | |
} | |
/** | |
* Prepare the given value for storage. | |
* | |
* @param \Illuminate\Database\Eloquent\Model $model | |
* @param string $key | |
* @param mixed $value | |
* @param array $attributes | |
* @return mixed|string | |
*/ | |
public function set($model, $key, $value, $attributes) | |
{ | |
return encrypt(json_encode($value)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment