@@ -50,7 +50,7 @@ public function getAttribute($key)
5050 }
5151
5252 if (in_array ($ key , $ this ->encrypted )) {
53- $ value = $ this ->aesDecrypt ($ value );
53+ return $ this ->aesDecrypt ($ value );
5454 }
5555
5656 return $ value ;
@@ -65,7 +65,7 @@ public function getAttribute($key)
6565 *
6666 * @return false|string
6767 */
68- protected function aesDecrypt ($ val , $ cypher = 'aes-128-ecb ' , $ mySqlKey = true )
68+ private function aesDecrypt ($ val , $ cypher = 'aes-128-ecb ' , $ mySqlKey = true )
6969 {
7070 $ secret = getenv ('ENCRYPTION_KEY ' );
7171
@@ -81,13 +81,14 @@ protected function aesDecrypt($val, $cypher = 'aes-128-ecb', $mySqlKey = true)
8181 *
8282 * @return string
8383 */
84- protected function generateMysqlAesKey ($ key )
84+ private function generateMysqlAesKey ($ key )
8585 {
8686 $ generatedKey = str_repeat (chr (0 ), 16 );
8787
8888 for ($ i = 0 , $ len = strlen ($ key ); $ i < $ len ; $ i ++) {
8989 $ generatedKey [$ i % 16 ] = $ generatedKey [$ i % 16 ] ^ $ key [$ i ];
9090 }
91+
9192 return $ generatedKey ;
9293 }
9394
@@ -117,7 +118,7 @@ public function setAttribute($key, $value)
117118 *
118119 * @return false|string
119120 */
120- protected function aesEncrypt ($ val , $ cypher = 'aes-128-ecb ' , $ mySqlKey = true )
121+ private function aesEncrypt ($ val , $ cypher = 'aes-128-ecb ' , $ mySqlKey = true )
121122 {
122123 $ secret = getenv ('ENCRYPTION_KEY ' );
123124
@@ -181,53 +182,67 @@ public function getAnonymizable()
181182 return $ this ->anonymizable ;
182183 }
183184
185+ /**
186+ * Anonymize model fields
187+ *
188+ * @param string|null $locale
189+ */
190+ public function anonymize ($ locale = null )
191+ {
192+ $ faker = Factory::create ($ locale ?? (getenv ('FAKER_LOCALE ' ) ?? Factory::DEFAULT_LOCALE ));
193+
194+ foreach ($ this ->anonymizable as $ field => $ type ) {
195+ if (in_array ($ field , $ this ->attributes )) {
196+ $ method = $ type [0 ];
197+
198+ if (count ($ type ) > 1 ) {
199+ $ this ->$ field = call_user_func ([$ faker , $ method ], array_slice ($ type , 1 ));
200+ } else {
201+ $ this ->$ field = $ faker ->$ method ;
202+ }
203+ }
204+ }
205+ }
206+
184207 /**
185208 * where for encrypted columns
186209 *
187210 * @param $query
188- * @param $field
211+ * @param $column
189212 * @param $value
190213 *
191214 * @return mixed
192215 */
193- public function scopeWhereEncrypted ($ query , $ field , $ value )
216+ public function scopeWhereEncrypted ($ query , $ column , $ value )
194217 {
195- return $ query ->whereRaw ('AES_DECRYPT( ' . $ field . ', " ' . getenv ("ENCRYPTION_KEY " ) . '") LIKE " ' . $ value . '" COLLATE utf8mb4_general_ci ' );
218+ return $ query ->whereRaw ('AES_DECRYPT( ' . $ column . ', " ' . getenv ("ENCRYPTION_KEY " ) . '") LIKE " ' . $ value . '" COLLATE utf8mb4_general_ci ' );
196219 }
197220
198221 /**
199222 * orWhere for encrypted columns
200223 *
201224 * @param $query
202- * @param $field
225+ * @param $column
203226 * @param $value
204227 *
205228 * @return mixed
206229 */
207- public function scopeOrWhereEncrypted ($ query , $ field , $ value )
230+ public function scopeOrWhereEncrypted ($ query , $ column , $ value )
208231 {
209- return $ query ->orWhereRaw ('AES_DECRYPT( ' . $ field . ', " ' . getenv ("ENCRYPTION_KEY " ) . '") LIKE " ' . $ value . '" COLLATE utf8mb4_general_ci ' );
232+ return $ query ->orWhereRaw ('AES_DECRYPT( ' . $ column . ', " ' . getenv ("ENCRYPTION_KEY " ) . '") LIKE " ' . $ value . '" COLLATE utf8mb4_general_ci ' );
210233 }
211234
212235 /**
213- * Anonymize model fields
236+ * orderBy for encrypted columns
214237 *
215- * @param string|null $locale
238+ * @param $query
239+ * @param $column
240+ * @param $direction
241+ *
242+ * @return mixed
216243 */
217- public function anonymize ( $ locale = null )
244+ public function scopeOrderByEncrypted ( $ query , $ column , $ direction )
218245 {
219- $ faker = Factory::create ($ locale ?? (getenv ('FAKER_LOCALE ' ) ?? Factory::DEFAULT_LOCALE ));
220-
221- foreach ($ this ->anonymizable as $ field => $ type ) {
222- if (in_array ($ field , $ this ->attributes )) {
223- $ method = $ type [0 ];
224-
225- if (count ($ type ) > 1 ) {
226- $ this ->$ field = call_user_func ([$ faker , $ method ], array_slice ($ type , 1 ));
227- } else {
228- $ this ->$ field = $ faker ->$ method ;
229- }
230- }
231- }
246+ return $ query ->orderByRaw ('AES_DECRYPT( ' . $ column . ', " ' . getenv ("ENCRYPTION_KEY " ) . '") ' . $ direction );
232247 }
233248}
0 commit comments