|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mtolhuys\LaravelSchematics\Services; |
|
4
|
|
|
|
|
5
|
|
|
class RuleParser |
|
6
|
|
|
{ |
|
7
|
|
|
public static $columnTypes = [ |
|
8
|
|
|
'date', |
|
9
|
|
|
'dateTime', |
|
10
|
|
|
'decimal', |
|
11
|
|
|
'integer', |
|
12
|
|
|
'string', |
|
13
|
|
|
'text', |
|
14
|
|
|
'time', |
|
15
|
|
|
'timestamp' |
|
16
|
|
|
]; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Parse the rules to column creation methods |
|
20
|
|
|
* |
|
21
|
|
|
* @param $rules |
|
22
|
|
|
* @return string |
|
23
|
|
|
*/ |
|
24
|
|
|
public static function rulesToMigrationColumns(array $rules): string |
|
25
|
|
|
{ |
|
26
|
|
|
$columns = '$table->increments(\'id\');' . PHP_EOL; |
|
27
|
|
|
|
|
28
|
|
|
foreach ($rules as $column => $rule) { |
|
29
|
|
|
$type = self::getType($rule); |
|
30
|
|
|
$max = self::getMax($rule); |
|
31
|
|
|
$nullable = self::isRequired($rule) ? '' : '->nullable()'; |
|
32
|
|
|
$unique = self::isUnique($rule) ? '->unique()' : ''; |
|
33
|
|
|
|
|
34
|
|
|
$columns .= |
|
35
|
|
|
str_repeat(' ', 12) . |
|
36
|
|
|
"\$table->{$type}('$column'{$max}){$nullable}{$unique};" . |
|
37
|
|
|
PHP_EOL; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
return $columns . str_repeat(' ', 12) . '$table->timestamps();'; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Fill the $fillable, mostly for cosmetics |
|
45
|
|
|
* |
|
46
|
|
|
* @param $rules |
|
47
|
|
|
* @return string |
|
48
|
|
|
*/ |
|
49
|
|
|
public static function rulesToFillables($rules): string |
|
50
|
|
|
{ |
|
51
|
|
|
$fillables = ''; |
|
52
|
|
|
|
|
53
|
|
|
foreach (array_keys($rules) as $index => $column) { |
|
54
|
|
|
if (count($rules) === 1) { |
|
55
|
|
|
return "'$column'"; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
if ($index === 0) { |
|
59
|
|
|
$fillables .= "'$column'," . PHP_EOL; |
|
60
|
|
|
} elseif ($index === count($rules) - 1) { |
|
61
|
|
|
$fillables .= str_repeat(' ', 8) . "'$column'"; |
|
62
|
|
|
} else { |
|
63
|
|
|
$fillables .= str_repeat(' ', 8) . "'$column'," . PHP_EOL; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return $fillables; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Check if $rule contains any of the supported type |
|
72
|
|
|
* WARNING: be aware of word length vs. word matching f.e. 'date' <-> 'dateTime' |
|
73
|
|
|
* In that case the longest word should appear last in the array |
|
74
|
|
|
* |
|
75
|
|
|
* @param $rule |
|
76
|
|
|
* @return mixed|string |
|
77
|
|
|
*/ |
|
78
|
|
|
public static function getType($rule) |
|
79
|
|
|
{ |
|
80
|
|
|
foreach (self::$columnTypes as $type) { |
|
81
|
|
|
if (stripos($rule, strtolower($type)) !== false) { |
|
82
|
|
|
return $type; |
|
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
|
|
|
* Checks if column can be set to nullable |
|
108
|
|
|
* |
|
109
|
|
|
* @param $rule |
|
110
|
|
|
* @return boolean |
|
111
|
|
|
*/ |
|
112
|
|
|
public static function isRequired($rule): bool |
|
113
|
|
|
{ |
|
114
|
|
|
return self::contains($rule, 'required') |
|
115
|
|
|
|| self::isUnique($rule); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Checks if columns needs to be set to unique |
|
120
|
|
|
* |
|
121
|
|
|
* @param $rule |
|
122
|
|
|
* @return boolean |
|
123
|
|
|
*/ |
|
124
|
|
|
public static function isUnique($rule): bool |
|
125
|
|
|
{ |
|
126
|
|
|
return self::contains($rule, 'unique'); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
private static function contains($rule, $needle): bool |
|
130
|
|
|
{ |
|
131
|
|
|
return stripos($rule, $needle) !== false; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
|