Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
42 / 42
Encryptable
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
5 / 5
16
100.00% covered (success)
100.00%
42 / 42
 restoreAfterHash
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
5 / 5
 boot
100.00% covered (success)
100.00%
1 / 1
5
100.00% covered (success)
100.00%
20 / 20
 scopeWhereHash
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
7 / 7
 scopeOrWhereHash
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
7 / 7
 insert
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
3 / 3
1<?php
2
3namespace Qmp\Laravel\GdprCryptData\Models;
4
5use Illuminate\Support\Facades\Crypt;
6use Qmp\Laravel\GdprCryptData\Hash\HashDb;
7
8trait Encryptable
9{
10    /**
11     * Restore model after hash
12     *
13     * @param $model
14     */
15    public static function restoreAfterHash($model)
16    {
17        $data = json_decode(Crypt::decrypt($model->data), true);
18
19        foreach ($model->encryptable as $field) {
20            $model->$field = isset($data[$field]) ? $data[$field] : null;
21        }
22
23        $model->data = $data;
24    }
25
26    /**
27     * Add Hash and encrypt informations
28     */
29    public static function boot()
30    {
31        parent::boot();
32
33        static::saving(function ($model) {
34
35            foreach ($model->encryptable as $field) {
36                if (!empty($model->$field)) {
37                    if (empty($model->data)) {
38                        $model->data = Crypt::encrypt(json_encode([]));
39                    }
40
41                    $data = is_array($model->data)
42                        ? $model->data
43                        : json_decode(Crypt::decrypt($model->data), true);
44
45                    $data[$field] = $model->$field;
46                    $model->data = Crypt::encrypt(json_encode($data));
47                    $model->$field = HashDb::make($model->$field);
48                }
49            }
50        });
51
52        static::saved(function ($model) {
53            self::restoreAfterHash($model);
54        });
55
56        static::retrieved(function ($model) {
57            self::restoreAfterHash($model);
58        });
59    }
60
61    /**
62     * Where with hashed value
63     *
64     * @param $query
65     * @param $attribute
66     * @param $operand
67     * @param null $value
68     */
69    public function scopeWhereHash($query, $attribute, $operand, $value = null)
70    {
71        if ($value === null) {
72            $value = $operand;
73            $operand = '=';
74        }
75
76        if (in_array($attribute, $this->encryptable)) {
77            $value = HashDb::make($value);
78        }
79
80        $query->where($attribute, $operand, $value);
81    }
82
83    /**
84     * orWhere with hashed value
85     *
86     * @param $query
87     * @param $attribute
88     * @param $operand
89     * @param null $value
90     */
91    public function scopeOrWhereHash($query, $attribute, $operand, $value = null)
92    {
93        if ($value === null) {
94            $value = $operand;
95            $operand = '=';
96        }
97
98        if (in_array($attribute, $this->encryptable)) {
99            $value = HashDb::make($value);
100        }
101
102        $query->orWhere($attribute, $operand, $value);
103    }
104
105    /**
106     * Undocumented function
107     *
108     * @param [type] $records
109     * @return void
110     */
111    public static function insert($records)
112    {
113        foreach ($records as $record) {
114            self::create($record);
115        }
116    }
117}