Skip to content

Commit 351bc7d

Browse files
committed
Fix multiple bugs. Auto handles more validation rules
1 parent 0e0718f commit 351bc7d

File tree

8 files changed

+87
-12
lines changed

8 files changed

+87
-12
lines changed

config/codegenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@
130130
| field's type will automaticly be set to datetime.
131131
|
132132
*/
133-
'common_datetime_patterns' => ['*_at'],
133+
'common_datetime_patterns' => ['*_at','*_date'],
134134

135135
/*
136136
|--------------------------------------------------------------------------

src/Commands/CreateModelCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,8 @@ protected function makeArrayString(array $names, $index = 0)
312312
*/
313313
protected function getCommandInput()
314314
{
315-
$table = trim($this->option('table-name')) ?: strtolower(str_plural(trim($this->argument('model-name'))));
315+
$modelName = trim($this->argument('model-name'));
316+
$table = trim($this->option('table-name')) ?: $this->makeTableName($modelName);
316317
$fillable = trim($this->option('fillable'));
317318
$primaryKey = trim($this->option('primary-key'));
318319
$relationships = !empty(trim($this->option('relationships'))) ? explode(',', trim($this->option('relationships'))) : [];
@@ -321,7 +322,6 @@ protected function getCommandInput()
321322
$fields = trim($this->option('fields'));
322323
$fieldsFile = trim($this->option('fields-file'));
323324
$template = $this->getTemplateName();
324-
$modelName = ucfirst(str_singular($table));
325325

326326
return (object) compact('table', 'fillable', 'primaryKey', 'relationships', 'useSoftDelete', 'useTimeStamps', 'fields', 'fieldsFile', 'template', 'modelName');
327327
}

src/Commands/CreateResourcesCommand.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,22 +270,23 @@ protected function createModel($input)
270270
protected function getCommandInput()
271271
{
272272
$modelName = trim($this->argument('model-name'));
273-
$modelNamePlural = strtolower(str_plural($modelName));
273+
//$modelNamePlural = strtolower(str_plural($modelName));
274+
$madeupTableName = $this->makeTableName($modelName);
274275
$controllerName = trim($this->option('controller-name') ?: ucfirst(Helpers::postFixWith(str_plural($modelName), 'Controller')));
275276
$viewsDirectory = $this->option('views-directory');
276277
$prefix = $this->option('routes-prefix');
277-
$prefix = $prefix == 'model-name-as-plural' ? $modelNamePlural : $prefix;
278+
$prefix = $prefix == 'model-name-as-plural' ? $madeupTableName : $prefix;
278279
$perPage = intval($this->option('models-per-page'));
279280
$fields = $this->option('fields');
280281
$fieldsFile = $this->option('fields-file');
281-
$languageFileName = $this->option('lang-file-name') ?: $modelNamePlural;
282+
$languageFileName = $this->option('lang-file-name') ?: $madeupTableName;
282283
$formRequest = $this->option('with-form-request');
283284
$controllerDirectory = $this->option('controller-directory');
284285
$controllerExtends = $this->option('controller-extends') ?: null;
285286
$withoutMigration = $this->option('without-migration');
286287
$force = $this->option('force');
287288
$modelDirectory = $this->option('model-directory');
288-
$table = $this->option('table-name') ?: $modelNamePlural;
289+
$table = $this->option('table-name') ?: $madeupTableName;
289290
$fillable = $this->option('fillable');
290291
$primaryKey = $this->option('primary-key');
291292
$relationships = $this->option('relationships');

src/Models/Field.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,7 @@ public function toArray()
596596
'is-inline-options' => $this->isInlineOptions,
597597
'is-multiple-answers' => $this->isMultipleAnswers,
598598
'is-date' => $this->isDate,
599+
'date-format' => $this->dateFormat,
599600
'cast-as' => $this->castAs,
600601
'placeholder' => $this->placeHolder,
601602
'delimiter' => $this->optionsDelimiter,
@@ -705,6 +706,24 @@ public function isDateOrTime()
705706
return $this->isDate() || $this->isDateTime() || $this->isTime() || $this->isTimeStamp();
706707
}
707708

