Passed
Push — main ( 8ecc9d...1ce003 )
by BRUNO
02:00
created

CrudBuilder::where()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace BMorais\Database;
4
5
/**
6
 * CLASS ABSTRACT QUERY
7
 * Basic class to make connection between the database and the application
8
 *
9
 * @author Bruno Morais <[email protected]>
10
 * @copyright MIT, bmorais.com
11
 * @package bmorais\database
12
 * @subpackage class
13
 * @access private
14
 */
15
16
class CrudBuilder
17
{
18
    use DatalayerTrait;
19
20
    public function select(string $fields = "*", array $paramns = []):self
21
    {
22
        $query = "SELECT {$fields} FROM {$this->getTable()} ";
23
        if (!empty($this->getTableAlias()))
24
            $query .= "AS {$this->getTableAlias()}";
25
        $this->add($query, $paramns);
26
        return $this;
27
    }
28
29
    public function insert(string $fields, array $paramns):self
30
    {
31
        $numparams = '';
32
        foreach ($paramns as $item) {
33
            $numparams .= ',?';
34
        }
35
        $numparams = substr($numparams, 1);
36
        $query = "INSERT INTO {$this->getTable()} ({$fields}) VALUES ({$numparams})";
37
        $this->add($query, $paramns);
38
        return $this;
39
    }
40
41
    public function update(string $fields, array $paramns):self
42
    {
43
        $fields_T = '';
44
        $atributos = explode(',', $fields);
45
46
        foreach ($atributos as $item) {
47
            $fields_T .= ", {$item} = ?";
48
        }
49
        $fields_T = substr($fields_T, 2);
50
        $query = "UPDATE {{$this->getTable()}} SET {$fields_T}";
51
        $this->add($query, $paramns);
52
        return $this;
53
    }
54
55
    public function delete(string $fields, array $paramns):self
0 ignored issues
show
Unused Code introduced by
The parameter $fields is not used and could be removed. ( Ignorable by Annotation )

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

55
    public function delete(/** @scrutinizer ignore-unused */ string $fields, array $paramns):self

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
56
    {
57
        $query = "DELETE FROM {$this->getTable()}";
58
        $this->add($query, $paramns);
59
        return $this;
60
    }
61
62
    public function query(string $texto, array $paramns): self
63
    {
64
        $this->add($texto, $paramns);
65
        return $this;
66
    }
67
68
    public function where(string $texto, array $paramns): self
69
    {
70
        $query = "WHERE {$texto} ";
71
        $this->add($query, $paramns);
72
        return $this;
73
    }
74
75
    public function andWhere(string $condition, array $paramns): self
76
    {
77
        $query = "AND {$condition} ";
78
        $this->add($query, $paramns);
79
        return $this;
80
    }
81
82
    public function orWhere(string $texto, array $paramns): self
83
    {
84
        $query = "OR {$texto} ";
85
        $this->add($query, $paramns);
86
        return $this;
87
    }
88
89
    public function orderBy(string $texto): self
90
    {
91
        $query = "ORDER BY {$texto} ";
92
        $this->add($query);
93
        return $this;
94
    }
95
96
    public function limit(string $texto): self
97
    {
98
        $query = "LIMIT {$texto} ";
99
        $this->add($query);
100
        return $this;
101
    }
102
103
    public function offset(string $texto): self
104
    {
105
        $query = "OFFSET {$texto} ";
106
        $this->add($query);
107
        return $this;
108
    }
109
110
    public function groupBy(string  $texto): self{
111
        $query = "GROUP BY {$texto} ";
112
        $this->add($query);
113
        return $this;
114
    }
115
116
    public function having(string $texto): self
117
    {
118
        $query = "HAVING {$texto} ";
119
        $this->add($query);
120
        return $this;
121
    }
122
123
    public function innerJoin(string $table, string $alias, string $codition): self
124
    {
125
        $query = "INNER JOIN {$table} AS {$alias} ON $codition ";
126
        $this->add($query);
127
        return $this;
128
    }
129
130
    public function leftJoin(string $table, string $alias, string $codition): self
131
    {
132
        $query = "LEFT JOIN {$table} AS {$alias} ON $codition ";
133
        $this->add($query);
134
        return $this;
135
    }
136
137
    public function rightJoin(string $table, string $alias, string $codition): self
138
    {
139
        $query = "RIGHT JOIN {$table} AS {$alias} ON $codition ";
140
        $this->add($query);
141
        return $this;
142
    }
143
144
    public function execute(): self
145
    {
146
        $this->executeSQL($this->query, $this->params);
147
        return $this;
148
    }
149
150
    public function debug()
151
    {
152
        return  $this->query.'<pre>'.print_r($this->params).'</pre>';
0 ignored issues
show
Bug introduced by
Are you sure print_r($this->params) of type string|true can be used in concatenation? ( Ignorable by Annotation )

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

152
        return  $this->query.'<pre>'./** @scrutinizer ignore-type */ print_r($this->params).'</pre>';
Loading history...
153
    }
154
155
    /**
156
     * @return false|string
157
     */
158
    public function lastInsertId(): ?string
159
    {
160
        return $this->lastId();
161
    }
162
163
    /**
164
     * @return string|null
165
     */
166
    public function getLogSQL(): ?string
167
    {
168
        return $this->logSQL ?? "";
169
    }
170
171
    private function add(string $text, array $params = [])
172
    {
173
        if (!empty($params))
174
            $this->params[] = $params;
175
        $this->query .= $text;
176
    }
177
178
}
179