From 4e5d5b4d09d50169fc4c67a4315a3a9a6978b980 Mon Sep 17 00:00:00 2001 From: Brian Faust Date: Thu, 17 Nov 2016 17:25:29 +0200 Subject: [PATCH] Add base64 Attribute Casting --- src/Illuminate/Database/Eloquent/Model.php | 28 ++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/Illuminate/Database/Eloquent/Model.php b/src/Illuminate/Database/Eloquent/Model.php index f1d0564aaeed..89608957e12c 100644 --- a/src/Illuminate/Database/Eloquent/Model.php +++ b/src/Illuminate/Database/Eloquent/Model.php @@ -2793,6 +2793,17 @@ protected function isEncryptCastable($key) return $this->hasCast($key, ['encrypted']); } + /** + * Determine whether a value should be encrypted. + * + * @param string $key + * @return bool + */ + protected function isBase64Castable($key) + { + return $this->hasCast($key, ['base64']); + } + /** * Determine whether a value is JSON castable for inbound manipulation. * @@ -2850,6 +2861,8 @@ protected function castAttribute($key, $value) return new BaseCollection($this->fromJson($value)); case 'encrypt': return decrypt($value); + case 'base64': + return base64_decode($value); case 'date': case 'datetime': return $this->asDateTime($value); @@ -2889,6 +2902,10 @@ public function setAttribute($key, $value) $value = $this->asEncrypted($value); } + if ($this->isBase64Castable($key) && ! is_null($value)) { + $value = $this->asBase64($value); + } + if ($this->isJsonCastable($key) && ! is_null($value)) { $value = $this->asJson($value); } @@ -3063,6 +3080,17 @@ protected function asEncrypted($value) return encrypt($value); } + /** + * Encode the given value. + * + * @param mixed $value + * @return string + */ + protected function asBase64($value) + { + return base64_encode($value); + } + /** * Encode the given value as JSON. *