1 | <?php |
||
2 | |||
3 | namespace ElePHPant; |
||
4 | |||
5 | /** |
||
6 | * Class LightQueryBuilder |
||
7 | * @package ElePHPant |
||
8 | */ |
||
9 | class LightQueryBuilder |
||
10 | { |
||
11 | /** |
||
12 | * INNER JOIN |
||
13 | */ |
||
14 | public const INNER_JOIN = 'INNER'; |
||
15 | /** |
||
16 | * LEFT JOIN |
||
17 | */ |
||
18 | public const LEFT_JOIN = 'LEFT'; |
||
19 | /** |
||
20 | * RIGHT JOIN |
||
21 | */ |
||
22 | public const RIGHT_JOIN = 'RIGHT'; |
||
23 | /** |
||
24 | * FULL JOIN |
||
25 | */ |
||
26 | public const FULL_JOIN = 'FULL'; |
||
27 | /** |
||
28 | * CROSS JOIN |
||
29 | */ |
||
30 | public const CROSS_JOIN = 'CROSS'; |
||
31 | |||
32 | /** |
||
33 | * @var |
||
34 | */ |
||
35 | private $table; |
||
36 | /** |
||
37 | * @var |
||
38 | */ |
||
39 | private $class; |
||
40 | /** |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $query = ''; |
||
44 | /** |
||
45 | * @var |
||
46 | */ |
||
47 | protected $params; |
||
48 | /** |
||
49 | * @var |
||
50 | */ |
||
51 | protected $terms; |
||
52 | /** |
||
53 | * @var |
||
54 | */ |
||
55 | protected $columns; |
||
56 | |||
57 | /** |
||
58 | * @var |
||
59 | */ |
||
60 | protected $order; |
||
61 | /** |
||
62 | * @var |
||
63 | */ |
||
64 | protected $limit; |
||
65 | /** |
||
66 | * @var |
||
67 | */ |
||
68 | protected $offset; |
||
69 | |||
70 | /** |
||
71 | * @var |
||
72 | */ |
||
73 | public $fail; |
||
74 | |||
75 | private array $config; |
||
76 | |||
77 | public function __construct(array $config) |
||
78 | { |
||
79 | $this->config = $config; |
||
80 | } |
||
81 | |||
82 | public static function bootstrap(array $config, string $table) |
||
83 | { |
||
84 | return (new self($config))->setTable($table); |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * @param string $table |
||
89 | */ |
||
90 | public function setTable(string $table):self |
||
91 | { |
||
92 | $this->table = $table; |
||
93 | return $this; |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * @param string $class |
||
98 | * @return $this |
||
99 | */ |
||
100 | public function setFetchClass(string $class) |
||
101 | { |
||
102 | $this->class = $class; |
||
103 | return $this; |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * @return string|null |
||
108 | */ |
||
109 | public function getQuery(): ?string |
||
110 | { |
||
111 | return $this->query; |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * @return mixed |
||
116 | */ |
||
117 | public function getFail() |
||
118 | { |
||
119 | return $this->fail; |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * @param string $columns |
||
124 | * @return $this |
||
125 | */ |
||
126 | public function select($columns = '*') |
||
127 | { |
||
128 | $this->columns = $columns; |
||
129 | return $this->toQuery("SELECT {$this->columns} FROM " . $this->table); |
||
130 | } |
||
131 | |||
132 | |||
133 | /** |
||
134 | * @param string $terms |
||
135 | * @param string|null $param |
||
136 | * @return $this |
||
137 | */ |
||
138 | public function where(string $terms, ?string $param = null): self |
||
139 | { |
||
140 | if ($param) { |
||
141 | $this->params = $param; |
||
142 | } |
||
143 | |||
144 | $this->terms = $terms; |
||
145 | return $this->toQuery(" WHERE {$this->terms}"); |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * @param string $columns |
||
150 | * @param string $search |
||
151 | * @param bool $all |
||
152 | * @return mixed |
||
153 | */ |
||
154 | public function match(string $columns, string $search, bool $all = true) |
||
155 | { |
||
156 | $this->params = "s={$search}"; |
||
157 | return $this |
||
158 | ->select("MATCH({$columns}) AGAINST(:s)") |
||
159 | ->get($all); |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * @return $this |
||
164 | */ |
||
165 | public function and(string $query) |
||
166 | { |
||
167 | return $this->toQuery('AND ' . $query); |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * @return $this |
||
172 | */ |
||
173 | public function or(string $query) |
||
174 | { |
||
175 | return $this->toQuery('OR ' . $query); |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * @param string $columns |
||
180 | * @param string $table |
||
181 | * @param $condition |
||
182 | * @param string $type |
||
183 | * @return LightQueryBuilder |
||
184 | */ |
||
185 | public function join(string $columns, string $table, $condition, $type = self::INNER_JOIN) |
||
186 | { |
||
187 | return $this->select($columns) |
||
188 | ->toQuery("{$type} JOIN {$table} ON {$condition}"); |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * @param string $firstValue |
||
193 | * @param string $secondValue |
||
194 | * @return LightQueryBuilder |
||
195 | */ |
||
196 | public function between(string $firstValue, string $secondValue) |
||
197 | { |
||
198 | return $this->toQuery(" BETWEEN {$firstValue}") |
||
199 | ->and($secondValue); |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * @param string $partial |
||
204 | * @return $this |
||
205 | */ |
||
206 | public function toQuery(string $partial): self |
||
207 | { |
||
208 | $this->query = trim(preg_replace('[\s+]', ' ', $this->query . " {$partial}")); |
||
209 | return $this; |
||
210 | } |
||
211 | |||
212 | /*-----------------------------------------------------------------------------------*/ |
||
213 | |||
214 | /** |
||
215 | * @param bool $all |
||
216 | * @return array|mixed|null |
||
217 | */ |
||
218 | public function get(bool $all = false) |
||
219 | { |
||
220 | $class = $this->class ?? \stdClass::class; |
||
221 | $crud = $this->crud()->setQuery($this->query); |
||
222 | |||
223 | if ($this->params) { |
||
224 | $crud->setParams($this->params); |
||
225 | } |
||
226 | |||
227 | return $crud->read($class, $all); |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * @param array $data |
||
232 | * @param string $terms |
||
233 | * @param $params |
||
234 | * @return int|null |
||
235 | */ |
||
236 | public function update(array $data, string $terms, $params) |
||
237 | { |
||
238 | $this->terms = $terms; |
||
239 | $this->params = $params; |
||
240 | return $this->crud()->update($data, $this->terms); |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * @param array $data |
||
245 | * @return int|null |
||
246 | */ |
||
247 | public function create(array $data) |
||
248 | { |
||
249 | return $this->crud()->create($data); |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * @param string $terms |
||
254 | * @param string $param |
||
255 | * @return bool |
||
256 | */ |
||
257 | public function delete(string $terms, string $param): bool |
||
258 | { |
||
259 | $this->params = $param; |
||
260 | $this->terms = $terms; |
||
261 | |||
262 | return $this->crud()->delete($this->terms, $this->params); |
||
263 | } |
||
264 | |||
265 | |||
266 | |||
267 | /*-----------------------------------------------------------------------------------*/ |
||
268 | |||
269 | /** |
||
270 | * @param string $key |
||
271 | * @return int |
||
272 | */ |
||
273 | public function count($key = 'id'): int |
||
274 | { |
||
275 | $crud = $this->crud(); |
||
276 | |||
277 | if ($this->params) { |
||
278 | $crud->setParams($this->params); |
||
279 | } |
||
280 | |||
281 | if ($this->query) { |
||
282 | $crud->setQuery($this->query); |
||
283 | } |
||
284 | |||
285 | return $crud->count($key); |
||
286 | } |
||
287 | |||
288 | /** |
||
289 | * @param string $columnOrder |
||
290 | * @return $this |
||
291 | */ |
||
292 | public function order(string $columnOrder) |
||
293 | { |
||
294 | $this->order = $columnOrder; |
||
295 | $this->toQuery(" ORDER BY {$columnOrder}"); |
||
296 | return $this; |
||
297 | } |
||
298 | |||
299 | /** |
||
300 | * @param int $limit |
||
301 | * @return $this |
||
302 | */ |
||
303 | public function limit(int $limit) |
||
304 | { |
||
305 | $this->limit = $limit; |
||
306 | $this->toQuery(" LIMIT {$this->limit}"); |
||
307 | return $this; |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * @param int $offset |
||
312 | * @return $this |
||
313 | */ |
||
314 | public function offset(int $offset) |
||
315 | { |
||
316 | $this->offset = $offset; |
||
317 | $this->toQuery(" OFFSET {$offset}"); |
||
318 | return $this; |
||
319 | } |
||
320 | |||
321 | /*-----------------------------------------------------------------------------------*/ |
||
322 | |||
323 | /** |
||
324 | * @return CRUD |
||
325 | */ |
||
326 | private function crud() |
||
327 | { |
||
328 | $crud = CRUD::bootstrap($this->config) |
||
0 ignored issues
–
show
|
|||
329 | ->setTable($this->table) |
||
330 | ->setQuery($this->query); |
||
331 | |||
332 | if ($this->params) { |
||
333 | $crud->setParams($this->params); |
||
334 | } |
||
335 | |||
336 | return $crud; |
||
337 | } |
||
338 | |||
339 | protected function raw(string $query) |
||
340 | { |
||
341 | return CRUD::bootstrap($this->config)->setTable($this->table)->setQuery($query); |
||
342 | } |
||
343 | |||
344 | } |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.