Passed
Push — master ( 561dbe...7fb23b )
by Sérgio Danilo
02:05 queued 13s
created

LightQueryBuilder::bootstrap()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
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 " . self::$table);
130
    }
131
132
    /**
133
     * @return int
134
     */
135
    public function lastId(): int
136
    {
137
        return Connection::getInstance()
138
                ->query('SELECT MAX(id) as maxId FROM ' . self::$table)
139
                ->fetch()
140
                ->maxId + 1;
141
    }
142
143
    /**
144
     * @param string $terms
145
     * @param string|null $param
146
     * @return $this
147
     */
148
    public function where(string $terms, ?string $param = null): self
149
    {
150
        if ($param) {
151
            $this->params = $param;
152
        }
153
154
        $this->terms = $terms;
155
        return $this->toQuery(" WHERE {$this->terms}");
156
    }
157
158
    /**
159
     * @param string $columns
160
     * @param string $search
161
     * @param bool $all
162
     * @return mixed
163
     */
164
    public function match(string $columns, string $search, bool $all = true)
165
    {
166
        $this->params = "s={$search}";
167
        return $this
168
            ->select("MATCH({$columns}) AGAINST(:s)")
169
            ->get($all);
170
    }
171
172
    /**
173
     * @return $this
174
     */
175
    public function and(string $query)
176
    {
177
        return $this->toQuery('AND ' . $query);
178
    }
179
180
    /**
181
     * @return $this
182
     */
183
    public function or(string $query)
184
    {
185
        return $this->toQuery('OR ' . $query);
186
    }
187
188
    /**
189
     * @param string $columns
190
     * @param string $table
191
     * @param $condition
192
     * @param string $type
193
     * @return LightQueryBuilder
194
     */
195
    public function join(string $columns, string $table, $condition, $type = self::INNER_JOIN)
196
    {
197
        return $this->select($columns)
198
            ->toQuery("{$type} JOIN {$table} ON {$condition}");
199
    }
200
201
    /**
202
     * @param string $firstValue
203
     * @param string $secondValue
204
     * @return LightQueryBuilder
205
     */
206
    public function between(string $firstValue, string $secondValue)
207
    {
208
        return $this->toQuery(" BETWEEN {$firstValue}")
209
            ->and($secondValue);
210
    }
211
212
    /**
213
     * @param string $partial
214
     * @return $this
215
     */
216
    public function toQuery(string $partial): self
217
    {
218
        $this->query = trim(preg_replace('[\s+]', ' ', $this->query . " {$partial}"));
219
        return $this;
220
    }
221
222
    /*-----------------------------------------------------------------------------------*/
223
224
    /**
225
     * @param bool $all
226
     * @return array|mixed|null
227
     */
228
    public function get(bool $all = false)
229
    {
230
        $class = $this->class ?? \stdClass::class;
231
        $crud = new CRUD();
232
        $crud->setQuery($this->query);
233
234
        if ($this->params) {
235
            $crud->setParams($this->params);
236
        }
237
238
        return $crud->read($class, $all);
239
    }
240
241
    /**
242
     * @param array $data
243
     * @param string $terms
244
     * @param $params
245
     * @return int|null
246
     */
247
    public function update(array $data, string $terms, $params)
248
    {
249
        $this->terms = $terms;
250
        $this->params = $params;
251
        return $this->crud()->update($data, $this->terms);
252
    }
253
254
    /**
255
     * @param array $data
256
     * @return int|null
257
     */
258
    public function create(array $data)
259
    {
260
        return $this->crud()->create($data);
261
    }
262
263
    /**
264
     * @param string $terms
265
     * @param string $param
266
     * @return bool
267
     */
268
    public function delete(string $terms, string $param): bool
269
    {
270
        $this->params = $param;
271
        $this->terms = $terms;
272
273
        return $this->crud()->delete($this->terms, $this->params);
274
    }
275
276
277
278
    /*-----------------------------------------------------------------------------------*/
279
280
    /**
281
     * @param string $key
282
     * @return int
283
     */
284
    public function count($key = 'id'): int
285
    {
286
        $crud = $this->crud();
287
288
        if ($this->params) {
289
            $crud->setParams($this->params);
290
        }
291
292
        if ($this->query) {
293
            $crud->setQuery($this->query);
294
        }
295
296
        return $crud->count($key);
297
    }
298
299
    /**
300
     * @param string $columnOrder
301
     * @return $this
302
     */
303
    public function order(string $columnOrder)
304
    {
305
        $this->order = $columnOrder;
306
        $this->toQuery(" ORDER BY {$columnOrder}");
307
        return $this;
308
    }
309
310
    /**
311
     * @param int $limit
312
     * @return $this
313
     */
314
    public function limit(int $limit)
315
    {
316
        $this->limit = $limit;
317
        $this->toQuery(" LIMIT {$this->limit}");
318
        return $this;
319
    }
320
321
    /**
322
     * @param int $offset
323
     * @return $this
324
     */
325
    public function offset(int $offset)
326
    {
327
        $this->offset = $offset;
328
        $this->toQuery(" OFFSET {$offset}");
329
        return $this;
330
    }
331
332
    /*-----------------------------------------------------------------------------------*/
333
334
    /**
335
     * @return CRUD
336
     */
337
    private function crud()
338
    {
339
        $crud = CRUD::bootstrap($this->config)
0 ignored issues
show
Bug introduced by
The method bootstrap() does not exist on ElePHPant\CRUD. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

339
        $crud = CRUD::/** @scrutinizer ignore-call */ bootstrap($this->config)

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.

Loading history...
340
            ->setTable($this->table)
341
            ->setQuery($this->query);
342
343
        if ($this->params) {
344
            $crud->setParams($this->params);
345
        }
346
347
        return $crud;
348
    }
349
350
}