709+
/**
710+
* Gets date validation rule for a datetime field.
711+
*
712+
* @return mix (null | string)
713+
*/
714+
public function getDateValidationRule()
715+
{
716+
if ($this->isDateOrTime()) {
717+
if (!empty($this->dateFormat)) {
718+
return sprintf('date_format:%s', $this->dateFormat);
719+
}
720+
721+
return 'date';
722+
}
723+
724+
return null;
725+
}
726+
708727
/**
709728
* Checks if the data type contains decimal.
710729
*

src/Support/.php_cs.cache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"php":"7.1.1","version":"2.3.1:v2.3.1#d5257f7433bb490299c4f300d95598fd911a8ab0","rules":{"blank_line_after_namespace":true,"braces":true,"class_definition":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_constants":true,"lowercase_keywords":true,"method_argument_space":true,"no_closing_tag":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_import_per_statement":true,"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"visibility_required":true,"encoding":true,"full_opening_tag":true},"hashes":{"FieldTransformer.php":-1158955351,"FieldOptimizer.php":718225636,"FieldsOptimizer.php":815514108,"Helpers.php":-2064356608,"ViewsCommand.php":516344620,"ValidationParser.php":-513830996,"CrestAppsTranslator.php":-558689661,"Config.php":1657619464}}
1+
{"php":"7.1.1","version":"2.3.1:v2.3.1#d5257f7433bb490299c4f300d95598fd911a8ab0","rules":{"blank_line_after_namespace":true,"braces":true,"class_definition":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_constants":true,"lowercase_keywords":true,"method_argument_space":true,"no_closing_tag":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_import_per_statement":true,"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"visibility_required":true,"encoding":true,"full_opening_tag":true},"hashes":{"FieldTransformer.php":-1158955351,"FieldOptimizer.php":1044313434,"FieldsOptimizer.php":815514108,"Helpers.php":-2064356608,"ViewsCommand.php":516344620,"ValidationParser.php":-513830996,"CrestAppsTranslator.php":-558689661,"Config.php":1657619464}}

src/Support/FieldOptimizer.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ protected function optimizeStringField()
104104
*/
105105
protected function optimizeRequiredField()
106106
{
107-
if ( !array_key_exists('is-nullable', $this->meta) && ($this->parser->isNullable() || !$this->parser->isRequired() || $this->parser->isConditionalRequired())) {
107+
if (!array_key_exists('is-nullable', $this->meta) && ($this->parser->isNullable() || !$this->parser->isRequired() || $this->parser->isConditionalRequired())) {
108108
$this->field->isNullable = true;
109109
}
110110

@@ -132,7 +132,7 @@ protected function optimizeDateFields()
132132
*/
133133
protected function addPlaceHolder()
134134
{
135-
if(empty($this->field->placeHolder) && $this->field->hasForeignRelation()) {
135+
if (empty($this->field->placeHolder) && $this->field->hasForeignRelation()) {
136136
$this->field->placeHolder = 'Please select a ' . $this->field->getForeignRelation()->name;
137137
}
138138

@@ -153,6 +153,14 @@ protected function optimizeValidations()
153153
return $rule != 'required';
154154
});
155155
}
156+
157+
if (!$this->field->isNullable && !in_array('required', $this->field->validationRules)) {
158+
$this->field->validationRules[] = 'required';
159+
}
160+
161+
if (($rule = $this->field->getDateValidationRule()) != null && !in_array($rule, $this->field->validationRules)) {
162+
$this->field->validationRules[] = $rule;
163+
}
156164

157165
if (! $this->field->isOnFormView) {
158166
// At this point we know the field is not going to be on any request form

src/Support/FieldTransformer.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,11 +234,11 @@ protected function transferField(array $properties)
234234
$this->setPredefindProperties($field, $properties)
235235
->setDataType($field, $properties)
236236
->setOptionsProperty($field, $properties)
237-
->setValidationProperty($field, $properties)
238237
->setLabelsProperty($field, $properties)
239238
->setDataTypeParams($field, $properties)
240239
->setMultipleAnswers($field, $properties)
241240
->setUnsignedProperty($field, $properties)
241+
->setValidationProperty($field, $properties)
242242
->setForeignRelation($field, $properties)
243243
->setRange($field, $properties)
244244
->setForeignConstraint($field, $properties);
@@ -731,6 +731,41 @@ protected function setValidationProperty(Field & $field, array $properties)
731731
$field->validationRules = is_array($properties['validation']) ? $properties['validation'] : Helpers::removeEmptyItems(explode('|', $properties['validation']));
732732
}
733733

734+
if (Helpers::isNewerThan('5.2') && $field->isNullable) {
735+
$field->validationRules[] = 'nullable';
736+
}
737+
738+
$params = [];
739+
740+
if ($this->isKeyExists($properties, 'data-type-params')) {
741+
$params = $this->getDataTypeParams($field->dataType, (array) $properties['data-type-params']);
742+
}
743+
744+
if (in_array($field->dataType, ['char','string'])
745+
&& isset($params[0]) && ($length = intval($params[0])) > 0) {
746+
747+
if(!$this->inArraySearch($field->validationRules, 'digits_between')) {
748+
$min = $field->isRequired() ? 1 : 0;
749+
$field->validationRules[] = sprintf('digits_between:%s,%s', $min, $length);
750+
}
751+
}
752+
753+
if (in_array($field->dataType, ['decimal','double','float'])
754+
&& isset($params[0]) && ($length = intval($params[0])) > 0 && isset($params[1]) && ($decimal = intval($params[1])) > 0) {
755+
756+
if(!in_array('numeric', $field->validationRules)) {
757+
$field->validationRules[] = 'numeric';
758+
}
759+
760+
if(!$this->inArraySearch($field->validationRules, 'min')) {
761+
$field->validationRules[] = sprintf('min:%s', $field->getMinValue());
762+
}
763+
764+
if(!$this->inArraySearch($field->validationRules, 'max')) {
765+
$field->validationRules[] = sprintf('max:%s', $field->getMaxValue());
766+
}
767+
}
768+
734769
return $this;
735770
}
736771

src/Traits/CommonCommand.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,18 @@ public function arguments()
114114

115115
return parent::argument();
116116
}
117+
118+
/**
119+
* Makes the table name from the giving model name.
120+
*
121+
* @param string $modelName
122+
*
123+
* @return string
124+
*/
125+
protected function makeTableName($modelName)
126+
{
127+
return snake_case(lcfirst(str_plural($modelName)));
128+
}
117129

118130
/**
119131
* Gets the indentation count.
@@ -125,7 +137,7 @@ public function arguments()
125137
*/
126138
protected function getIndent($stub, $template)
127139
{
128-
$lines = explode(PHP_EOL, $stub);
140+
$lines = explode("\n", $stub);
129141

130142
foreach ($lines as $line) {
131143
if (($index = strpos($line, $template)) !== false) {

0 commit comments

Comments
 (0)