Passed
Push — develop ( e333c2...b3554d )
by nguereza
02:25
created

CreateTable::fixed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
/**
4
 * Platine Database
5
 *
6
 * Platine Database is the abstraction layer using PDO with support of query and schema builder
7
 *
8
 * This content is released under the MIT License (MIT)
9
 *
10
 * Copyright (c) 2020 Platine Database
11
 *
12
 * Permission is hereby granted, free of charge, to any person obtaining a copy
13
 * of this software and associated documentation files (the "Software"), to deal
14
 * in the Software without restriction, including without limitation the rights
15
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
 * copies of the Software, and to permit persons to whom the Software is
17
 * furnished to do so, subject to the following conditions:
18
 *
19
 * The above copyright notice and this permission notice shall be included in all
20
 * copies or substantial portions of the Software.
21
 *
22
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
 * SOFTWARE.
29
 */
30
31
/**
32
 *  @file CreateTable.php
33
 *
34
 *  The create table schema class
35
 *
36
 *  @package    Platine\Database\Schema
37
 *  @author Platine Developers Team
38
 *  @copyright  Copyright (c) 2020
39
 *  @license    http://opensource.org/licenses/MIT  MIT License
40
 *  @link   http://www.iacademy.cf
41
 *  @version 1.0.0
42
 *  @filesource
43
 */
44
45
declare(strict_types=1);
46
47
namespace Platine\Database\Schema;
48
49
/**
50
 * Class CreateTable
51
 * @package Platine\Database\Schema
52
 */
