Passed
Push — main ( ff7bb8...23065b )
by BRUNO
02:50
created

Crud::extractModel()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace BMorais\Database;
4
5
/**
6
 * CLASS ABSTRACT CRUD
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
abstract class Crud
17
{
18
    use DatalayerTrait;
19
20
    /**
21
     * @param string $fields
22
     * @param string $add
23
     * @param array|null $values
24
     * @param bool $returnModel
25
     * @param bool $debug
26
     * @return array|false|void
27
     */
28
    public function select(string $fields = '*', string $add = '', array $values = null, bool $returnModel = false, bool $debug = false)
29
    {
30
        if (strlen($add) > 0) {
31
            $add = ' ' . $add;
32
        }
33
34
        $sql = "SELECT {$fields} FROM {$this->getTableName()}";
35
36
        if (!empty($this->getTableAlias()))
37
            $sql .= " AS {$this->getTableAlias()}";
38
39
        $sql .= "{$add}";
40
41
        if ($debug) {
42
            echo $sql;
43
44
            return;
45
        }
46
        $this->executeSQL($sql, $values);
47
        if ($returnModel) {
48
            return $this->fetchArrayClass();
49
        } else {
50
            return $this->fetchArrayObj();
51
        }
52
    }
53
54
    /**
55
     * @param string $fields
56
     * @param array|null $values
57
     * @param bool $debug
58
     * @return bool|void
59
     */
60
    public function insert(string $fields, array $values = null, bool $debug = false)
61
    {
62
        $numparams = '';
63
        foreach ($values as $item) {
64
            $numparams .= ',?';
65
        }
66
        $numparams = substr($numparams, 1);
67
        $sql = "INSERT INTO {$this->getTableName()} ({$fields}) VALUES ({$numparams})";
68
        if ($debug) {
69
            echo $sql.'<pre>'.print_r($values).'</pre>';
0 ignored issues
show
Bug introduced by
Are you sure print_r($values) 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

69
            echo $sql.'<pre>'./** @scrutinizer ignore-type */ print_r($values).'</pre>';
Loading history...
70
            return;
71
        }
72
        $result = $this->executeSQL($sql, $values);
73
        return !empty($result);
74
75
    }
76
77
    /**
78
     * @param object $object
79
     * @return bool|null
80
     */
81
    public function insertObject(object $object)
82
    {
83
        $args = [];
84
        $params = [];
85
        foreach ($object as $chave => $valor) {
86
            if ($valor != null) {
87
                $args[] = $chave;
88
                $params[] = $valor;
89
            }
90
        }
91
        $args = implode(',', $args);
92
93
        return $this->insert($args, $params);
94
    }
95
96
    /**
97
     * @param array $params
98
     * @return bool
99
     */
100
    public function insertArray(array $params): bool
101
    {
102
        if (!empty($params)) {
103
            $query = "INSERT INTO {$this->getTableName()}";
104
            $values = [];
105
            $dataColumns = array_keys($params);
106
            if (isset($dataColumns[0])) {
107
                $query .= ' (`' . implode('`, `', $dataColumns) . '`) ';
108
            }
109
            $query .= ' VALUES (';
110
111
            foreach ($dataColumns as $index => $column) {
112
                $values[] = $params[$column];
113
                $query .= '?, ';
114
            }
115
116
            $query = rtrim($query, ', ');
117
            $query .= ')';
118
119
            $result = $this->executeSQL($query, $values);
120
            if (empty($result)) {
121
                return false;
122
            }
123
124
            return true;
125
        } else {
126
            return false;
127
        }
128
    }
129
130
    /**
131
     * @param string $fields
132
     * @param array|null $values
133
     * @param string|null $where
134
     * @param bool $debug
135
     * @return bool|void
136
     */
137
    public function update(string $fields, array $values = null, string $where = null, bool $debug = false)
138
    {
139
        $fields_T = '';
140
        $atributos = explode(',', $fields);
141
142
        foreach ($atributos as $item) {
143
            $fields_T .= ", {$item} = ?";
144
        }
145
        $fields_T = substr($fields_T, 2);
146
        $sql = "UPDATE {$this->getTableName()} SET {$fields_T}";
147
        if (isset($where)) {
148
            $sql .= " WHERE $where";
149
        }
150
        if ($debug) {
151
            echo $sql.'<pre>'.print_r($values).'</pre>';
0 ignored issues
show
Bug introduced by
Are you sure print_r($values) 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

151
            echo $sql.'<pre>'./** @scrutinizer ignore-type */ print_r($values).'</pre>';
Loading history...
152
            return;
153
        }
154
        $result = $this->executeSQL($sql, $values);
155
        return !empty($result);
156
    }
157
158
159
    /**
160
     * @param object $object
161
     * @param string $where
162
     * @return bool|null
163
     */
164
    public function updateObject(object $object, string $where): ?bool
165
    {
166
        $args = [];
167
        $params = [];
168
        foreach ($object as $chave => $valor) {
169
            if ($valor != null) {
170
                $args[] = $chave;
171
                $params[] = $valor;
172
            }
173
        }
174
        $args = implode(',', $args);
175
176
        return $this->update($args, $params, $where);
177
    }
178
179
    /**
180
     * @param array $params
181
     * @param string $where
182
     * @return bool
183
     */
184
185
    public function updateArray(array $params, string $where): bool
186
    {
187
        if (!empty($params)) {
188
            $query = "UPDATE {$this->getTableName()} SET";
189
            $values = [];
190
191
            foreach ($params as $index => $column) {
192
                $query .= " {$index} = ?, ";
193
                $values[] = $params[$index];
194
            }
195
            $query = rtrim($query, ', ');
196
197
            if (!empty($where)) {
198
                $query .= " WHERE {$where}";
199
            }
200
201
            $result = $this->executeSQL($query, $values);
202
            if (empty($result)) {
203
                return false;
204
            }
205
206
            return true;
207
        } else {
208
            return false;
209
        }
210
    }
211
212
    /**
213
     * @param array|null $values
214
     * @param string|null $where
215
     * @param bool $debug
216
     * @return bool|void
217
     */
218
219
    public function delete(array $values = null, string $where = null, bool $debug = false)
220
    {
221
        $sql = "DELETE FROM {$this->getTableName()}";
222
        if (!empty($where)) {
223
            $sql .= " WHERE $where";
224
        }
225
        if ($debug) {
226
            echo $sql.'<pre>'.print_r($values).'</pre>';
0 ignored issues
show
Bug introduced by
Are you sure print_r($values) 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

226
            echo $sql.'<pre>'./** @scrutinizer ignore-type */ print_r($values).'</pre>';
Loading history...
227
            return;
228
        }
229
        $result = $this->executeSQL($sql, $values);
230
        return !empty($result);
231
232
    }
233
234
    /**
235
     * @return false|string
236
     */
237
    public function lastInsertId(): ?string
238
    {
239
        return $this->lastId();
240
    }
241
242
    /**
243
     * @return string|null
244
     */
245
    public function getLogSQL(): ?string
246
    {
247
        return $this->logSQL ?? "";
248
    }
249
250
}
251