1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Omatech\Enigma\Database\Query; |
4
|
|
|
|
5
|
|
|
use Omatech\Enigma\CipherSweet\Index; |
6
|
|
|
use Omatech\Enigma\Enigma; |
7
|
|
|
|
8
|
|
|
class Builder extends \Illuminate\Database\Query\Builder |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @param string $column |
12
|
|
|
* @param string $value |
13
|
|
|
* @param Index $index |
14
|
|
|
* @param string $boolean |
15
|
|
|
* @return $this |
16
|
|
|
* @throws \ParagonIE\CipherSweet\Exception\BlindIndexNameCollisionException |
17
|
|
|
* @throws \ParagonIE\CipherSweet\Exception\CryptoOperationException |
18
|
|
|
*/ |
19
|
8 |
|
public function whereEnigma(string $column, string $value, Index $index, string $boolean = 'and'): self |
20
|
|
|
{ |
21
|
8 |
|
$this->findByHash($column, $value, $index, $boolean); |
22
|
|
|
|
23
|
8 |
|
return $this; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param string $column |
28
|
|
|
* @param string $value |
29
|
|
|
* @param Index $index |
30
|
|
|
* @return $this |
31
|
|
|
* @throws \ParagonIE\CipherSweet\Exception\BlindIndexNameCollisionException |
32
|
|
|
* @throws \ParagonIE\CipherSweet\Exception\CryptoOperationException |
33
|
|
|
*/ |
34
|
4 |
|
public function orWhereEnigma(string $column, string $value, Index $index): self |
35
|
|
|
{ |
36
|
4 |
|
return $this->whereEnigma($column, $value, $index, 'or'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $column |
41
|
|
|
* @param string $value |
42
|
|
|
* @param Index $index |
43
|
|
|
* @param string $boolean |
44
|
|
|
* @return Builder |
45
|
|
|
* @throws \ParagonIE\CipherSweet\Exception\BlindIndexNameCollisionException |
46
|
|
|
* @throws \ParagonIE\CipherSweet\Exception\CryptoOperationException |
47
|
|
|
*/ |
48
|
8 |
|
private function findByHash(string $column, string $value, Index $index, string $boolean): self |
49
|
|
|
{ |
50
|
8 |
|
$table = $this->from; |
51
|
8 |
|
$tableColumn = explode('.', $column); |
52
|
|
|
|
53
|
8 |
|
if (count($tableColumn) === 2) { |
54
|
4 |
|
[$table, $column] = $tableColumn; |
55
|
|
|
} |
56
|
|
|
|
57
|
8 |
|
$parts = explode(' as ', $table); |
58
|
8 |
|
$table = $parts[0]; |
59
|
8 |
|
$alias = $parts[1] ?? null; |
60
|
|
|
|
61
|
8 |
|
$ids = (new Enigma)->search($table, $column, $value, $index); |
62
|
|
|
|
63
|
|
|
$closure = static function (self $query) use ($table, $alias, $ids) { |
64
|
8 |
|
$query->whereRaw(($alias ?? $table).".id IN ($ids)"); |
65
|
8 |
|
}; |
66
|
|
|
|
67
|
8 |
|
$boolean === 'and' |
68
|
8 |
|
? $this->where($closure) |
69
|
4 |
|
: $this->orWhere($closure); |
70
|
|
|
|
71
|
8 |
|
return $this; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|