Skip to content

Commit ad22414

Browse files
committed
Fixes multiple issues. Adds datetime and bool handling
1 parent 2afbdcb commit ad22414

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+761
-303
lines changed

src/Commands/.php_cs.cache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"php":"5.6.24","version":"2.1.2:","rules":{"encoding":true,"full_opening_tag":true,"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":["property"],"single_import_per_statement":true,"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"visibility_required":true},"hashes":{"CreateControllerCommand.php":1979890676,"CreateCreateViewCommand.php":131691758,"CreateEditViewCommand.php":1171483022,"CreateFieldsFileCommand.php":1361178139,"CreateFormRequestCommand.php":194589446,"CreateFormViewCommand.php":623765643,"CreateIndexViewCommand.php":-1734787902,"CreateLanguageCommand.php":-1844147042,"CreateLayoutCommand.php":396331127,"CreateMigrationCommand.php":1405553898,"CreateModelCommand.php":-1793560043,"CreateResourcesCommand.php":-1298335991,"CreateRoutesCommand.php":1075556265,"CreateShowViewCommand.php":2103655095,"CreateViewLayoutCommand.php":213476386,"CreateViewsCommand.php":-986690200}}
1+
{"php":"5.6.24","version":"2.1.2:","rules":{"encoding":true,"full_opening_tag":true,"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":["property"],"single_import_per_statement":true,"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"visibility_required":true},"hashes":{"CreateControllerCommand.php":1668938608,"CreateCreateViewCommand.php":131691758,"CreateEditViewCommand.php":1171483022,"CreateFieldsFileCommand.php":1361178139,"CreateFormRequestCommand.php":194589446,"CreateFormViewCommand.php":623765643,"CreateIndexViewCommand.php":-1734787902,"CreateLanguageCommand.php":-1844147042,"CreateLayoutCommand.php":396331127,"CreateMigrationCommand.php":1405553898,"CreateModelCommand.php":271158263,"CreateResourcesCommand.php":-1298335991,"CreateRoutesCommand.php":1075556265,"CreateShowViewCommand.php":2103655095,"CreateViewLayoutCommand.php":213476386,"CreateViewsCommand.php":-986690200}}

src/Commands/CreateControllerCommand.php

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ protected function buildClass($name)
8585
->replaceFormRequestName($stub, $formRequestName)
8686
->replaceFormRequestFullName($stub, $this->getRequestsNamespace() . $formRequestName)
8787
->replacePaginationNumber($stub, $input->perPage)
88-
->replaceFileSnippet($stub, $this->getFileReadySnippet($fields))
88+
->replaceFileSnippet($stub, $this->getFileSnippet($fields))
89+
->replaceBooleadSnippet($stub, $this->getBooleanSnippet($fields))
90+
->replaceStringToNullSnippet($stub, $this->getStringToNullSnippet($fields))
8991
->replaceFileMethod($stub, $this->getUploadFileMethod($fields))
9092
->replaceClass($stub, $name);
9193
}
@@ -287,6 +289,36 @@ protected function replaceFileSnippet(&$stub, $snippet)
287289
return $this;
288290
}
289291

292+
/**
293+
* Replaces the boolean snippet for the given stub.
294+
*
295+
* @param string $stub
296+
* @param string $snippet
297+
*
298+
* @return $this
299+
*/
300+
protected function replaceBooleadSnippet(&$stub, $snippet)
301+
{
302+
$stub = str_replace('{{booleanSnippet}}', $snippet, $stub);
303+
304+
return $this;
305+
}
306+
307+
/**
308+
* Replaces the form-request's name for the given stub.
309+
*
310+
* @param string $stub
311+
* @param string $snippet
312+
*
313+
* @return $this
314+
*/
315+
protected function replaceStringToNullSnippet(&$stub, $snippet)
316+
{
317+
$stub = str_replace('{{stringToNullSnippet}}', $snippet, $stub);
318+
319+
return $this;
320+
}
321+
290322
/**
291323
* Replaces the upload-method's code for the given stub.
292324
*
@@ -342,17 +374,58 @@ public function getNameInput()
342374
*
343375
* @return string
344376
*/
345-
protected function getFileReadySnippet(array $fields)
377+
protected function getFileSnippet(array $fields)
346378
{
347379
$code = '';
348380

349381
foreach ($fields as $field) {
350382
if ($field->isFile()) {
351-
$code = ($code) ?: '$this';
383+
$code = ($code) ?: ' $this';
352384
$code .= sprintf("->uploadFile('%s', \$data)", $field->name);
353385
}
354386
}
355387

356388
return $code != '' ? $code . ';' : $code;
357389
}
390+
391+
/**
392+
* Gets the code that is needed to check for bool property.
393+
*
394+
* @param array $fields
395+
*
396+
* @return string
397+
*/
398+
protected function getBooleanSnippet(array $fields)
399+
{
400+
$code = '';
401+
402+
foreach ($fields as $field) {
403+
if ($field->isBoolean() && $field->isCheckbox()) {
404+
$code .= sprintf(" \$data['%s'] = \$request->has('%s');", $field->name, $field->name) . PHP_EOL;
405+
}
406+
}
407+
408+
return $code;
409+
}
410+
411+
412+
/**
413+
* Gets the code that is needed to convert empty string to null.
414+
*
415+
* @param array $fields
416+
*
417+
* @return string
418+
*/
419+
protected function getStringToNullSnippet(array $fields)
420+
{
421+
$code = '';
422+
423+
foreach ($fields as $field) {
424+
if ($field->isNullable && !$field->isPrimary && !$field->isAutoIncrement && !$field->isRequired() && !$field->isBoolean()) {
425+
$code .= sprintf(" \$data['%s'] = !empty(\$request->input('%s')) ? \$request->input('%s') : null;", $field->name, $field->name, $field->name) . PHP_EOL;
426+
}
427+
}
428+
429+
return $code;
430+
}
358431
}

