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