1
|
|
|
<?php |
2
|
|
|
namespace Godsgood33\Php_Db; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Class to create a where clause |
6
|
|
|
* |
7
|
|
|
* @author Ryan Prather <[email protected]> |
8
|
|
|
*/ |
9
|
|
|
class DBWhere |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Global to represent an IN statement (e.g. |
14
|
|
|
* WHERE field IN (1,2)) |
15
|
|
|
* |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
const IN = 'IN'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Global to represent a NOT IN statement (e.g. |
22
|
|
|
* WHERE field NOT IN (1,2)) |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
const NOT_IN = 'NOT IN'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Global to represent a BETWEEN statement (e.g. |
30
|
|
|
* WHERE field BETWEEN 1 and 2) |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
const BETWEEN = 'BETWEEN'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Global to represent a LIKE statement (e.g. |
38
|
|
|
* WHERE field LIKE '%value%') |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
const LIKE = 'LIKE'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Global to represent a NOT LIKE statement (e.g. |
46
|
|
|
* WHERE field NOT LIKE '%value%') |
47
|
|
|
* |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
const NOT_LIKE = 'NOT LIKE'; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Global to represent an IS statement (e.g. |
54
|
|
|
* WHERE field IS NULL) |
55
|
|
|
* |
56
|
|
|
* @var string |
57
|
|
|
*/ |
58
|
|
|
const IS = 'IS'; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Global to represent an IS NOT statement (e.g. |
62
|
|
|
* WHERE field IS NOT NULL) |
63
|
|
|
* |
64
|
|
|
* @var string |
65
|
|
|
*/ |
66
|
|
|
const IS_NOT = 'IS NOT'; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Array to store the necessary class variables |
70
|
|
|
* |
71
|
|
|
* @var array |
72
|
|
|
*/ |
73
|
|
|
protected $data = []; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Constructor |
77
|
|
|
* |
78
|
|
|
* @param string $field |
79
|
|
|
* @param mixed $value |
80
|
|
|
* @param string $operator |
81
|
|
|
*/ |
82
|
|
|
public function __construct($field = null, $value = null, $operator = '=') |
83
|
|
|
{ |
84
|
|
|
$this->data = [ |
85
|
|
|
'index' => 0, |
86
|
|
|
'field' => $field, |
87
|
|
|
'value' => $value, |
88
|
|
|
'low' => null, |
89
|
|
|
'high' => null, |
90
|
|
|
'escape' => true, |
91
|
|
|
'operator' => $operator, |
92
|
|
|
'sqlOperator' => 'AND', |
93
|
|
|
'backticks' => true, |
94
|
|
|
'openParen' => false, |
95
|
|
|
'closeParen' => false, |
96
|
|
|
'caseInsensitive' => false |
97
|
|
|
]; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Method to return the variables |
102
|
|
|
* |
103
|
|
|
* @param string $var |
104
|
|
|
* |
105
|
|
|
* @return mixed |
106
|
|
|
* |
107
|
|
|
* @throws InvalidArgumentException |
108
|
|
|
*/ |
109
|
|
|
public function __get($var) |
110
|
|
|
{ |
111
|
|
|
if(!in_array($var, [ |
112
|
|
|
'index', 'field', 'value', 'low', 'high', 'operator', 'backticks', |
113
|
|
|
'sqlOperator', 'escape', 'openParen', 'closeParen', 'caseInsensitive' |
114
|
|
|
])) { |
115
|
|
|
$trace = \debug_backtrace(); |
116
|
|
|
throw new \InvalidArgumentException("Property not allowed via __get(): $var in {$trace[0]['file']} on line {$trace[0]['line']}", E_USER_WARNING); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $this->data[$var]; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Method to set a variable |
124
|
|
|
* |
125
|
|
|
* @param string $name |
126
|
|
|
* @param mixed $value |
127
|
|
|
* |
128
|
|
|
* @throws InvalidArgumentException |
129
|
|
|
*/ |
130
|
|
|
public function __set($name, $value) |
131
|
|
|
{ |
132
|
|
|
if(!in_array($name, [ |
133
|
|
|
'index', 'field', 'value', 'low', 'high', 'operator', 'backticks', |
134
|
|
|
'sqlOperator', 'escape', 'openParen', 'closeParen', 'caseInsensitive' |
135
|
|
|
])) { |
136
|
|
|
$trace = \debug_backtrace(); |
137
|
|
|
throw new \InvalidArgumentException("Property not allowed via __set(): $name in {$trace[0]['file']} on line {$trace[0]['line']}", E_USER_WARNING); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$this->data[$name] = $value; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Method to parse where clauses |
145
|
|
|
*/ |
146
|
|
|
public function __toString() |
147
|
|
|
{ |
148
|
|
|
$ret = ''; |
149
|
|
|
|
150
|
|
|
if (is_null($this->data['field']) && isset($this->data['closeParen']) && $this->data['closeParen']) { |
151
|
|
|
$ret .= ")"; |
152
|
|
|
return $ret; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
switch ($this->data['operator']) { |
156
|
|
|
case self::BETWEEN: |
157
|
|
|
if (! isset($this->data['field']) || ! isset($this->data['low']) || ! isset($this->data['high'])) { |
158
|
|
|
return ''; |
159
|
|
|
} |
160
|
|
|
break; |
161
|
|
|
default: |
162
|
|
|
if (! $this->data['field']) { |
163
|
|
|
return ''; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
if ($this->data['openParen']) { |
168
|
|
|
$ret .= " ("; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
if (! $this->data['backticks']) { |
172
|
|
|
$field = $this->data['field']; |
173
|
|
|
} else { |
174
|
|
|
$field = "`{$this->data['field']}`"; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
if ($this->data['operator'] == self::IN || $this->data['operator'] == self::NOT_IN) { |
178
|
|
|
if (is_string($this->data['value'])) { |
179
|
|
|
$ret .= " {$field} {$this->data['operator']} " . (strpos($this->data['value'], '(') !== false ? $this->data['value'] : "({$this->data['value']})"); |
180
|
|
|
} elseif (is_array($this->data['value'])) { |
181
|
|
|
$ret .= " {$field} {$this->data['operator']} (" . implode(",", $this->data['value']) . ")"; |
182
|
|
|
} else { |
183
|
|
|
return ''; |
184
|
|
|
} |
185
|
|
|
} elseif ($this->data['operator'] == self::BETWEEN) { |
186
|
|
|
$ret .= " {$field} BETWEEN {$this->data['low']} AND {$this->data['high']}"; |
187
|
|
|
} else { |
188
|
|
|
$value = (is_null($this->data['value']) ? "NULL" : $this->data['value']); |
189
|
|
|
|
190
|
|
|
if ($this->data['caseInsensitive']) { |
191
|
|
|
$ret .= " LOWER({$field}) {$this->data['operator']} LOWER({$value})"; |
192
|
|
|
} else { |
193
|
|
|
$ret .= " {$field} {$this->data['operator']} {$value}"; |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
if (isset($this->data['closeParen']) && $this->data['closeParen']) { |
198
|
|
|
$ret .= ")"; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return $ret; |
202
|
|
|
} |
203
|
|
|
} |