Completed
Push — master ( dfd334...80840e )
by Restu
11:54
created

Database::getModel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
namespace JayaCode\Framework\Core\Database;
3
4
use JayaCode\Framework\Core\Database\Connector\Connector;
5
use JayaCode\Framework\Core\Database\Connector\ConnectorMySql;
6
use JayaCode\Framework\Core\Database\Query\Grammar\GrammarMySql;
7
use JayaCode\Framework\Core\Database\Query\Query;
8
use PDO;
9
10
/**
11
 * Class Database
12
 * @package JayaCode\Framework\Core\Database
13
 */
14
class Database
15
{
16
    /**
17
     * @var Connector
18
     */
19
    protected $connector;
20
21
    /**
22
     * @var \PDO
23
     */
24
    protected $pdo;
25
26
    /**
27
     * @var \PDOStatement
28
     */
29
    protected $statement;
30
31
    /**
32
     * @var Query
33
     */
34
    protected $query;
35
36
    /**
37
     * @var
38
     */
39
    protected $grammar;
40
41
    /**
42
     * @var array
43
     */
44
    protected $driver = [
45
        "mysql" => [
46
            "connector" => ConnectorMySql::class,
47
            "grammar" => GrammarMySql::class,
48
        ]
49
    ];
50
51
    /**
52
     * @var array
53
     */
54
    protected $config = [
55
        "driver" => "mysql",
56
57
        "host" => "",
58
        "username" => "",
59
        "password" => "",
60
61
        "dbname" => "",
62
        "options" => [
63
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
64
            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
65
        ]
66
    ];
67
68
    /**
69
     * @var string
70
     */
71
    protected $model = null;
72
73
    /**
74
     * @param $config
75
     */
76
    public function __construct($config)
77
    {
78
        $this->config = array_merge($this->config, $config);
79
80
        $this->initialize();
81
82
        $this->createConnection();
83
    }
84
85
    /**
86
     * @param $config
87
     * @return static
88
     */
89
    public static function create($config)
90
    {
91
        return new static($config);
92
    }
93
94
    /**
95
     *
96
     */
97
    protected function initialize()
98
    {
99
        $connectorClass = $this->driver[$this->config["driver"]]["connector"];
100
        $this->connector = new $connectorClass();
101
102
        $grammarClass = $this->driver[$this->config["driver"]]["grammar"];
103
        $this->grammar = new $grammarClass();
104
105
        $this->query = new Query();
106
    }
107
108
    /**
109
     * @return PDO
110
     */
111
    public function createConnection()
112
    {
113
        $this->pdo = $this->connector->connect($this->config);
114
        return $this->pdo;
115
    }
116
117
    /**
118
     * @param $query
119
     * @param null $params
120
     * @return $this
121
     */
122
    public function sql($query, $params = null)
123
    {
124
        $this->query = $this->query->sql($query, $params);
125
        return $this;
126
    }
127
128
    /**
129
     * @param $table
130
     * @return $this
131
     */
132
    public function table($table)
133
    {
134
        $this->query->setTable($table);
135
        return $this;
136
    }
137
138
    /**
139
     * @param null $columns
140
     * @return $this
141
     */
142
    public function select($columns = null)
143
    {
144
        $this->query->select($columns);
145
        return $this;
146
    }
147
148
    /**
149
     * @param $query
150
     * @param string $type
151
     * @return Query
152
     */
153
    public function whereQ($query, $type = "AND")
154
    {
155
        $this->query->whereQ($query, $type);
156
        return $this;
157
    }
158
159
    /**
160
     * @param $column
161
     * @param $value
162
     * @param string $operator
163
     * @param string $type
164
     * @return $this
165
     */
166
    public function where($column, $value, $operator = "=", $type = "AND")
167
    {
168
        $this->query->where($column, $value, $operator, $type);
169
        return $this;
170
    }
171
172
    /**
173
     * @param $column
174
     * @param $value
175
     * @param string $operator
176
     * @return $this
177
     */
178
    public function andWhere($column, $value, $operator = "=")
179
    {
180
        $this->query->andWhere($column, $value, $operator);
181
        return $this;
182
    }
183
184
    /**
185
     * @param $column
186
     * @param $value
187
     * @param string $operator
188
     * @return $this
189
     */
190
    public function orWhere($column, $value, $operator = "=")
191
    {
192
        $this->query->orWhere($column, $value, $operator);
193
        return $this;
194
    }
195
196
    /**
197
     * @param $column
198
     * @param $value
199
     * @param string $type
200
     * @return $this
201
     */
202
    public function like($column, $value, $type = "AND")
203
    {
204
        $this->query->like($column, $value, $type);
205
        return $this;
206
    }
207
208
    /**
209
     * @param $column
210
     * @param $value
211
     * @return $this
212
     */
213
    public function andLike($column, $value)
214
    {
215
        $this->query->andLike($column, $value);
216
        return $this;
217
    }
218
219
    /**
220
     * @param $column
221
     * @param $value
222
     * @return $this
223
     */
224
    public function orLike($column, $value)
225
    {
226
        $this->query->orLike($column, $value);
227
        return $this;
228
    }
229
230
    /**
231
     * @param $column
232
     * @param $value
233
     * @param string $type
234
     * @return $this
235
     */
236
    public function between($column, $value, $type = "AND")
237
    {
238
        $this->query->between($column, $value, $type);
239
        return $this;
240
    }
241
242
    /**
243
     * @param $column
244
     * @param $value
245
     * @return $this
246
     */
247
    public function andBetween($column, $value)
248
    {
249
        $this->query->andBetween($column, $value);
250
        return $this;
251
    }
252
253
    /**
254
     * @param $column
255
     * @param $value
256
     * @return $this
257
     */
258
    public function orBetween($column, $value)
259
    {
260
        $this->query->orBetween($column, $value);
261
        return $this;
262
    }
263
264
    /**
265
     * @return $this
266
     * @throws \Exception
267
     */
268
    public function execute()
269
    {
270
        $qArr = $this->query->build($this->grammar);
271
272
        $this->statement = $this->pdo->prepare($qArr[0]);
273
274
        if (!$this->statement->execute($qArr[1])) {
275
            throw new \Exception(join(". ", $this->statement->errorInfo()));
276
        }
277
278
        return $this;
279
    }
280
281
    /**
282
     * @return array
283
     * @throws \Exception
284
     */
285 View Code Duplication
    public function all()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
286
    {
287
        if (!$this->statement) {
288
            $this->execute();
289
        }
290
291
        if ($this->model && class_exists($this->model)) {
292
            $dataModel = array();
293
            while ($data = $this->statement->fetch()) {
294
                $dataModel[] = new $this->model($data, false);
295
            }
296
            return $dataModel;
297
        }
298
        return $this->statement->fetchAll();
299
    }
300
301
    /**
302
     * @return mixed
303
     * @throws \Exception
304
     */
305 View Code Duplication
    public function get()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
306
    {
307
        if (!$this->statement) {
308
            $this->execute();
309
        }
310
311
        if ($this->model && class_exists($this->model)) {
312
            $data = $this->statement->fetch();
313
            return $data?new $this->model($data, false):$data;
314
        }
315
316
        return $this->statement->fetch();
317
    }
318
319
    /**
320
     * Clear statement PDO and query builder
321
     */
322
    public function clear()
323
    {
324
        $this->statement = null;
325
        $this->query->clear();
326
    }
327
328
    /**
329
     * @return string
330
     */
331
    public function getModel()
332
    {
333
        return $this->model;
334
    }
335
336
    /**
337
     * @param string $model
338
     * @param null $table
339
     */
340
    public function setModel($model, $table = null)
341
    {
342
        $this->clear();
343
344
        if ($table) {
345
            $this->table($table);
346
        }
347
        $this->model = $model;
348
    }
349
}
350