Completed
Push — master ( 8e13bf...38fa9e )
by Arman
18s queued 15s
created

SleekDbal::getConnection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.9.0
13
 */
14
15
namespace Quantum\Libraries\Database\Sleekdb;
16
17
use Quantum\Libraries\Database\Sleekdb\Statements\Criteria;
18
use Quantum\Libraries\Database\Sleekdb\Statements\Reducer;
19
use Quantum\Libraries\Database\Sleekdb\Statements\Result;
20
use Quantum\Libraries\Database\Sleekdb\Statements\Model;
21
use Quantum\Libraries\Database\Sleekdb\Statements\Join;
22
use Quantum\Libraries\Database\DbalInterface;
23
use Quantum\Exceptions\DatabaseException;
24
use SleekDB\QueryBuilder;
25
use SleekDB\Store;
26
27
/**
28
 * Class SleekDbal
29
 * @package Quantum\Libraries\Database
30
 */
31
class SleekDbal implements DbalInterface
32
{
33
34
    use Model;
35
    use Result;
36
    use Criteria;
37
    use Reducer;
38
    use Join;
39
40
    /**
41
     * Join type join to
42
     */
43
    const JOINTO = 'joinTo';
44
45
    /**
46
     * Join type join through
47
     */
48
    const JOINTHROUGH = 'joinThrough';
49
50
    /**
51
     * @var bool
52
     */
53
    protected $isNew = false;
54
55
    /**
56
     * @var array
57
     */
58
    protected $data = [];
59
60
    /**
61
     * @var array
62
     */
63
    protected $modifiedFields = [];
64
65
    /**
66
     * @var array
67
     */
68
    protected $criterias = [];
69
70
    /**
71
     * @var array
72
     */
73
    protected $havings = [];
74
75
    /**
76
     * @var array
77
     */
78
    protected $selected = [];
79
80
    /**
81
     * @var array
82
     */
83
    protected $grouped = [];
84
85
    /**
86
     * @var array
87
     */
88
    protected $ordered = [];
89
90
    /**
91
     * @var int
92
     */
93
    protected $offset;
94
95
    /**
96
     * @var int
97
     */
98
    protected $limit;
99
100
    /**
101
     * @var array
102
     */
103
    protected $joins = [];
104
105
    /**
106
     * The database table associated with model
107
     * @var string
108
     */
109
    private $table;
110
111
    /**
112
     * ID column of table
113
     * @var string
114
     */
115
    private $idColumn;
116
117
    /**
118
     * Foreign keys
119
     * @var array
120
     */
121
    private $foreignKeys = [];
122
123
    /**
124
     * Hidden fields
125
     * @var array
126
     */
127
    public $hidden = [];
128
129
    /**
130
     * ORM Model
131
     * @var object|null
132
     */
133
    private $ormModel = null;
134
135
    /**
136
     * @var QueryBuilder|null
137
     */
138
    private $queryBuilder;
139
140
    /**
141
     * Active connection
142
     * @var array|null
143
     */
144
    private static $connection = null;
145
146
    /**
147
     * Operators map
148
     * @var string[]
149
     */
150
    private $operators = [
0 ignored issues
show
introduced by
The private property $operators is not used, and could be removed.
Loading history...
151
        '=', '!=',
152
        '>', '>=',
153
        '<', '<=',
154
        'IN', 'NOT IN',
155
        'LIKE', 'NOT LIKE',
156
        'BETWEEN', 'NOT BETWEEN',
157
    ];
158
159
    /**
160
     * Class constructor
161
     * @param string $table
162
     * @param string $idColumn
163
     * @param array $foreignKeys
164
     */
165
    public function __construct(string $table, string $idColumn = 'id', array $foreignKeys = [], array $hidden = [])
166
    {
167
        $this->table = $table;
168
        $this->idColumn = $idColumn;
169
        $this->foreignKeys = $foreignKeys;
170
        $this->hidden = $hidden;
171
    }
172
173
    public function __get($key) {
174
        return isset($this->data[$key]) ? $this->data[$key] : null;
175
    }
176
177
    /**
178
     * @inheritDoc
179
     */
180
    public static function connect(array $config)
181
    {
182
        self::$connection = $config;
183
    }
184
185
		public function setData(array $data)
186
		{
187
			$this->data = $data;
188
		}
189
190
		public function setModifiedFields($modifiedFields)
191
		{
192
			$this->modifiedFields = $modifiedFields;
193
		}
194
195
		public function setIsNew($isNew)
196
		{
197
			$this->isNew = $isNew;
198
		}
199
200
    /**
201
     * @inheritDoc
202
     */
203
    public static function getConnection(): ?array
204
    {
205
        return self::$connection;
206
    }
207
208
    /**
209
     * @inheritDoc
210
     */
211
    public static function disconnect()
212
    {
213
        self::$connection = null;
214
    }
215
216
    /**
217
     * @inheritDoc
218
     */
219
    public function getTable(): string
220
    {
221
        return $this->table;
222
    }
223
224
    /**
225
     * Gets the ORM model
226
     * @return Store
227
     * @throws DatabaseException
228
     * @throws \SleekDB\Exceptions\IOException
229
     * @throws \SleekDB\Exceptions\InvalidArgumentException
230
     * @throws \SleekDB\Exceptions\InvalidConfigurationException
231
     */
232
    public function getOrmModel(): Store
233
    {
234
        if (!$this->ormModel) {
235
            if (!self::getConnection()) {
236
                throw DatabaseException::missingConfig();
237
            }
238
239
            $connection = self::getConnection();
240
241
            if (empty($connection['database_dir'])) {
242
                throw DatabaseException::incorrectConfig();
243
            }
244
245
            $connection['config']['primary_key'] = $this->idColumn;
246
247
            $this->ormModel = new Store($this->table, $connection['database_dir'], $connection['config']);
248
        }
249
250
        return $this->ormModel;
251
    }
252
253
    /**
254
     * Deletes the table and the data
255
     * @throws DatabaseException
256
     * @throws \SleekDB\Exceptions\IOException
257
     * @throws \SleekDB\Exceptions\InvalidArgumentException
258
     * @throws \SleekDB\Exceptions\InvalidConfigurationException
259
     */
260
    public function deleteTable()
261
    {
262
        $this->getOrmModel()->deleteStore();
263
    }
264
265
    /**
266
     * Gets the query builder object
267
     * @return QueryBuilder
268
     * @throws DatabaseException
269
     * @throws \Quantum\Exceptions\ModelException
270
     * @throws \SleekDB\Exceptions\IOException
271
     * @throws \SleekDB\Exceptions\InvalidArgumentException
272
     * @throws \SleekDB\Exceptions\InvalidConfigurationException
273
     */
274
    public function getBuilder(): QueryBuilder
275
    {
276
        if (!$this->queryBuilder) {
277
            $this->queryBuilder = $this->getOrmModel()->createQueryBuilder();
278
        }
279
280
        if (!empty($this->selected)) {
281
            $this->queryBuilder->select($this->selected);
282
        }
283
284
        if (!empty($this->joins)) {
285
            $this->applyJoins();
286
        }
287
288
        if (!empty($this->criterias)) {
289
            $this->queryBuilder->where($this->criterias);
290
        }
291
292
        if (!empty($this->havings)) {
293
            $this->queryBuilder->having($this->havings);
294
        }
295
296
        if (!empty($this->grouped)) {
297
            $this->queryBuilder->groupBy($this->grouped);
298
        }
299
300
        if (!empty($this->ordered)) {
301
            $this->queryBuilder->orderBy($this->ordered);
302
        }
303
304
        if ($this->offset) {
305
            $this->queryBuilder->skip($this->offset);
306
        }
307
308
        if ($this->limit) {
309
            $this->queryBuilder->limit($this->limit);
310
        }
311
312
        return $this->queryBuilder;
313
    }
314
315
}