1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Quantum PHP Framework |
5
|
|
|
* |
6
|
|
|
* An open source software development framework for PHP |
7
|
|
|
* |
8
|
|
|
* @package Quantum |
9
|
|
|
* @author Arman Ag. <[email protected]> |
10
|
|
|
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) |
11
|
|
|
* @link http://quantum.softberg.org/ |
12
|
|
|
* @since 2.9.7 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Quantum\Libraries\Database\Adapters\Sleekdb\Statements; |
16
|
|
|
|
17
|
|
|
use Quantum\Libraries\Database\Exceptions\DatabaseException; |
18
|
|
|
use Quantum\Libraries\Database\Contracts\DbalInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Trait Criteria |
22
|
|
|
* @package Quantum\Libraries\Database |
23
|
|
|
*/ |
24
|
|
|
trait Criteria |
25
|
|
|
{ |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @inheritDoc |
29
|
|
|
* @throws DatabaseException |
30
|
|
|
*/ |
31
|
|
|
public function criteria(string $column, string $operator, $value = null): DbalInterface |
32
|
|
|
{ |
33
|
|
|
if (!in_array($operator, $this->operators)) { |
34
|
|
|
throw DatabaseException::operatorNotSupported($operator); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$this->criterias[] = [$column, $operator, $this->sanitizeValue($value)]; |
|
|
|
|
38
|
|
|
|
39
|
|
|
return $this; |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @inheritDoc |
44
|
|
|
* @throws DatabaseException |
45
|
|
|
*/ |
46
|
|
|
public function criterias(...$criterias): DbalInterface |
47
|
|
|
{ |
48
|
|
|
foreach ($criterias as $criteria) { |
49
|
|
|
if (isset($criteria[0]) && is_array($criteria[0])) { |
50
|
|
|
$this->orCriteria($criteria); |
51
|
|
|
} else { |
52
|
|
|
$this->criteria(...$criteria); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $this; |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @inheritDoc |
61
|
|
|
* @throws DatabaseException |
62
|
|
|
*/ |
63
|
|
|
public function having(string $column, string $operator, string $value = null): DbalInterface |
64
|
|
|
{ |
65
|
|
|
if (!in_array($operator, $this->operators)) { |
66
|
|
|
throw DatabaseException::operatorNotSupported($operator); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$this->havings[] = [$column, $operator, $value]; |
|
|
|
|
70
|
|
|
|
71
|
|
|
return $this; |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @inheritDoc |
76
|
|
|
* @throws DatabaseException |
77
|
|
|
*/ |
78
|
|
|
public function isNull(string $column): DbalInterface |
79
|
|
|
{ |
80
|
|
|
return $this->criteria($column, '=', null); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @inheritDoc |
85
|
|
|
* @throws DatabaseException |
86
|
|
|
*/ |
87
|
|
|
public function isNotNull(string $column): DbalInterface |
88
|
|
|
{ |
89
|
|
|
return $this->criteria($column, '!=', null); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Adds one or more OR criteria in brackets |
94
|
|
|
* @param array $orCriterias |
95
|
|
|
* @throws DatabaseException |
96
|
|
|
*/ |
97
|
|
|
protected function orCriteria(array $orCriterias) |
98
|
|
|
{ |
99
|
|
|
foreach ($orCriterias as $index => $criteria) { |
100
|
|
|
$this->criteria(...$criteria); |
|
|
|
|
101
|
|
|
|
102
|
|
|
if ($index != array_key_last($orCriterias)) { |
103
|
|
|
$this->criterias[] = 'OR'; |
|
|
|
|
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param $value |
110
|
|
|
* @return mixed|string|null |
111
|
|
|
*/ |
112
|
|
|
protected function sanitizeValue($value) |
113
|
|
|
{ |
114
|
|
|
$escapeMap = [ |
115
|
|
|
'\\' => '\\\\', |
116
|
|
|
'.' => '\.', |
117
|
|
|
'^' => '\^', |
118
|
|
|
'$' => '\$', |
119
|
|
|
'*' => '\*', |
120
|
|
|
'+' => '\+', |
121
|
|
|
'?' => '\?', |
122
|
|
|
'(' => '\(', |
123
|
|
|
')' => '\)', |
124
|
|
|
'[' => '\[', |
125
|
|
|
']' => '\]', |
126
|
|
|
'{' => '\{', |
127
|
|
|
'}' => '\}', |
128
|
|
|
'|' => '\|' |
129
|
|
|
]; |
130
|
|
|
|
131
|
|
|
if (is_array($value)) { |
132
|
|
|
return array_map(function ($v) use ($escapeMap) { |
133
|
|
|
return is_string($v) ? strtr($v, $escapeMap) : $v; |
134
|
|
|
}, $value); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return is_string($value) ? strtr($value, $escapeMap) : $value; |
138
|
|
|
} |
139
|
|
|
} |