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\Database\Schema\Builder; |
12
|
|
|
use KitLoong\MigrationsGenerator\Generators\Modifier\CharsetModifier; |
13
|
|
|
use KitLoong\MigrationsGenerator\Generators\Modifier\CollationModifier; |
14
|
|
|
use KitLoong\MigrationsGenerator\MigrationMethod\ColumnName; |
15
|
|
|
use KitLoong\MigrationsGenerator\MigrationMethod\ColumnType; |
16
|
|
|
use KitLoong\MigrationsGenerator\MigrationsGeneratorSetting; |
17
|
|
|
use KitLoong\MigrationsGenerator\Repositories\PgSQLRepository; |
18
|
|
|
use KitLoong\MigrationsGenerator\Repositories\SQLSrvRepository; |
19
|
|
|
use KitLoong\MigrationsGenerator\Support\Regex; |
20
|
|
|
|
21
|
|
|
class StringField |
22
|
|
|
{ |
23
|
|
|
const SQLSRV_TEXT_TYPE = 'nvarchar'; |
24
|
|
|
const SQLSRV_TEXT_LENGTH = -1; |
25
|
|
|
|
26
|
|
|
private $collationModifier; |
27
|
|
|
private $charsetModifier; |
28
|
|
|
private $pgSQLRepository; |
29
|
|
|
private $sqlSrvRepository; |
30
|
|
|
private $regex; |
31
|
|
|
|
32
|
|
|
public function __construct( |
33
|
|
|
CollationModifier $collationModifier, |
34
|
|
|
CharsetModifier $charsetModifier, |
35
|
|
|
PgSQLRepository $pgSQLRepository, |
36
|
|
|
SQLSrvRepository $sqlSrvRepository, |
37
|
|
|
Regex $regex |
38
|
|
|
) { |
39
|
|
|
$this->collationModifier = $collationModifier; |
40
|
|
|
$this->charsetModifier = $charsetModifier; |
41
|
|
|
$this->pgSQLRepository = $pgSQLRepository; |
42
|
|
|
$this->sqlSrvRepository = $sqlSrvRepository; |
43
|
|
|
$this->regex = $regex; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function makeField(string $tableName, array $field, Column $column): array |
47
|
|
|
{ |
48
|
|
|
switch (app(MigrationsGeneratorSetting::class)->getPlatform()) { |
49
|
|
|
// It could be pgsql enum |
50
|
|
|
case Platform::POSTGRESQL: |
51
|
|
|
if (($pgSQLEnum = $this->getPgSQLEnumValue($tableName, $column->getName())) !== '') { |
52
|
|
|
$field['type'] = ColumnType::ENUM; |
53
|
|
|
$field['args'][] = $pgSQLEnum; |
54
|
|
|
} |
55
|
|
|
break; |
56
|
|
|
// It could be sqlsrv text |
57
|
|
|
case Platform::SQLSERVER: |
58
|
|
|
$colDef = $this->sqlSrvRepository->getColumnDefinition($tableName, $column->getName()); |
59
|
|
|
if ($colDef->getType() === self::SQLSRV_TEXT_TYPE && |
60
|
|
|
$colDef->getLength() === self::SQLSRV_TEXT_LENGTH) { |
61
|
|
|
$field['type'] = ColumnType::TEXT; |
62
|
|
|
} |
63
|
|
|
break; |
64
|
|
|
default: |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// Continue if type is `string` |
68
|
|
|
if ($field['type'] === ColumnType::STRING) { |
69
|
|
|
if ($field['field'] === ColumnName::REMEMBER_TOKEN && $column->getLength() === 100 && !$column->getFixed()) { |
70
|
|
|
$field['type'] = ColumnType::REMEMBER_TOKEN; |
71
|
|
|
$field['field'] = null; |
72
|
|
|
$field['args'] = []; |
73
|
|
|
} else { |
74
|
|
|
if ($column->getFixed()) { |
75
|
|
|
$field['type'] = ColumnType::CHAR; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if ($column->getLength() && $column->getLength() !== Builder::$defaultStringLength) { |
|
|
|
|
79
|
|
|
$field['args'][] = $column->getLength(); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$charset = $this->charsetModifier->generate($tableName, $column); |
85
|
|
|
if ($charset !== '') { |
86
|
|
|
$field['decorators'][] = $charset; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$collation = $this->collationModifier->generate($tableName, $column); |
90
|
|
|
if ($collation !== '') { |
91
|
|
|
$field['decorators'][] = $collation; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $field; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
private function getPgSQLEnumValue(string $tableName, string $column): string |
98
|
|
|
{ |
99
|
|
|
$definition = ($this->pgSQLRepository->getCheckConstraintDefinition($tableName, $column)); |
100
|
|
|
if (!empty($definition)) { |
101
|
|
|
$enumValues = $this->regex->getTextBetweenAll($definition, "'", "'::"); |
102
|
|
|
if (!empty($enumValues)) { |
103
|
|
|
return "['".implode("', '", $enumValues)."']"; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
return ''; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: