Passed
Push — main ( 0f5d47...9a2714 )
by BRUNO
02:00
created

Crud::insertObject()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 1
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
namespace BMorais\Database;
3
4
/**
5
 * CLASSE CRUD
6
 * Classe abastrada para fazer ligação entre o banco e aplicação
7
 *
8
 * @author Bruno Morais <[email protected]>
9
 * @copyright GPL © 2023, bmorais.com
10
 * @package bmorais\database
11
 * @subpackage class
12
 * @access private
13
 */
14
15
abstract class Crud {
16
17
    use DatalayerTrait;
18
19
    /**
20
     * @param string $fields
21
     * @param string $add
22
     * @param array|null $values
23
     * @param bool $returnModel
24
     * @param bool $debug
25
     * @return array|false|void
26
     */
27
    public function select(string $fields = "*", string $add = "", array $values = null, bool $returnModel = false, bool $debug = false)
28
    {
29
        if (strlen($add)>0) { $add = " " . $add; }
30
        $sql = "SELECT {$fields} FROM {$this->tableName}{$add}";
31
        if ($debug) { echo $sql; die(); }
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
32
        $this->executeSQL($sql, $values);
33
        if ($returnModel) {
34
            return $this->fetchArrayClass($this->prepare);
35
        } else {
36
            return $this->fetchArrayObj($this->prepare);
37
        }
38
    }
39
40
    /**
41
     * @param string $fields
42
     * @param array|null $values
43
     * @param $debug
44
     * @return bool|void
45
     */
46
    public function insert(string $fields, array $values = null, $debug = false)
47
    {
48
        $numparams = "";
49
        foreach ($values as $item) { $numparams .= ",?"; }
50
        $numparams = substr($numparams, 1);
51
        $sql = "INSERT INTO {$this->tableName} ({$fields}) VALUES ({$numparams})";
52
        if ($debug) { echo $sql; echo "<pre>"; print_r($values); echo "</pre>"; die(); }
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
53
        $result = $this->executeSQL($sql, $values);
54
        if (empty($result))
55
            return false;
56
        return true;
57
    }
58
59
    /**
60
     * @param object $object
61
     * @return bool|null
62
     */
63
    public function insertObject(object $object)
64
    {
65
        $args = [];
66
        $params = [];
67
        foreach ($object as $chave => $valor) {
68
            if ($valor != NULL) {
69
                array_push($args, $chave);
70
                array_push($params, $valor);
71
            }
72
        }
73
        $args = implode(',', $args);
74
        return $this->insert($args, $params);
75
    }
76
77
    /**
78
     * @param array $params
79
     * @return bool
80
     */
81
    public function insertArray(array $params): bool
82
    {
83
        if (!empty($params)) {
84
            $query = "INSERT INTO $this->tableName";
85
            $values = [];
86
            $dataColumns = array_keys($params);
87
            if (isset ($dataColumns[0]))
88
                $query .= ' (`' . implode('`, `', $dataColumns) . '`) ';
89
            $query .= ' VALUES (';
90
91
            foreach ($dataColumns as $index => $column) {
92
                $values[] = $params[$column];
93
                $query .= "?, ";
94
            }
95
96
            $query = rtrim($query, ', ');
97
            $query .= ')';
98
99
            $result = $this->executeSQL($query, $values);
100
            if (empty($result))
101
                return false;
102
            return true;
103
        } else {
104
            return false;
105
        }
106
    }
107
108
    /**
109
     * @param string $fields
110
     * @param array|null $values
111
     * @param string|null $where
112
     * @param bool $debug
113
     * @return bool|void
114
     */
115
    public function update(string $fields, array $values = null, string $where = null, bool $debug = false)
116
    {
117
        $fields_T = "";
118
        $atributos = explode(",", $fields);
119
120
        foreach ($atributos as $item) { $fields_T .= ", {$item} = ?"; }
121
        $fields_T = substr($fields_T, 2);
122
        $sql = "UPDATE {$this->tableName} SET {$fields_T}";
123
        if (isset($where)) { $sql .= " WHERE $where"; }
124
        if ($debug) { echo $sql; echo "<pre>"; print_r($values); echo "</pre>"; die(); }
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
125
        $result = $this->executeSQL($sql, $values);
126
        if (empty($result))
127
            return false;
128
        return true;
129
    }
130
131
132
    /**
133
     * @param object $object
134
     * @param string $where
135
     * @return bool|null
136
     */
137
    public function updateObject(object $object, string $where){
138
        $args = [];
139
        $params = [];
140
        foreach ($object as $chave => $valor) {
141
            if ($valor != NULL) {
142
                array_push($args, $chave);
143
                array_push($params, $valor);
144
            }
145
        }
146
        $args = implode(',', $args);
147
        return $this->update($args, $params, $where);
148
    }
149
150
    /**
151
     * @param array $params
152
     * @param string $where
153
     * @return bool
154
     */
155
156
    public function updateArray(array $params, string $where): bool
157
    {
158
        if (!empty($params)) {
159
            $query = "UPDATE {$this->tableName} SET";
160
            $values = [];
161
162
            foreach ($params as $index => $column) {
163
                $query .= " {$index} = ?, ";
164
                $values[] = $params[$index];
165
            }
166
            $query = rtrim($query, ", ");
167
168
            if (!empty($where))
169
                $query .= " WHERE {$where}";
170
171
            $result = $this->executeSQL($query, $values);
172
            if (empty($result))
173
                return false;
174
            return true;
175
        } else {
176
            return false;
177
        }
178
    }
179
180
    /**
181
     * @param array|null $values
182
     * @param string|null $where
183
     * @param bool $debug
184
     * @return bool|void
185
     */
186
187
    public function delete(array $values = null, string $where = null, bool $debug = false)
188
    {
189
        $sql = "DELETE FROM {$this->tableName}";
190
        if (!empty($where)) { $sql .= " WHERE $where"; }
191
        if ($debug) { echo $sql; echo "<pre>"; print_r($values); echo "</pre>"; die(); }
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
192
        $result = $this->executeSQL($sql, $values);
193
        if (empty($result))
194
            return false;
195
        return true;
196
    }
197
198
    /**
199
     * @return false|string
200
     */
201
    public function lastInsertId(): ?string
202
    {
203
        return $this->lastId();
204
    }
205
206
    /**
207
     * @return string|null
208
     */
209
    public function getLogSQL():?string
210
    {
211
        return $this->logSQL;
212
    }
213
214
}