src/Commands/CreateModelCommand.php

Lines changed: 65 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,16 @@ protected function getAccessors(array $fields = null)
297297

298298
foreach ($fields as $field) {
299299
if ($field->isMultipleAnswers) {
300-
$accessors[] = $this->getAccessor($field);
300+
$content = $this->getStubContent('model-accessor-multiple-answers');
301+
302+
$accessors[] = $this->getAccessor($field, $content);
303+
}
304+
305+
if ($field->isDateOrTime()) {
306+
$content = $this->getStubContent('model-accessor-datetime');
307+
$this->replaceDateFormat($content, $field->dateFormat);
308+
309+
$accessors[] = $this->getAccessor($field, $content);
301310
}
302311
}
303312

@@ -317,7 +326,17 @@ protected function getMutators(array $fields = null)
317326

318327
foreach ($fields as $field) {
319328
if ($field->isMultipleAnswers) {
320-
$mutators[] = $this->getMutator($field);
329+
$content = $this->getStubContent('model-mutator-multiple-answers');
330+
$this->replaceFieldName($content, $field->name);
331+
332+
$mutators[] = $this->getMutator($field, $content);
333+
}
334+
335+
if ($field->isDateOrTime()) {
336+
$content = $this->getStubContent('model-mutator-datetime');
337+
$this->replaceFieldName($content, $field->name);
338+
339+
$mutators[] = $this->getMutator($field, $content);
321340
}
322341
}
323342

@@ -328,15 +347,15 @@ protected function getMutators(array $fields = null)
328347
* Gets accessor for a giving field.
329348
*
330349
* @param CrestApps\CodeGenerator\Models\Field $field
331-
*
350+
* @param string $content
332351
* @return string
333352
*/
334-
protected function getAccessor(Field $field)
353+
protected function getAccessor(Field $field, $content)
335354
{
336355
$stub = $this->getStubContent('model-accessor');
337356

338357
$this->replaceFieldName($stub, $field->name)
339-
->replaceDelimiter($stub, $field->optionsDelimiter);
358+
->replaceFieldContent($stub, $content);
340359

341360
return $stub;
342361
}
@@ -345,19 +364,52 @@ protected function getAccessor(Field $field)
345364
* Gets mutator for a giving field.
346365
*
347366
* @param CrestApps\CodeGenerator\Models\Field $field
367+
* @param string $content
348368
*
349369
* @return string
350370
*/
351-
protected function getMutator(Field $field)
371+
protected function getMutator(Field $field, $content)
352372
{
353373
$stub = $this->getStubContent('model-mutator');
354374

355375
$this->replaceFieldName($stub, $field->name)
356-
->replaceDelimiter($stub, $field->optionsDelimiter);
376+
->replaceFieldContent($stub, $content);
357377

358378
return $stub;
359379
}
360380

