1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Omatech\Enigma\Database\Eloquent; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Omatech\Enigma\Enigma; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @mixin \Illuminate\Database\Query\Builder |
10
|
|
|
* @method whereRaw($sql, $bindings = [], $boolean = 'and') |
11
|
|
|
*/ |
12
|
|
|
class Builder extends \Illuminate\Database\Eloquent\Builder |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Add a basic where clause to the query. |
16
|
|
|
* |
17
|
|
|
* @param Closure|string|array $column |
18
|
|
|
* @param mixed $operator |
19
|
|
|
* @param mixed $value |
20
|
|
|
* @param string $boolean |
21
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
22
|
|
|
* @throws \ParagonIE\CipherSweet\Exception\BlindIndexNameCollisionException |
23
|
|
|
* @throws \ParagonIE\CipherSweet\Exception\BlindIndexNotFoundException |
24
|
|
|
* @throws \ParagonIE\CipherSweet\Exception\CryptoOperationException |
25
|
|
|
* @throws \SodiumException |
26
|
|
|
*/ |
27
|
52 |
|
public function where($column, $operator = null, $value = null, $boolean = 'and'): \Illuminate\Database\Eloquent\Builder |
28
|
|
|
{ |
29
|
52 |
|
$enigma = (array) $this->model->getEnigmaEncryptable(); |
30
|
|
|
|
31
|
|
|
if ( |
32
|
52 |
|
! $column instanceof Closure && |
33
|
52 |
|
in_array($column, $enigma, true) !== false |
34
|
|
|
) { |
35
|
20 |
|
[$value, $operator] = $this->query->prepareValueAndOperator( |
36
|
20 |
|
$value, |
37
|
|
|
$operator, |
38
|
20 |
|
func_num_args() === 2 |
39
|
|
|
); |
40
|
|
|
|
41
|
20 |
|
if ($value) { |
42
|
20 |
|
return $this->findByHash($column, $value); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
52 |
|
return parent::where($column, $operator, $value, $boolean); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Add an "or where" clause to the query. |
51
|
|
|
* |
52
|
|
|
* @param \Closure|array|string $column |
53
|
|
|
* @param mixed $operator |
54
|
|
|
* @param mixed $value |
55
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
56
|
|
|
* @throws \ParagonIE\CipherSweet\Exception\BlindIndexNameCollisionException |
57
|
|
|
* @throws \ParagonIE\CipherSweet\Exception\BlindIndexNotFoundException |
58
|
|
|
* @throws \ParagonIE\CipherSweet\Exception\CryptoOperationException |
59
|
|
|
* @throws \SodiumException |
60
|
|
|
*/ |
61
|
4 |
|
public function orWhere($column, $operator = null, $value = null): \Illuminate\Database\Eloquent\Builder |
62
|
|
|
{ |
63
|
4 |
|
$enigma = (array) $this->model->getEnigmaEncryptable(); |
64
|
|
|
|
65
|
|
|
if ( |
66
|
4 |
|
! $column instanceof Closure && |
67
|
4 |
|
in_array($column, $enigma, true) !== false |
68
|
|
|
) { |
69
|
4 |
|
[$value, $operator] = $this->query->prepareValueAndOperator( |
70
|
4 |
|
$value, |
71
|
|
|
$operator, |
72
|
4 |
|
func_num_args() === 2 |
73
|
|
|
); |
74
|
|
|
|
75
|
4 |
|
if ($value) { |
76
|
4 |
|
return $this->findByHash($column, $value, 'or'); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
4 |
|
return parent::orWhere($column, $operator, $value); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param $column |
85
|
|
|
* @param $value |
86
|
|
|
* @param string $boolean |
87
|
|
|
* @return Builder |
88
|
|
|
* @throws \ParagonIE\CipherSweet\Exception\BlindIndexNameCollisionException |
89
|
|
|
* @throws \ParagonIE\CipherSweet\Exception\BlindIndexNotFoundException |
90
|
|
|
* @throws \ParagonIE\CipherSweet\Exception\CryptoOperationException |
91
|
|
|
* @throws \SodiumException |
92
|
|
|
*/ |
93
|
20 |
|
private function findByHash($column, $value, $boolean = 'and'): self |
94
|
|
|
{ |
95
|
20 |
|
$ids = (new Enigma) |
96
|
20 |
|
->searchAsModel($this->getModel(), $column, $value); |
97
|
|
|
|
98
|
|
|
$closure = function (self $query) use ($ids) { |
99
|
20 |
|
$query->whereRaw("{$this->model->getTable()}.id IN ($ids)"); |
100
|
20 |
|
}; |
101
|
|
|
|
102
|
20 |
|
$boolean === 'and' |
103
|
20 |
|
? $this->where($closure) |
104
|
4 |
|
: $this->orWhere($closure); |
105
|
|
|
|
106
|
20 |
|
return $this; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|