Crud::updateObject()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 6
nop 2
dl 0
loc 17
rs 9.9332
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
86
        // Filtrar propriedades do objeto que não são null
87
        foreach ($object as $chave => $valor) {
88
            if ($valor !== null) {  // Verifica explicitamente se o valor não é null
89
                $args[] = $chave;
90
                $params[] = $valor;
91
            }
92
        }
93
94
        // Se houver colunas a serem inseridas
95
        if (!empty($args)) {
96
            $columns = implode(',', $args);
97
            return $this->insert($columns, $params);
98
        }
99
100
        // Retorna falso se todos os valores forem null
101
        return false;
102
    }
103
104
    /**
105
     * @param array $params
106
     * @return bool
107
     */
108
    public function insertArray(array $object): bool
109
    {
110
        $args = [];
111
        $params = [];
112
        foreach ($object as $chave => $valor) {
113
            if ($valor !== null) {
114
                $args[] = $chave;
115
                $params[] = $valor;
116
            }
117
        }
118
119
        if (!empty($args)) {
120
            $args = implode(',', $args);
121
            return $this->insert($args, $params);
122
        }
123
124
        return false;
125
    }
126
127
    /**
128
     * @param string $fields
129
     * @param array|null $values
130
     * @param string|null $where
131
     * @param bool $debug
132
     * @return bool|void
133
     */
134
    public function update(string $fields, ?array $values = null, ?string $where = null, bool $debug = false)
135
    {
136
        $fields_T = '';
137
        $atributos = explode(',', $fields);
138
139
        foreach ($atributos as $item) {
140
            $fields_T .= ", {$item} = ?";
141
        }
142
        $fields_T = substr($fields_T, 2);
143
        $sql = "UPDATE {$this->getTableName()} SET {$fields_T}";
144
        if (isset($where)) {
145
            $sql .= " WHERE $where";
146
        }
147
        if ($debug) {
148
            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

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

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