|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mtolhuys\LaravelSchematics\Services; |
|
4
|
|
|
|
|
5
|
|
|
use Mtolhuys\LaravelSchematics\Models\Migration; |
|
6
|
|
|
|
|
7
|
|
|
class RuleParser |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Parse the rules to column creation methods |
|
11
|
|
|
* |
|
12
|
|
|
* @param $fields |
|
13
|
|
|
* @return string |
|
14
|
|
|
*/ |
|
15
|
|
|
public static function fieldsToMigrationMethods(array $fields): string |
|
16
|
|
|
{ |
|
17
|
|
|
$columns = ''; |
|
18
|
|
|
|
|
19
|
|
|
foreach ($fields as $field) { |
|
20
|
|
|
$column = key($field); |
|
21
|
|
|
$rule = $field[$column]; |
|
22
|
|
|
$break = PHP_EOL . str_repeat(' ', 12); |
|
23
|
|
|
|
|
24
|
|
|
if (self::isMethodOnly($rule)) { |
|
25
|
|
|
$columns .= "\$table->{$rule}();{$break}"; |
|
26
|
|
|
|
|
27
|
|
|
continue; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
$max = self::getMax($rule); |
|
31
|
|
|
$method = self::getMethod($rule); |
|
32
|
|
|
$oldName = self::getRenameFrom($rule); |
|
33
|
|
|
$additional = self::getAdditionalUpMethods($rule); |
|
34
|
|
|
|
|
35
|
|
|
$columns .= "\$table->{$method}({$oldName}'$column'{$max}){$additional}{$break}"; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
return $columns; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param $rule |
|
43
|
|
|
* @return string |
|
44
|
|
|
*/ |
|
45
|
|
|
public static function getAdditionalUpMethods($rule): string |
|
46
|
|
|
{ |
|
47
|
|
|
$methods = ''; |
|
48
|
|
|
$methods .= self::isUnsigned($rule) ? '->unsigned()' : ''; |
|
49
|
|
|
$methods .= !self::isRequired($rule) ? '->nullable()' : ''; |
|
50
|
|
|
$methods .= self::isUnique($rule) ? '->unique()' : ''; |
|
51
|
|
|
$methods .= self::hasChanged($rule) ? '->change()' : ''; |
|
52
|
|
|
|
|
53
|
|
|
return "$methods;"; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Check if $rule should be handled as single method |
|
58
|
|
|
* |
|
59
|
|
|
* @param $rule |
|
60
|
|
|
* @return mixed|string |
|
61
|
|
|
*/ |
|
62
|
|
|
public static function isMethodOnly($rule) |
|
63
|
|
|
{ |
|
64
|
|
|
|
|
65
|
|
|
return self::ruleContains($rule, [ |
|
66
|
|
|
'softDeletes', |
|
67
|
|
|
'rememberToken', |
|
68
|
|
|
'softDeletesTz', |
|
69
|
|
|
]); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Check if $rule contains any of the possible methods |
|
74
|
|
|
* |
|
75
|
|
|
* @param $rule |
|
76
|
|
|
* @return mixed|string |
|
77
|
|
|
*/ |
|
78
|
|
|
public static function getMethod($rule) |
|
79
|
|
|
{ |
|
80
|
|
|
foreach (Migration::$methods as $method) { |
|
81
|
|
|
if (stripos($rule, strtolower($method)) !== false) { |
|
82
|
|
|
return $method; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return 'string'; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Parses the max:* rule |
|
91
|
|
|
* |
|
92
|
|
|
* @param $rule |
|
93
|
|
|
* @return string |
|
94
|
|
|
*/ |
|
95
|
|
|
public static function getMax($rule): string |
|
96
|
|
|
{ |
|
97
|
|
|
$max = (int)substr($rule, strpos($rule, 'max:') + 4); |
|
98
|
|
|
|
|
99
|
|
|
if ($max > 0) { |
|
100
|
|
|
return ", $max"; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return ''; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Gets old name for renaming column |
|
108
|
|
|
* |
|
109
|
|
|
* @param $rule |
|
110
|
|
|
* @return string |
|
111
|
|
|
*/ |
|
112
|
|
|
public static function getRenameFrom($rule): string |
|
113
|
|
|
{ |
|
114
|
|
|
foreach (explode('|', $rule) as $token) { |
|
115
|
|
|
$hasRenameRule = stripos($token, 'from:') !== false; |
|
116
|
|
|
|
|
117
|
|
|
if ($hasRenameRule) { |
|
118
|
|
|
$from = substr($token, strpos($token, 'from:') + 5); |
|
119
|
|
|
|
|
120
|
|
|
return "'$from', "; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return ''; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Checks if column can be set to nullable |
|
129
|
|
|
* |
|
130
|
|
|
* @param $rule |
|
131
|
|
|
* @return boolean |
|
132
|
|
|
*/ |
|
133
|
|
|
public static function isRequired($rule): bool |
|
134
|
|
|
{ |
|
135
|
|
|
return self::ruleContains($rule, ['required']) |
|
136
|
|
|
|| self::isIncrements($rule) |
|
137
|
|
|
|| self::isUnique($rule); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Checks if columns needs to be set to unique |
|
142
|
|
|
* |
|
143
|
|
|
* @param $rule |
|
144
|
|
|
* @return boolean |
|
145
|
|
|
*/ |
|
146
|
|
|
public static function isIncrements($rule): bool |
|
147
|
|
|
{ |
|
148
|
|
|
return self::ruleContains($rule, [ |
|
149
|
|
|
'increments', |
|
150
|
|
|
'bigIncrements', |
|
151
|
|
|
'mediumIncrements', |
|
152
|
|
|
'smallIncrements', |
|
153
|
|
|
'tinyIncrements', |
|
154
|
|
|
]); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Checks if columns needs to be set to unique |
|
159
|
|
|
* |
|
160
|
|
|
* @param $rule |
|
161
|
|
|
* @return boolean |
|
162
|
|
|
*/ |
|
163
|
|
|
public static function isUnique($rule): bool |
|
164
|
|
|
{ |
|
165
|
|
|
return self::ruleContains($rule, ['unique']); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Checks if columns is unsigned |
|
170
|
|
|
* |
|
171
|
|
|
* @param $rule |
|
172
|
|
|
* @return boolean |
|
173
|
|
|
*/ |
|
174
|
|
|
public static function isUnsigned($rule): bool |
|
175
|
|
|
{ |
|
176
|
|
|
return self::ruleContains($rule, ['unsigned']) |
|
177
|
|
|
&& ! self::ruleContains($rule, ['unsignedInteger']); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Checks if columns has changed |
|
183
|
|
|
* |
|
184
|
|
|
* @param $rule |
|
185
|
|
|
* @return boolean |
|
186
|
|
|
*/ |
|
187
|
|
|
public static function hasChanged($rule): bool |
|
188
|
|
|
{ |
|
189
|
|
|
return self::ruleContains($rule, ['change']); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Checks if columns needs to be constructed as a foreign key |
|
194
|
|
|
* |
|
195
|
|
|
* @param $rule |
|
196
|
|
|
* @return boolean |
|
197
|
|
|
*/ |
|
198
|
|
|
public static function isForeign($rule): bool |
|
199
|
|
|
{ |
|
200
|
|
|
return self::ruleContains($rule, ['foreign']); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* For aesthetic reasons |
|
205
|
|
|
* |
|
206
|
|
|
* @param $rule |
|
207
|
|
|
* @param $needles |
|
208
|
|
|
* @return bool |
|
209
|
|
|
*/ |
|
210
|
|
|
private static function ruleContains($rule, array $needles): bool |
|
211
|
|
|
{ |
|
212
|
|
|
return !empty(array_filter( |
|
213
|
|
|
array_map(static function ($rule) use ($needles) { |
|
214
|
|
|
return in_array($rule, $needles, true); |
|
215
|
|
|
}, explode('|', $rule)) |
|
216
|
|
|
)); |
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
|