-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncryptable.php
More file actions
43 lines (36 loc) · 846 Bytes
/
Encryptable.php
File metadata and controls
43 lines (36 loc) · 846 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
namespace KDuma\Eloquent;
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Support\Facades\Crypt;
/**
* Class Encryptable.
*/
trait Encryptable
{
/**
* @param $key
* @return mixed
*/
public function getAttribute($key)
{
$value = parent::getAttribute($key);
try {
if (array_key_exists($key, array_flip($this->encrypted))) {
$value = Crypt::decrypt($value);
}
} catch (DecryptException $e) {
}
return $value;
}
/**
* @param $key
* @param $value
*/
public function setAttribute($key, $value)
{
if (array_key_exists($key, array_flip($this->encrypted))) {
$value = Crypt::encrypt($value);
}
return parent::setAttribute($key, $value);
}
}