1 | <?php namespace Magros\Encryptable; |
||
2 | |||
3 | trait Encryptable |
||
4 | { |
||
5 | |||
6 | public $aesKey = null; |
||
7 | public static $enableEncryption = true; |
||
8 | private $magrosEncrypter; |
||
9 | |||
10 | /** |
||
11 | * @return Encrypter |
||
12 | */ |
||
13 | public function encrypter() |
||
14 | { |
||
15 | if(! $this->magrosEncrypter){ |
||
16 | $this->magrosEncrypter = new Encrypter(); |
||
17 | } |
||
18 | return $this->magrosEncrypter; |
||
19 | } |
||
20 | |||
21 | /** |
||
22 | * @return mixed |
||
23 | */ |
||
24 | public function getEncryptableAttributes() |
||
25 | { |
||
26 | return $this->encryptable; |
||
27 | } |
||
28 | |||
29 | /** |
||
30 | * @param $key |
||
31 | * @return bool |
||
32 | */ |
||
33 | public function encryptable($key) |
||
34 | { |
||
35 | if(self::$enableEncryption){ |
||
36 | return in_array($key, $this->encryptable); |
||
37 | } |
||
38 | return false; |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * @param $key |
||
43 | * @return bool |
||
44 | */ |
||
45 | public function isCamelcase($key) |
||
46 | { |
||
47 | return isset($this->camelcase) && is_array($this->camelcase) && in_array($key, $this->camelcase); |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * Decrypt a value. |
||
52 | * |
||
53 | * @param $value |
||
54 | * |
||
55 | * @return string |
||
56 | * @throws \Exception |
||
57 | */ |
||
58 | public function decryptAttribute($value) |
||
59 | { |
||
60 | return $value ? $this->encrypter()->decrypt($value) : ''; |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * @param $value |
||
65 | * @return string |
||
66 | */ |
||
67 | public function camelCaseAttribute($value) |
||
68 | { |
||
69 | return ucwords($value); |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * @param $value |
||
74 | * @return string |
||
75 | * @throws \Exception |
||
76 | */ |
||
77 | public function encryptAttribute($value) |
||
78 | { |
||
79 | return $value ? $this->encrypter()->encrypt(strtolower($value)) : ''; |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * @param $key |
||
84 | * @return string |
||
85 | * @throws \Exception |
||
86 | */ |
||
87 | public function getAttribute($key) |
||
88 | { |
||
89 | $value = parent::getAttribute($key); |
||
90 | |||
91 | if ($this->encryptable($key)) { |
||
92 | $value = $this->decryptAttribute($value); |
||
93 | } |
||
94 | if ($this->isCamelcase($key)){ |
||
95 | $value = $this->camelCaseAttribute($value); |
||
96 | } |
||
97 | |||
98 | |||
99 | return $value; |
||
100 | } |
||
101 | |||
102 | |||
103 | /** |
||
104 | * @param $key |
||
105 | * @param $value |
||
106 | * @return mixed |
||
107 | * @throws \Exception |
||
108 | */ |
||
109 | public function setAttribute($key, $value) |
||
110 | { |
||
111 | if ($this->encryptable($key)) { |
||
112 | $value = $this->encryptAttribute($value); |
||
113 | } |
||
114 | |||
115 | return parent::setAttribute($key, $value); |
||
116 | } |
||
117 | |||
118 | |||
119 | /** |
||
120 | * @return mixed |
||
121 | * @throws \Exception |
||
122 | */ |
||
123 | public function getArrayableAttributes() |
||
124 | { |
||
125 | $attributes = parent::getArrayableAttributes(); |
||
126 | |||
127 | foreach ($attributes as $key => $attribute) { |
||
128 | if ($this->encryptable($key)) { |
||
129 | $attributes[$key] = $this->decryptAttribute($attribute); |
||
130 | } |
||
131 | if ($this->isCamelcase($key)){ |
||
132 | $attributes[$key] = $this->camelCaseAttribute($attributes[$key]); |
||
133 | } |
||
134 | } |
||
135 | |||
136 | return $attributes; |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * @return EncryptableQueryBuilder |
||
141 | */ |
||
142 | public function newBaseQueryBuilder() |
||
143 | { |
||
144 | $connection = $this->getConnection(); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
145 | |||
146 | return new EncryptableQueryBuilder($connection, $this); |
||
147 | } |
||
148 | } |