Passed
Pull Request — master (#125)
by
unknown
03:55
created

SleekDbal::__get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 1
dl 0
loc 2
rs 10
c 0
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
    /**
186
     * @inheritDoc
187
     */
188
    public static function getConnection(): ?array
189
    {
190
        return self::$connection;
191
    }
192
193
    /**
194
     * @inheritDoc
195
     */
196
    public static function disconnect()
197
    {
198
        self::$connection = null;
199
    }
200
201
    /**
202
     * @inheritDoc
203
     */
204
    public function getTable(): string
205
    {
206
        return $this->table;
207
    }
208
209
    /**
210
     * Gets the ORM model
211
     * @return Store
212
     * @throws DatabaseException
213
     * @throws \SleekDB\Exceptions\IOException
214
     * @throws \SleekDB\Exceptions\InvalidArgumentException
215
     * @throws \SleekDB\Exceptions\InvalidConfigurationException
216
     */
217
    public function getOrmModel(): Store
218
    {
219
        if (!$this->ormModel) {
220
            if (!self::getConnection()) {
221
                throw DatabaseException::missingConfig();
222
            }
223
224
            $connection = self::getConnection();
225
226
            if (empty($connection['database_dir'])) {
227
                throw DatabaseException::incorrectConfig();
228
            }
229
230
            $connection['config']['primary_key'] = $this->idColumn;
231
232
            $this->ormModel = new Store($this->table, $connection['database_dir'], $connection['config']);
233
        }
234
235
        return $this->ormModel;
236
    }
237
238
    /**
239
     * Deletes the table and the data
240
     * @throws DatabaseException
241
     * @throws \SleekDB\Exceptions\IOException
242
     * @throws \SleekDB\Exceptions\InvalidArgumentException
243
     * @throws \SleekDB\Exceptions\InvalidConfigurationException
244
     */
245
    public function deleteTable()
246
    {
247
        $this->getOrmModel()->deleteStore();
248
    }
249
250
    /**
251
     * Gets the query builder object
252
     * @return QueryBuilder
253
     * @throws DatabaseException
254
     * @throws \Quantum\Exceptions\ModelException
255
     * @throws \SleekDB\Exceptions\IOException
256
     * @throws \SleekDB\Exceptions\InvalidArgumentException
257
     * @throws \SleekDB\Exceptions\InvalidConfigurationException
258
     */
259
    public function getBuilder(): QueryBuilder
260
    {
261
        if (!$this->queryBuilder) {
262
            $this->queryBuilder = $this->getOrmModel()->createQueryBuilder();
263
        }
264
265
        if (!empty($this->selected)) {
266
            $this->queryBuilder->select($this->selected);
267
        }
268
269
        if (!empty($this->joins)) {
270
            $this->applyJoins();
271
        }
272
273
        if (!empty($this->criterias)) {
274
            $this->queryBuilder->where($this->criterias);
275
        }
276
277
        if (!empty($this->havings)) {
278
            $this->queryBuilder->having($this->havings);
279
        }
280
281
        if (!empty($this->grouped)) {
282
            $this->queryBuilder->groupBy($this->grouped);
283
        }
284
285
        if (!empty($this->ordered)) {
286
            $this->queryBuilder->orderBy($this->ordered);
287
        }
288
289
        if ($this->offset) {
290
            $this->queryBuilder->skip($this->offset);
291
        }
292
293
        if ($this->limit) {
294
            $this->queryBuilder->limit($this->limit);
295
        }
296
297
        return $this->queryBuilder;
298
    }
299
300
}