Passed
Pull Request — master (#312)
by Arman
03:23
created

SleekDbal::setData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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.8
13
 */
14
15
namespace Quantum\Libraries\Database\Adapters\Sleekdb;
16
17
use Quantum\Libraries\Database\Adapters\Sleekdb\Statements\Criteria;
18
use Quantum\Libraries\Database\Adapters\Sleekdb\Statements\Reducer;
19
use Quantum\Libraries\Database\Adapters\Sleekdb\Statements\Result;
20
use Quantum\Libraries\Database\Adapters\Sleekdb\Statements\Model;
21
use Quantum\Libraries\Database\Adapters\Sleekdb\Statements\Join;
22
use Quantum\Libraries\Database\Exceptions\DatabaseException;
23
use Quantum\Libraries\Database\Contracts\DbalInterface;
24
use SleekDB\Exceptions\InvalidConfigurationException;
25
use SleekDB\Exceptions\InvalidArgumentException;
26
use Quantum\Model\Exceptions\ModelException;
27
use SleekDB\Exceptions\IOException;
28
use SleekDB\QueryBuilder;
29
use SleekDB\Store;
30
31
/**
32
 * Class SleekDbal
33
 * @package Quantum\Libraries\Database
34
 */
35
class SleekDbal implements DbalInterface
36
{
37
38
    use Model;
39
    use Result;
40
    use Criteria;
41
    use Reducer;
42
    use Join;
43
44
    /**
45
     * Join type join to
46
     */
47
    const JOINTO = 'joinTo';
48
49
    /**
50
     * Join type join through
51
     */
52
    const JOINTHROUGH = 'joinThrough';
53
54
    /**
55
     * @var bool
56
     */
57
    protected $isNew = false;
58
59
    /**
60
     * @var array
61
     */
62
    protected $data = [];
63
64
    /**
65
     * @var array
66
     */
67
    protected $modifiedFields = [];
68
69
    /**
70
     * @var array
71
     */
72
    protected $criterias = [];
73
74
    /**
75
     * @var array
76
     */
77
    protected $havings = [];
78
79
    /**
80
     * @var array
81
     */
82
    protected $selected = [];
83
84
    /**
85
     * @var array
86
     */
87
    protected $grouped = [];
88
89
    /**
90
     * @var array
91
     */
92
    protected $ordered = [];
93
94
    /**
95
     * @var int
96
     */
97
    protected $offset;
98
99
    /**
100
     * @var int
101
     */
102
    protected $limit;
103
104
    /**
105
     * @var array
106
     */
107
    protected $joins = [];
108
109
    /**
110
     * Associated model name
111
     * @var string
112
     */
113
    private $modelName;
114
115
    /**
116
     * The database table associated with model
117
     * @var string
118
     */
119
    private $table;
120
121
    /**
122
     * ID column of table
123
     * @var string
124
     */
125
    private $idColumn;
126
127
    /**
128
     * Foreign keys
129
     * @var array
130
     */
131
    private $foreignKeys;
132
133
    /**
134
     * Hidden fields
135
     * @var array
136
     */
137
    public $hidden = [];
138
139
    /**
140
     * ORM Model
141
     * @var object|null
142
     */
143
    private $ormModel = null;
144
145
    /**
146
     * @var QueryBuilder|null
147
     */
148
    private $queryBuilder;
149
150
    /**
151
     * Active connection
152
     * @var array|null
153
     */
154
    private static $connection = null;
155
156
    /**
157
     * Operators map
158
     * @var string[]
159
     */
160
    private $operators = [
0 ignored issues
show
introduced by
The private property $operators is not used, and could be removed.
Loading history...
161
        '=', '!=',
162
        '>', '>=',
163
        '<', '<=',
164
        'IN', 'NOT IN',
165
        'LIKE', 'NOT LIKE',
166
        'BETWEEN', 'NOT BETWEEN',
167
    ];
168
169
    /**
170
     * @param string $table
171
     * @param string|null $modelName
172
     * @param string $idColumn
173
     * @param array $foreignKeys
174
     * @param array $hidden
175
     */
176
    public function __construct(
177
        string $table,
178
        string $modelName = null,
179
        string $idColumn = 'id',
180
        array  $foreignKeys = [],
181
        array  $hidden = []
182
    )
183
    {
184
        $this->modelName = $modelName;
185
        $this->table = $table;
186
        $this->idColumn = $idColumn;
187
        $this->foreignKeys = $foreignKeys;
188
        $this->hidden = $hidden;
189
    }
190
191
    public function __get($key)
192
    {
193
        return $this->data[$key] ?? null;
194
    }
195
196
    /**
197
     * @inheritDoc
198
     */
199
    public static function connect(array $config)
200
    {
201
        self::$connection = $config;
202
    }
203
204
    public function setData(array $data)
205
    {
206
        $this->data = $data;
207
    }
208
209
    /**
210
     * @param $modifiedFields
211
     * @return void
212
     */
213
    public function setModifiedFields($modifiedFields)
214
    {
215
        $this->modifiedFields = $modifiedFields;
216
    }
217
218
    /**
219
     * @param bool $isNew
220
     * @return void
221
     */
222
    public function setIsNew(bool $isNew)
223
    {
224
        $this->isNew = $isNew;
225
    }
226
227
    /**
228
     * @inheritDoc
229
     */
230
    public static function getConnection(): ?array
231
    {
232
        return self::$connection;
233
    }
234
235
    /**
236
     * @inheritDoc
237
     */
238
    public static function disconnect()
239
    {
240
        self::$connection = null;
241
    }
242
243
    /**
244
     * @inheritDoc
245
     */
246
    public function getTable(): string
247
    {
248
        return $this->table;
249
    }
250
251
    /**
252
     * Gets the ORM model
253
     * @return Store
254
     * @throws DatabaseException
255
     * @throws IOException
256
     * @throws InvalidArgumentException
257
     * @throws InvalidConfigurationException
258
     */
259
    public function getOrmModel(): Store
260
    {
261
        if (!$this->ormModel) {
262
            if (!self::getConnection()) {
263
                throw DatabaseException::missingConfig();
264
            }
265
266
            $connection = self::getConnection();
267
268
            if (empty($connection['database_dir'])) {
269
                throw DatabaseException::incorrectConfig();
270
            }
271
272
            $connection['config']['primary_key'] = $this->idColumn;
273
274
            $this->ormModel = new Store($this->table, $connection['database_dir'], $connection['config']);
275
        }
276
277
        return $this->ormModel;
278
    }
279
280
    /**
281
     * @param array|null $modelData
282
     */
283
    public function updateOrmModel(?array $modelData)
284
    {
285
        $this->data = $modelData;
286
        $this->modifiedFields = $modelData;
287
        $this->isNew = false;
288
    }
289
290
    /**
291
     * Deletes the table and the data
292
     * @throws DatabaseException
293
     * @throws IOException
294
     * @throws InvalidArgumentException
295
     * @throws InvalidConfigurationException
296
     */
297
    public function deleteTable()
298
    {
299
        $this->getOrmModel()->deleteStore();
300
    }
301
302
    /**
303
     * Gets the query builder object
304
     * @return QueryBuilder
305
     * @throws DatabaseException
306
     * @throws ModelException
307
     * @throws IOException
308
     * @throws InvalidArgumentException
309
     * @throws InvalidConfigurationException
310
     */
311
    public function getBuilder(): QueryBuilder
312
    {
313
        if (!$this->queryBuilder) {
314
            $this->queryBuilder = $this->getOrmModel()->createQueryBuilder();
315
        }
316
317
        if (!empty($this->selected)) {
318
            $this->queryBuilder->select($this->selected);
319
        }
320
321
        if (!empty($this->joins)) {
322
            $this->applyJoins();
323
        }
324
325
        if (!empty($this->criterias)) {
326
            $this->queryBuilder->where($this->criterias);
327
        }
328
329
        if (!empty($this->havings)) {
330
            $this->queryBuilder->having($this->havings);
331
        }
332
333
        if (!empty($this->grouped)) {
334
            $this->queryBuilder->groupBy($this->grouped);
335
        }
336
337
        if (!empty($this->ordered)) {
338
            $this->queryBuilder->orderBy($this->ordered);
339
        }
340
341
        if ($this->offset) {
342
            $this->queryBuilder->skip($this->offset);
343
        }
344
345
        if ($this->limit) {
346
            $this->queryBuilder->limit($this->limit);
347
        }
348
349
        return $this->queryBuilder;
350
    }
351
352
    /**
353
     * Gets foreign keys
354
     * @return array
355
     */
356
    public function getForeignKeys(): array
357
    {
358
        return $this->foreignKeys;
359
    }
360
361
    /**
362
     * Gets the associated model name
363
     * @return string
364
     */
365
    public function getModelName(): string
366
    {
367
        return $this->modelName;
368
    }
369
370
    /**
371
     * Resets the builder state
372
     */
373
    protected function resetBuilderState()
374
    {
375
        $this->criterias = [];
376
        $this->havings = [];
377
        $this->selected = [];
378
        $this->grouped = [];
379
        $this->ordered = [];
380
        $this->offset = null;
381
        $this->limit = null;
382
        $this->joins = [];
383
        $this->queryBuilder = null;
384
    }
385
}