53
class CreateTable
54
{
0 ignored issues
show
Coding Style introduced by
Opening brace must not be followed by a blank line
Loading history...
55
56
    /**
57
     * The list of CreateColumn
58
     * @var array<string, CreateColumn>
59
     */
60
    protected array $columns = [];
61
62
    /**
63
     * The primary or list of primary key
64
     * @var string|array<string, mixed>
65
     */
66
    protected $primaryKey;
67
68
    /**
69
     * The list of unique keys
70
     * @var array<string, array<int, mixed>>
71
     */
72
    protected array $uniqueKeys = [];
73
74
    /**
75
     * The list of indexes keys
76
     * @var array<string, array<int, mixed>>
77
     */
78
    protected array $indexes = [];
79
80
    /**
81
     * The list of ForeignKey
82
     * @var array<string, ForeignKey>
83
     */
84
    protected array $foreignKeys = [];
85
86
    /**
87
     * The name of the table
88
     * @var string
89
     */
90
    protected string $table;
91
92
    /**
93
     * The engine for the table
94
     * @var string|null
95
     */
96
    protected ?string $engine = null;
97
98
    /**
99
     * The auto increment name
100
     * @var bool|null
101
     */
102
    protected ?bool $autoincrement = null;
103
104
    /**
105
     * Class constructor
106
     * @param string $table
107
     */
108
    public function __construct(string $table)
109
    {
110
        $this->table = $table;
111
    }
112
113
    /**
114
     *
115
     * @return string
116
     */
117
    public function getTableName(): string
118
    {
119
        return $this->table;
120
    }
121
122
    /**
123
     *
124
     * @return array<string, CreateColumn>
125
     */
126
    public function getColumns(): array
127
    {
128
        return $this->columns;
129
    }
130
131
    /**
132
     *
133
     * @return mixed
134
     */
135
    public function getPrimaryKey()
136
    {
137
        return $this->primaryKey;
138
    }
139
140
    /**
141
     *
142
     * @return array<string, array<int, mixed>>
143
     */
144
    public function getUniqueKeys(): array
145
    {
146
        return $this->uniqueKeys;
147
    }
148
149
    /**
150
     *
151
     * @return array<string, array<int, mixed>>
152
     */
153
    public function getIndexes(): array
154
    {
155
        return $this->indexes;
156
    }
157
158
    /**
159
     *
160
     * @return array<string, ForeignKey>
161
     */
162
    public function getForeignKeys(): array
163
    {
164
        return $this->foreignKeys;
165
    }
166
167
    /**
168
     *
169
     * @return string|null
170
     */
171
    public function getEngine(): ?string
172
    {
173
        return $this->engine;
174
    }
175
176
    /**
177
     *
178
     * @return bool|null
179
     */
180
    public function getAutoincrement(): ?bool
181
    {
182
        return $this->autoincrement;
183
    }
184
185
    /**
186
     *
187
     * @param string|null $name
188
     * @return self
189
     */
190
    public function engine(?string $name): self
191
    {
192
        $this->engine = $name;
193
194
        return $this;
195
    }
196
197
    /**
198
     *
199
     * @param string|array<int, string> $columns
200
     * @param string|null $name
201
     * @return self
202
     */
203
    public function primary($columns, ?string $name = null): self
204
    {
205
        if (!is_array($columns)) {
206
            $columns = [$columns];
207
        }
208
209
        if ($name === null) {
210
            $name = $this->table . '_pk_' . implode('_', $columns);
211
        }
212
213
        $this->primaryKey = [
214
            'name' => $name,
215
            'columns' => $columns
216
        ];
217
218
        return $this;
219
    }
220
221
    /**
222
     *
223
     * @param string|array<int, string> $columns
224
     * @param string|null $name
225
     * @return self
226
     */
227
    public function unique($columns, ?string $name = null): self
228
    {
229
        if (!is_array($columns)) {
230
            $columns = [$columns];
231
        }
232
233
        if ($name === null) {
234
            $name = $this->table . '_uk_' . implode('_', $columns);
235
        }
236
237
        $this->uniqueKeys[$name] = $columns;
238
239
        return $this;
240
    }
241
242
    /**
243
     *
244
     * @param string|array<int, string> $columns
245
     * @param string|null $name
246
     * @return self
247
     */
248
    public function index($columns, ?string $name = null): self
249
    {
250
        if (!is_array($columns)) {
251
            $columns = [$columns];
252
        }
253
254
        if ($name === null) {
255
            $name = $this->table . '_ik_' . implode('_', $columns);
256
        }
257
258
        $this->indexes[$name] = $columns;
259
260
        return $this;
261
    }
262
263
    /**
264
     *
265
     * @param string|array<int, string> $columns
266
     * @param string|null $name
267
     * @return ForeignKey
268
     */
269
    public function foreign($columns, ?string $name = null): ForeignKey
270
    {
271
        if (!is_array($columns)) {
272
            $columns = [$columns];
273
        }
274
275
        if ($name === null) {
276
            $name = $this->table . '_fk_' . implode('_', $columns);
277
        }
278
279
        return $this->foreignKeys[$name] = new ForeignKey($columns);
280
    }
281
282
    /**
283
     *
284
     * @param CreateColumn $column
285
     * @param string|null $name
286
     * @return self
287
     */
288
    public function autoincrement(CreateColumn $column, ?string $name = null): self
289
    {
290
        if ($column->getType() !== 'integer') {
291
            return $this;
292
        }
293
294
        $this->autoincrement = true;
295
296
        $column->set('autoincrement', true);
297
298
        return $this->primary($column->getName(), $name);
299
    }
300
301
    /**
302
     *
303
     * @param string $name
304
     * @return CreateColumn
305
     */
306
    public function integer(string $name): CreateColumn
307
    {
308
        return $this->addColumn($name, 'integer');
309
    }
310
311
    /**
312
     *
313
     * @param string $name
314
     * @return CreateColumn
315
     */
316
    public function float(string $name): CreateColumn
317
    {
318
        return $this->addColumn($name, 'float');
319
    }
320
321
    /**
322
     *
323
     * @param string $name
324
     * @return CreateColumn
325
     */
326
    public function double(string $name): CreateColumn
327
    {
328
        return $this->addColumn($name, 'double');
329
    }
330
331
    /**
332
     *
333
     * @param string $name
334
     * @param int|null $length
335
     * @param int|null $precision
336
     * @return CreateColumn
337
     */
338
    public function decimal(
339
        string $name,
340
        ?int $length = null,
341
        ?int $precision = null
342
    ): CreateColumn {
343
        return $this->addColumn($name, 'decimal')
344
                        ->length($length)
345
                        ->set('precision', $precision);
346
    }
347
348
    /**
349
     *
350
     * @param string $name
351
     * @return CreateColumn
352
     */
353
    public function boolean(string $name): CreateColumn
354
    {
355
        return $this->addColumn($name, 'boolean');
356
    }
357
358
    /**
359
     *
360
     * @param string $name
361
     * @return CreateColumn
362
     */
363
    public function binary(string $name): CreateColumn
364
    {
365
        return $this->addColumn($name, 'binary');
366
    }
367
368
    /**
369
     *
370
     * @param string $name
371
     * @param int $length
372
     * @return CreateColumn
373
     */
374
    public function string(string $name, int $length = 255): CreateColumn
375
    {
376
        return $this->addColumn($name, 'string')
377
                        ->length($length);
378
    }
379
380
    /**
381
     *
382
     * @param string $name
383
     * @param int $length
384
     * @return CreateColumn
385
     */
386
    public function fixed(string $name, int $length = 255): CreateColumn
387
    {
388
        return $this->addColumn($name, 'fixed')
389
                        ->length($length);
390
    }
391
    
392
    /**
393
     *
394
     * @param string $name
395
     * @param array<mixed> $values
396
     * @return CreateColumn
397
     */
398
    public function enum(
399
        string $name,
400
        array $values
401
    ): CreateColumn {
402
        return $this->addColumn($name, 'enum')
403
                        ->set('values', $values);
404
    }
405
406
    /**
407
     *
408
     * @param string $name
409
     * @return CreateColumn
410
     */
411
    public function text(string $name): CreateColumn
412
    {
413
        return $this->addColumn($name, 'text');
414
    }
415
416
    /**
417
     *
418
     * @param string $name
419
     * @return CreateColumn
420
     */
421
    public function time(string $name): CreateColumn
422
    {
423
        return $this->addColumn($name, 'time');
424
    }
425
426
    /**
427
     *
428
     * @param string $name
429
     * @return CreateColumn
430
     */
431
    public function timestamp(string $name): CreateColumn
432
    {
433
        return $this->addColumn($name, 'timestamp');
434
    }
435
436
    /**
437
     *
438
     * @param string $name
439
     * @return CreateColumn
440
     */
441
    public function date(string $name): CreateColumn
442
    {
443
        return $this->addColumn($name, 'date');
444
    }
445
446
    /**
447
     *
448
     * @param string $name
449
     * @return CreateColumn
450
     */
451
    public function datetime(string $name): CreateColumn
452
    {
453
        return $this->addColumn($name, 'datetime');
454
    }
455
456
    /**
457
     *
458
     * @param string $column
459
     * @return self
460
     */
461
    public function softDelete(string $column = 'deleted_at'): self
462
    {
463
        $this->datetime($column);
464
465
        return $this;
466
    }
467
468
    /**
469
     *
470
     * @param string $createColumn
471
     * @param string $updateColumn
472
     * @return self
473
     */
474
    public function timestamps(
475
        string $createColumn = 'created_at',
476
        string $updateColumn = 'updated_at'
477
    ): self {
478
        $this->datetime($createColumn)->notNull();
479
        $this->datetime($updateColumn);
480
481
        return $this;
482
    }
483
484
    /**
485
     *
486
     * @param string $name
487
     * @param string $type
488
     * @return CreateColumn
489
     */
490
    protected function addColumn(string $name, string $type): CreateColumn
491
    {
492
        $column = new CreateColumn($this, $name, $type);
493
        $this->columns[$name] = $column;
494
495
        return $column;
496
    }
497
}
498