|
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 Illuminate\Support\Collection; |
|
12
|
|
|
use KitLoong\MigrationsGenerator\MigrationsGeneratorSetting; |
|
13
|
|
|
use KitLoong\MigrationsGenerator\MigrationMethod\ColumnModifier; |
|
14
|
|
|
use KitLoong\MigrationsGenerator\MigrationMethod\ColumnType; |
|
15
|
|
|
|
|
16
|
|
|
class IntegerField |
|
17
|
|
|
{ |
|
18
|
12 |
|
public function makeField(string $tableName, array $field, Column $column, Collection $indexes): array |
|
19
|
|
|
{ |
|
20
|
12 |
|
if (isset(FieldGenerator::$fieldTypeMap[$field['type']])) { |
|
21
|
9 |
|
$field['type'] = FieldGenerator::$fieldTypeMap[$field['type']]; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
12 |
|
$isBoolean = $this->checkIsMySQLBoolean($tableName, $field, $column); |
|
25
|
12 |
|
if ($isBoolean) { |
|
26
|
3 |
|
return $this->handleBoolean($field, $column); |
|
27
|
|
|
} else { |
|
28
|
9 |
|
return $this->handleInteger($field, $column, $indexes); |
|
29
|
|
|
} |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
3 |
|
private function handleBoolean(array $field, Column $column): array |
|
33
|
|
|
{ |
|
34
|
3 |
|
$field['type'] = ColumnType::BOOLEAN; |
|
35
|
3 |
|
if ($column->getUnsigned()) { |
|
36
|
3 |
|
$field['decorators'][] = ColumnModifier::UNSIGNED; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
3 |
|
return $field; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
9 |
|
private function handleInteger(array $field, Column $column, Collection $indexes): array |
|
43
|
|
|
{ |
|
44
|
9 |
|
if ($column->getUnsigned() && $column->getAutoincrement()) { |
|
45
|
3 |
|
if ($field['type'] === 'integer') { |
|
46
|
3 |
|
$field['type'] = ColumnType::INCREMENTS; |
|
47
|
|
|
} else { |
|
48
|
3 |
|
$field['type'] = str_replace('Integer', 'Increments', $field['type']); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
3 |
|
$indexes->forget($field['field']); |
|
52
|
|
|
} else { |
|
53
|
6 |
|
if ($column->getUnsigned()) { |
|
54
|
3 |
|
$field['type'] = 'unsigned'.ucfirst($field['type']); |
|
55
|
|
|
} |
|
56
|
6 |
|
if ($column->getAutoincrement()) { |
|
57
|
3 |
|
$field['args'][] = 'true'; |
|
58
|
3 |
|
$indexes->forget($field['field']); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
9 |
|
return $field; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
12 |
|
private function checkIsMySQLBoolean(string $tableName, array $field, Column $column): bool |
|
65
|
|
|
{ |
|
66
|
|
|
/** @var MigrationsGeneratorSetting $setting */ |
|
67
|
12 |
|
$setting = app(MigrationsGeneratorSetting::class); |
|
68
|
|
|
|
|
69
|
12 |
|
if ($setting->getPlatform() === Platform::MYSQL && |
|
70
|
9 |
|
$field['type'] === ColumnType::TINY_INTEGER && |
|
71
|
9 |
|
!$column->getAutoincrement()) { |
|
72
|
3 |
|
$column = $setting->getConnection()->select("SHOW COLUMNS FROM `${tableName}` where Field = '${field['field']}' AND Type LIKE 'tinyint(1)%'"); |
|
73
|
3 |
|
return !empty($column); |
|
74
|
|
|
} |
|
75
|
9 |
|
return false; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|