1 | <?php |
||
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 |
||
105 | |||
106 | /** |
||
107 | * Gets old name for renaming column |
||
108 | * |
||
109 | * @param $rule |
||
110 | * @return string |
||
111 | */ |
||
112 | public static function getRenameFrom($rule): string |
||
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 |
||
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 |
||
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 |
||
167 | |||
168 | /** |
||
169 | * Checks if columns is unsigned |
||
170 | * |
||
171 | * @param $rule |
||
172 | * @return boolean |
||
173 | */ |
||
174 | public static function isUnsigned($rule): bool |
||
179 | |||
180 | |||
181 | /** |
||
182 | * Checks if columns has changed |
||
183 | * |
||
184 | * @param $rule |
||
185 | * @return boolean |
||
186 | */ |
||
187 | public static function hasChanged($rule): bool |
||
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 |
||
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 |
||
218 | } |
||
219 | |||
220 |