381+
382+
383+
/**
384+
* Replaces date format for the giving field.
385+
*
386+
* @param string $stub
387+
* @param string $format
388+
*
389+
* @return $this
390+
*/
391+
protected function replaceDateFormat(&$stub, $format)
392+
{
393+
$stub = str_replace('{{dateFormat}}', $format, $stub);
394+
395+
return $this;
396+
}
397+
398+
/**
399+
* Replaces content of the giving stub.
400+
*
401+
* @param string $stub
402+
* @param string $content
403+
*
404+
* @return $this
405+
*/
406+
protected function replaceFieldContent(&$stub, $content)
407+
{
408+
$stub = str_replace('{{content}}', $content, $stub);
409+
410+
return $this;
411+
}
412+
361413
/**
362414
* Replaces the table for the given stub.
363415
*
@@ -445,7 +497,7 @@ protected function replaceFieldName(&$stub, $name)
445497
{
446498
$stub = str_replace('{{fieldName}}', $name, $stub);
447499

448-
$stub = str_replace('{{fieldNameCap}}', ucfirst($name), $stub);
500+
$stub = str_replace('{{fieldNameCap}}', ucwords(camel_case($name)), $stub);
449501

450502
return $this;
451503
}
@@ -463,11 +515,13 @@ protected function replaceSoftDelete(&$stub, $shouldUseSoftDelete)
463515
if ($shouldUseSoftDelete) {
464516
$stub = str_replace('{{useSoftDelete}}', PHP_EOL . 'use Illuminate\Database\Eloquent\SoftDeletes;' . PHP_EOL, $stub);
465517
$stub = str_replace('{{useSoftDeleteTrait}}', PHP_EOL . ' use SoftDeletes;' . PHP_EOL, $stub);
466-
} else {
467-
$stub = str_replace('{{useSoftDelete}}', null, $stub);
468-
$stub = str_replace('{{useSoftDeleteTrait}}', null, $stub);
518+
519+
return $this;
469520
}
470521

522+
$stub = str_replace('{{useSoftDelete}}', null, $stub);
523+
$stub = str_replace('{{useSoftDeleteTrait}}', null, $stub);
524+
471525
return $this;
472526
}
473527

src/Commands/CreateShowViewCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ protected function handleCreateView()
5757
if ($this->canCreateView($destenationFile, $input->force, $fields)) {
5858
$this->replaceCommonTemplates($stub, $input)
5959
->replacePrimaryKey($stub, $this->getPrimaryKeyName($fields))
60-
->replaceTableRows($stub, $htmlCreator->getShowRowsHtmlField())
60+
->replaceTableRows($stub, $htmlCreator->getShowRowsHtml())
6161
->replaceModelHeader($stub, $this->getHeaderFieldAccessor($fields, $input->modelName))
6262
->createViewFile($stub, $destenationFile)
6363
->info('Show view was crafted successfully.');

src/HtmlGenerators/.php_cs.cache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"php":"5.6.24","version":"2.1.2:","rules":{"encoding":true,"full_opening_tag":true,"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":["property"],"single_import_per_statement":true,"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"visibility_required":true},"hashes":{"GenerateFormViews.php":945491809,"HtmlGeneratorBase.php":1920424724,"LaravelCollectiveHtml.php":688927201,"StandardHtml.php":798515697}}
1+
{"php":"5.6.24","version":"2.1.2:","rules":{"encoding":true,"full_opening_tag":true,"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":["property"],"single_import_per_statement":true,"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"visibility_required":true},"hashes":{"GenerateFormViews.php":945491809,"HtmlGeneratorBase.php":260638370,"LaravelCollectiveHtml.php":382217718,"StandardHtml.php":967239408}}

0 commit comments

Comments
 (0)