|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* |
|
4
|
|
|
* Licensed under The MIT License |
|
5
|
|
|
* For full copyright and license information, please see the LICENSE.txt |
|
6
|
|
|
* Redistributions of files must retain the above copyright notice. |
|
7
|
|
|
* |
|
8
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License |
|
9
|
|
|
* @author Terry Cullen - [email protected] |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
require_once __DIR__ . "/../../../autoload.php"; |
|
13
|
|
|
require_once __DIR__ . "/functions.php"; |
|
14
|
|
|
require_once __DIR__ . "../src/Inflector.php"; |
|
15
|
|
|
|
|
16
|
|
|
use Terah\FluentPdoModel\Inflector; |
|
17
|
|
|
|
|
18
|
|
|
$params = parseArgs($_SERVER); |
|
19
|
|
|
|
|
20
|
|
|
function generateModel($table, $allColumns, $foreignKeys, $modelsPath) |
|
21
|
|
|
{ |
|
22
|
|
|
$columns = $allColumns[$table]; |
|
23
|
|
|
$params = [ |
|
24
|
|
|
'table' => $table, |
|
25
|
|
|
'class_name' => Inflector::classify($table), |
|
26
|
|
|
'validations' => generateValidationRules($columns), |
|
27
|
|
|
'columns' => getPrettyColumns($columns), |
|
28
|
|
|
'display_col' => getDisplayColumn($columns), |
|
29
|
|
|
'auto_joins' => !empty($foreignKeys['foreign_keys'][$table]) ? getAutoJoins($foreignKeys['foreign_keys'][$table], $allColumns) : '', |
|
30
|
|
|
]; |
|
31
|
|
|
$output = renderTemplate($params); |
|
32
|
|
|
//$associations = '';//static::generateAssociations($table, $foreignKeys, $columns); |
|
33
|
|
|
|
|
34
|
|
|
$file = $modelsPath . $params['class_name'] . '.php'; |
|
35
|
|
|
$keep = file_exists($file) ? StringUtils::between('//START', '//END', file_get_contents($file)) : ''; |
|
36
|
|
|
$keep = trim($keep); |
|
37
|
|
|
$tpl = str_replace('//START//END', "//START\n\n\t{$keep}\n\n //END", $output); |
|
38
|
|
|
FileUtils::createDirectoriesAndSaveFile($file, $tpl); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
function generateValidationRules($columns) |
|
42
|
|
|
{ |
|
43
|
|
|
$rules = []; |
|
44
|
|
|
foreach ( $columns as $column => $column ) |
|
45
|
|
|
{ |
|
46
|
|
|
$rules[$column] = [ |
|
47
|
|
|
'field' => $column, |
|
48
|
|
|
'rule' => generateValidationRule($column) |
|
49
|
|
|
]; |
|
50
|
|
|
} |
|
51
|
|
|
return $rules; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
function generateValidationRule($column, $js = false) |
|
55
|
|
|
{ |
|
56
|
|
|
$jRules = []; |
|
57
|
|
|
$assert = []; |
|
58
|
|
|
$label = getPrettyColumnName($column); |
|
59
|
|
|
$errorName = str_replace('?', '', $label); |
|
60
|
|
|
$nullable = $column->is_nullable === 'NO' ? false : true; |
|
61
|
|
|
$numeric = in_array($column->data_type, ['int', 'tinyint', 'decimal']); |
|
62
|
|
|
if ( ! $nullable ) |
|
63
|
|
|
{ |
|
64
|
|
|
$assert['not_empty'] = $numeric ? "numeric('{$errorName} must not be empty.')" : "notBlank('{$errorName} must not be empty.')"; |
|
65
|
|
|
$jRules['required'] = true; |
|
66
|
|
|
$jRules['data-bv-notempty-message'] = "{$errorName} must not be empty."; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
switch ($column->data_type) |
|
70
|
|
|
{ |
|
71
|
|
|
case 'text': |
|
72
|
|
|
|
|
73
|
|
|
$jRules['maxlength'] = '10000'; |
|
74
|
|
|
$jRules['data-bv-stringlength-message'] = "{$errorName} cannot exceed 10000 characters, (%s) submitted."; |
|
75
|
|
|
$assert['utf-8'] = "utf8('{$errorName} has to be UTF-8 characters only, (%s) submitted.')"; |
|
76
|
|
|
$assert['maxlength'] = "maxLength(10000, '{$errorName} cannot exceed 10000 characters, (%s) submitted.')"; |
|
77
|
|
|
break; |
|
78
|
|
|
|
|
79
|
|
|
case 'varchar': |
|
80
|
|
|
|
|
81
|
|
|
$jRules['maxlength'] = $column->character_maximum_length; |
|
82
|
|
|
$jRules['data-bv-stringlength-message'] = "{$errorName} cannot exceed {$column->character_maximum_length} characters."; |
|
83
|
|
|
$assert['utf-8'] = "utf8('The value has to be UTF-8 characters only, (%s) submitted.')"; |
|
84
|
|
|
$assert['maxlength'] = "maxLength($column->character_maximum_length, '{$errorName} cannot exceed {$column->character_maximum_length} characters.')"; |
|
85
|
|
|
break; |
|
86
|
|
|
|
|
87
|
|
|
case 'int': |
|
88
|
|
|
|
|
89
|
|
|
$assert['integer'] = "integer('{$errorName} must be a integer, (%s) submitted.')"; |
|
90
|
|
|
|
|
91
|
|
|
$jRules['data-bv-lessthan-value'] = ($column->numeric_precision + 1); |
|
92
|
|
|
$jRules['data-bv-lessthan-message'] = "{$errorName} cannot exceed {$jRules['data-bv-lessthan-value']}."; |
|
93
|
|
|
$jRules['data-bv-integer-message'] = "{$errorName} must be a integer, (%s) submitted."; |
|
94
|
|
|
if ( $column->column_name === 'id' || preg_match('/_id$/', $column->column_name) ) |
|
95
|
|
|
{ |
|
96
|
|
|
$jRules['data-bv-greaterthan-value'] = '0'; |
|
97
|
|
|
$jRules['data-bv-greaterthan-message'] = "{$errorName} must be greater than 0."; |
|
98
|
|
|
$assert['range'] = "range(1, 4294967295, '{$errorName} must be between 1 and 4294967295, (%s) submitted.')"; |
|
99
|
|
|
} |
|
100
|
|
|
break; |
|
101
|
|
|
|
|
102
|
|
|
case 'decimal': |
|
103
|
|
|
|
|
104
|
|
|
$jRules['data-bv-lessthan-value'] = ($column->numeric_precision + 1); |
|
105
|
|
|
$jRules['data-bv-lessthan-message'] = "{$errorName} cannot exceed {$jRules['data-bv-lessthan-value']}."; |
|
106
|
|
|
$jRules['data-bv-numeric-message'] = "{$errorName} must be a number."; |
|
107
|
|
|
$jRules['data-bv-numeric-separator'] = "."; |
|
108
|
|
|
$assert['range'] = "range(-4294967295, 4294967295, '{$errorName} must be between -4294967295 and 4294967295.')"; |
|
109
|
|
|
|
|
110
|
|
|
break; |
|
111
|
|
|
|
|
112
|
|
|
case 'tinyint': |
|
113
|
|
|
|
|
114
|
|
|
$assert['boolean'] = "range(0, 1, '{$errorName} must be between 0 and 1.')"; |
|
115
|
|
|
$jRules['pattern'] = '^(1|0)$|^$'; |
|
116
|
|
|
$jRules['data-bv-between-message'] = "{$errorName} must selected."; |
|
117
|
|
|
unset($jRules['required'], $jRules['data-bv-notempty-message']); |
|
118
|
|
|
break; |
|
119
|
|
|
|
|
120
|
|
|
case 'enum': |
|
121
|
|
|
|
|
122
|
|
|
$values = str_replace("'", '', str_replace(',', '|', str_replace(['enum(', ')'], '', $column->column_type))); |
|
123
|
|
|
$list = str_replace(',', ', ', str_replace("'", '', str_replace(['enum(', ')'], '', $column->column_type))); |
|
124
|
|
|
$assertList = str_replace(['enum(', ')'], '', $column->column_type); |
|
125
|
|
|
$jRules['pattern'] = '^(' . $values . ')$|^$'; |
|
126
|
|
|
$jRules['data-bv-regexp-message'] = "{$errorName} must be one of the following: {$list}."; |
|
127
|
|
|
$assert['list'] = "inArray([$assertList], '{$errorName} must be one of the following: {$list}.')"; |
|
128
|
|
|
break; |
|
129
|
|
|
|
|
130
|
|
|
case 'date': |
|
131
|
|
|
|
|
132
|
|
|
$assert['date'] = "date('{$errorName} must be a valid date, (%s) submitted.')"; |
|
133
|
|
|
break; |
|
134
|
|
|
|
|
135
|
|
|
case 'datetime': |
|
136
|
|
|
case 'timestamp': |
|
137
|
|
|
|
|
138
|
|
|
$assert['date'] = "date('{$errorName} must be a valid timestamp, (%s) submitted.')"; |
|
139
|
|
|
break; |
|
140
|
|
|
|
|
141
|
|
|
default: |
|
142
|
|
|
|
|
143
|
|
|
} |
|
144
|
|
|
if ( preg_match('/email$/', $column->column_name) ) |
|
145
|
|
|
{ |
|
146
|
|
|
$jRules['data-bv-emailaddress-message'] = "{$errorName} must be a valid email address."; |
|
147
|
|
|
$assert['email'] = "email('{$errorName} must be a valid email address.')"; |
|
148
|
|
|
} |
|
149
|
|
|
$nullOr = $nullable ? "->nullOr()\n " : ''; |
|
150
|
|
|
$assert = " Assert(\$value)\n {$nullOr}->" . implode("\n ->", $assert); |
|
151
|
|
|
return $js ? $jRules : $assert; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
function getPrettyColumnName($meta) |
|
155
|
|
|
{ |
|
156
|
|
|
$columnName = preg_replace("/^{$meta->table_name}_/", '', $meta->column_name); |
|
157
|
|
|
$columnName = preg_replace('/_fg$/', '?', $columnName); |
|
158
|
|
|
$columnName = preg_replace('/_id$|_dt$|_ts$/', '', $columnName); |
|
159
|
|
|
$columnName = preg_replace('/^id$/', 'ID', $columnName); |
|
160
|
|
|
$columnName = preg_replace('/_am$/', '_amount', $columnName); |
|
161
|
|
|
$columnName = preg_replace('/_nm$/', '_name', $columnName); |
|
162
|
|
|
$columnName = preg_replace('/_nr$/', '_number', $columnName); |
|
163
|
|
|
$columnName = preg_replace('/ds$/', 'Description', $columnName); |
|
164
|
|
|
$columnName = preg_replace('/cd$/', 'Code', $columnName); |
|
165
|
|
|
$columnName = Inflector::humanize($columnName); |
|
166
|
|
|
return trim($columnName); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
function getPrettyColumns($columns) |
|
170
|
|
|
{ |
|
171
|
|
|
$output = ["\n"]; |
|
172
|
|
|
foreach ( $columns as $column => $column ) |
|
173
|
|
|
{ |
|
174
|
|
|
$column = str_pad("'{$column}'", 20, ' '); |
|
175
|
|
|
$output[] = <<<PHP |
|
176
|
|
|
{$column} => '{$column->data_type}', |
|
177
|
|
|
|
|
178
|
|
|
PHP; |
|
179
|
|
|
} |
|
180
|
|
|
return implode('', $output); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
function getDisplayColumn($columns) |
|
184
|
|
|
{ |
|
185
|
|
|
foreach ( $columns as $column => $column ) |
|
186
|
|
|
{ |
|
187
|
|
|
if ( preg_match('/_nm$/', $column) ) |
|
188
|
|
|
{ |
|
189
|
|
|
return $column; |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
foreach ( $columns as $column => $column ) |
|
193
|
|
|
{ |
|
194
|
|
|
if ( preg_match('/_cd$/', $column) ) |
|
195
|
|
|
{ |
|
196
|
|
|
return $column; |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
return 'id'; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
function getAutoJoins($foreignKeys, $columns) |
|
203
|
|
|
{ |
|
204
|
|
|
$output = [""]; |
|
205
|
|
|
$output[] = <<<HTML |
|
206
|
|
|
'belongsTo' => [ |
|
207
|
|
|
HTML; |
|
208
|
|
|
|
|
209
|
|
|
foreach ( $foreignKeys as $meta ) |
|
210
|
|
|
{ |
|
211
|
|
|
$displayField = getDisplayColumn($columns[$meta->referenced_table_name]); |
|
212
|
|
|
$shortName = preg_replace('/_id$/', '', $meta->column_name); |
|
213
|
|
|
$alias = Inflector::classify($shortName); |
|
214
|
|
|
$aliasPadded = str_pad("'{$alias}'", 20, ' '); |
|
215
|
|
|
$output[] = <<<HTML |
|
216
|
|
|
{$aliasPadded} => ['{$meta->referenced_table_name}', '{$meta->column_name}', '{$alias}.{$displayField} AS {$shortName}'], |
|
217
|
|
|
HTML; |
|
218
|
|
|
} |
|
219
|
|
|
$output[] = <<<HTML |
|
220
|
|
|
], |
|
221
|
|
|
|
|
222
|
|
|
HTML; |
|
223
|
|
|
return implode("\n", $output); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
function renderTemplate(array $params) |
|
227
|
|
|
{ |
|
228
|
|
|
$table = $class_name = $display_col = $schema = $auto_joins = $rules = ''; |
|
229
|
|
|
$validations = []; |
|
230
|
|
|
extract($params); |
|
231
|
|
|
foreach ( $validations as $field => $rule ) |
|
232
|
|
|
{ |
|
233
|
|
|
$rules .= <<<PHP |
|
234
|
|
|
'$field' => function(\$value, \$type) { |
|
235
|
|
|
$rule; |
|
236
|
|
|
}, |
|
237
|
|
|
PHP; |
|
238
|
|
|
} |
|
239
|
|
|
return <<<PHP |
|
240
|
|
|
<?php |
|
241
|
|
|
|
|
242
|
|
|
namespace Prism\Models; |
|
243
|
|
|
|
|
244
|
|
|
use function Terah\Assert\Assert; |
|
245
|
|
|
use function Terah\Assert\Validate; |
|
246
|
|
|
|
|
247
|
|
|
use Terah\Utils\Dates; |
|
248
|
|
|
|
|
249
|
|
|
class ^[ class_name?> extends Model |
|
250
|
|
|
{ |
|
251
|
|
|
protected \$_table_name = '$table'; |
|
252
|
|
|
|
|
253
|
|
|
protected \$_table_alias = '$class_name'; |
|
254
|
|
|
|
|
255
|
|
|
protected \$_display_column = '$display_col'; |
|
256
|
|
|
|
|
257
|
|
|
protected \$_schema = [$schema ]; |
|
258
|
|
|
|
|
259
|
|
|
protected \$_auto_joins = [$auto_joins ]; |
|
260
|
|
|
|
|
261
|
|
|
public function __getFieldHandler() |
|
262
|
|
|
{ |
|
263
|
|
|
return [ |
|
264
|
|
|
$rules |
|
265
|
|
|
]; |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
//START//END |
|
269
|
|
|
} |
|
270
|
|
|
PHP; |
|
271
|
|
|
|
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
|