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\Support\Regex; |
19
|
|
|
|
20
|
|
|
class StringField |
21
|
|
|
{ |
22
|
|
|
private $collationModifier; |
23
|
|
|
private $charsetModifier; |
24
|
|
|
private $pgSQLRepository; |
25
|
|
|
private $regex; |
26
|
|
|
|
27
|
|
|
public function __construct( |
28
|
|
|
CollationModifier $collationModifier, |
29
|
|
|
CharsetModifier $charsetModifier, |
30
|
|
|
PgSQLRepository $pgSQLRepository, |
31
|
|
|
Regex $regex |
32
|
|
|
) { |
33
|
|
|
$this->collationModifier = $collationModifier; |
34
|
|
|
$this->charsetModifier = $charsetModifier; |
35
|
|
|
$this->pgSQLRepository = $pgSQLRepository; |
36
|
|
|
$this->regex = $regex; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function makeField(string $tableName, array $field, Column $column): array |
40
|
|
|
{ |
41
|
|
|
if (($pgSQLEnum = $this->getPgSQLEnumValue($tableName, $column->getName())) !== '') { |
42
|
|
|
$field['type'] = ColumnType::ENUM; |
43
|
|
|
$field['args'][] = $pgSQLEnum; |
44
|
|
|
} else { |
45
|
|
|
if ($field['field'] === ColumnName::REMEMBER_TOKEN && $column->getLength() === 100 && !$column->getFixed()) { |
46
|
|
|
$field['type'] = ColumnType::REMEMBER_TOKEN; |
47
|
|
|
$field['field'] = null; |
48
|
|
|
$field['args'] = []; |
49
|
|
|
} else { |
50
|
|
|
if ($column->getFixed()) { |
51
|
|
|
$field['type'] = ColumnType::CHAR; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if ($column->getLength() && $column->getLength() !== Builder::$defaultStringLength) { |
|
|
|
|
55
|
|
|
$field['args'][] = $column->getLength(); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$charset = $this->charsetModifier->generate($tableName, $column); |
61
|
|
|
if ($charset !== '') { |
62
|
|
|
$field['decorators'][] = $charset; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$collation = $this->collationModifier->generate($tableName, $column); |
66
|
|
|
if ($collation !== '') { |
67
|
|
|
$field['decorators'][] = $collation; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $field; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
private function getPgSQLEnumValue(string $tableName, string $column): string |
74
|
|
|
{ |
75
|
|
|
if (app(MigrationsGeneratorSetting::class)->getPlatform() === Platform::POSTGRESQL) { |
76
|
|
|
$definition = ($this->pgSQLRepository->getCheckConstraintDefinition($tableName, $column)); |
77
|
|
|
if (!empty($definition)) { |
78
|
|
|
$enumValues = $this->regex->getTextBetweenAll($definition, "'", "'::"); |
79
|
|
|
if (!empty($enumValues)) { |
80
|
|
|
return "['".implode("', '", $enumValues)."']"; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
return ''; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
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: