|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: liow.kitloong |
|
5
|
|
|
* Date: 2020/03/29 |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace KitLoong\MigrationsGenerator\Generators; |
|
9
|
|
|
|
|
10
|
|
|
use Doctrine\DBAL\Schema\Column; |
|
11
|
|
|
use KitLoong\MigrationsGenerator\MigrationMethod\ColumnModifier; |
|
12
|
|
|
use KitLoong\MigrationsGenerator\MigrationMethod\ColumnName; |
|
13
|
|
|
use KitLoong\MigrationsGenerator\MigrationMethod\ColumnType; |
|
14
|
|
|
|
|
15
|
|
|
class DatetimeField |
|
16
|
|
|
{ |
|
17
|
|
|
private $decorator; |
|
18
|
|
|
|
|
19
|
63 |
|
public function __construct(Decorator $decorator) |
|
20
|
|
|
{ |
|
21
|
63 |
|
$this->decorator = $decorator; |
|
22
|
63 |
|
} |
|
23
|
|
|
|
|
24
|
12 |
|
public function makeField(array $field, Column $column, bool $useTimestamps): array |
|
25
|
|
|
{ |
|
26
|
12 |
|
if ($useTimestamps) { |
|
27
|
6 |
|
if ($field['field'] === ColumnName::CREATED_AT) { |
|
28
|
3 |
|
return []; |
|
29
|
3 |
|
} elseif ($field['field'] === ColumnName::UPDATED_AT) { |
|
30
|
3 |
|
$field['type'] = ColumnType::TIMESTAMPS; |
|
31
|
3 |
|
$field['field'] = null; |
|
32
|
|
|
} |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
9 |
|
if ($field['field'] === ColumnName::DELETED_AT && !$column->getNotnull()) { |
|
36
|
3 |
|
$field['type'] = ColumnType::SOFT_DELETES; |
|
37
|
3 |
|
$field['field'] = null; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
9 |
|
if (isset(FieldGenerator::$fieldTypeMap[$field['type']])) { |
|
41
|
3 |
|
$field['type'] = FieldGenerator::$fieldTypeMap[$field['type']]; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
9 |
|
if ($column->getLength() !== null && $column->getLength() > 0) { |
|
45
|
9 |
|
if ($field['type'] === ColumnType::SOFT_DELETES) { |
|
46
|
3 |
|
$field['field'] = ColumnName::DELETED_AT; |
|
47
|
|
|
} |
|
48
|
9 |
|
$field['args'][] = $column->getLength(); |
|
49
|
|
|
} |
|
50
|
9 |
|
return $field; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
6 |
|
public function makeDefault(Column $column): string |
|
54
|
|
|
{ |
|
55
|
6 |
|
if (in_array($column->getDefault(), ['CURRENT_TIMESTAMP'], true)) { |
|
56
|
3 |
|
return ColumnModifier::USE_CURRENT; |
|
57
|
|
|
} else { |
|
58
|
3 |
|
$default = $this->decorator->columnDefaultToString($column->getDefault()); |
|
|
|
|
|
|
59
|
3 |
|
return $this->decorator->decorate(ColumnModifier::DEFAULT, [$default]); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param Column[] $columns |
|
65
|
|
|
* @return bool |
|
66
|
|
|
*/ |
|
67
|
12 |
|
public function isUseTimestamps($columns): bool |
|
68
|
|
|
{ |
|
69
|
|
|
/** @var Column[] $timestampsColumns */ |
|
70
|
12 |
|
$timestampsColumns = []; |
|
71
|
12 |
|
foreach ($columns as $column) { |
|
72
|
12 |
|
if ($column->getName() === ColumnName::CREATED_AT || $column->getName() === ColumnName::UPDATED_AT) { |
|
73
|
12 |
|
$timestampsColumns[] = $column; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
12 |
|
$useTimestamps = false; |
|
78
|
|
|
|
|
79
|
12 |
|
if (count($timestampsColumns) === 2) { |
|
80
|
6 |
|
$useTimestamps = true; |
|
81
|
6 |
|
foreach ($timestampsColumns as $timestamp) { |
|
82
|
6 |
|
if ($timestamp->getNotnull() || $timestamp->getDefault() !== null) { |
|
83
|
3 |
|
$useTimestamps = false; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
12 |
|
return $useTimestamps; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|