Skip to content

Commit a83d3c4

Browse files
author
Mike Alhayek
committed
Add casting to the model
1 parent 3282f58 commit a83d3c4

File tree

9 files changed

+251
-67
lines changed

9 files changed

+251
-67
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "crestapps/laravel-code-generator",
33
"license": "MIT",
44
"description": "A clean code generator for Laravel framework that will save you time! This awesome tool will help you generate resources like views, controllers, routes, migrations, languages or request-forms! It is extremely flexible and customizable to cover many use cases. It is shipped with cross-browsers compatible template, along with a client-side validation to modernize your application.",
5-
"version": "v1.3.0",
5+
"version": "v2.0.0",
66
"keywords": [
77
"laravel","crud","crud generator",
88
"laravel crud generator","laravel crud builder",

src/Commands/.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":{"CreateControllerCommand.php":-832128993,"CreateLanguageCommand.php":143920987,"FieldsFileCreateCommand.php":-280774643}}
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":{"CreateControllerCommand.php":-832128993,"CreateLanguageCommand.php":143920987,"FieldsFileCreateCommand.php":-280774643,"CreateModelCommand.php":-1845010254}}

src/Commands/CreateModelCommand.php

Lines changed: 103 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ class CreateModelCommand extends Command
1414
{
1515
use CommonCommand, GeneratorReplacers;
1616

17+
/**
18+
* Total white-spaced to eliminate when creating an array string.
19+
*
20+
* @var string
21+
*/
22+
protected $backspaceCount = 8;
23+
1724
/**
1825
* The name and signature of the console command.
1926
*
@@ -79,8 +86,9 @@ public function handle()
7986
->replaceNamespace($stub, $this->getNamespace($input->modelName))
8087
->replaceSoftDelete($stub, $input->useSoftDelete)
8188
->replaceTimestamps($stub, $this->getTimeStampsStub($input->useTimeStamps))
82-
->replaceFillable($stub, $this->getFillables($input->fillable, $fields))
83-
->replaceDateFields($stub, $this->getDateFields($fields))
89+
->replaceFillable($stub, $this->getFillables($stub, $input->fillable, $fields))
90+
->replaceDateFields($stub, $this->getDateFields($stub, $fields))
91+
->replaceCasts($stub, $this->getCasts($stub, $fields))
8492
->replacePrimaryKey($stub, $primaryKey)
8593
->replaceRelationshipPlaceholder($stub, $relations)
8694
->replaceAccessors($stub, $this->getAccessors($fields))
@@ -172,68 +180,129 @@ protected function getStub()
172180
/**
173181
* Gets the formatted fillable line.
174182
*
175-
* @param $fillables
183+
* @param string $stub
184+
* @param string $fillables
176185
* @param array $fields
177186
*
178187
* @return string
179188
*/
180-
protected function getFillables($fillables, array $fields)
189+
protected function getFillables($stub, $fillables, array $fields)
181190
{
182191
if (!empty($fillables)) {
183-
return $this->getFillablesFromString($fillables);
192+
return $this->getFillablesFromString($stub, $fillables);
184193
}
185194

186-
return $this->getFillablefields($fields);
195+
return $this->getFillablefields($stub, $fields);
187196
}
188197

189198
/**
190199
* Gets the fillable string from a giving raw string.
200+
*
201+
* @param string $stub
202+
* @param string $fillablesString
191203
*
192204
* @return string
193205
*/
194-
protected function getFillablesFromString($fillablesString)
206+
protected function getFillablesFromString($stub, $fillablesString)
195207
{
196208
$columns = Helpers::removeEmptyItems(explode(',', $fillablesString), function ($column) {
197209
return trim(Helpers::removeNonEnglishChars($column));
198210
});
199211

200-
return sprintf('[%s]', implode(',', Helpers::wrapItems($columns)));
212+
$fillables = [];
213+
$indentCount = $this->getIndent($stub, $this->getTemplateVariable('fillable'));
214+
$indent = $this->Indent($indentCount - $this->backspaceCount);
215+
216+
foreach($columns as $column) {
217+
$fillables[$column] = sprintf("%s'%s'", $index, $column);
218+
}
219+
220+
return $this->makeArrayString($fillables, $indentCount - $this->backspaceCount - 4);
201221
}
202222

203223
/**
204224
* Gets the fillable string from a giving fields array.
205225
*
226+
* @param string $stub
227+
* @param array $fields
228+
*
206229
* @return string
207230
*/
208-
protected function getFillablefields(array $fields)
231+
protected function getFillablefields($stub, array $fields)
209232
{
210233
$fillables = [];
211-
234+
$indentCount = $this->getIndent($stub, $this->getTemplateVariable('fillable'));
235+
$indent = $this->Indent($indentCount - $this->backspaceCount);
212236
foreach ($fields as $field) {
213237
if ($field->isOnFormView) {
214-
$fillables[] = sprintf("'%s'", Helpers::removeNonEnglishChars($field->name));
238+
$fillables[] = sprintf("%s'%s'", $indent, $field->name);
215239
}
216240
}
217241

218-
return sprintf('[%s]', implode(',', $fillables));
242+
return $this->makeArrayString($fillables, $indentCount - $this->backspaceCount - 4);
219243
}
220244

221245
/**
222246
* Gets the date fields string from a giving fields array.
223247
*
248+
* @param string $stub
249+
* @param array $fields
250+
*
224251
* @return string
225252
*/
226-
protected function getDateFields(array $fields)
253+
protected function getDateFields($stub, array $fields)
227254
{
228255
$dates = [];
229-
256+
$indentCount = $this->getIndent($stub, $this->getTemplateVariable('dates'));
257+
$indent = $this->Indent($indentCount - $this->backspaceCount);
230258
foreach ($fields as $field) {
231259
if ($field->isDate) {
232-
$dates[] = sprintf("'%s'", Helpers::removeNonEnglishChars($field->name));
260+
$dates[] = sprintf("%s'%s'", $indent, $field->name);
261+
}
262+
}
263+
264+
return $this->makeArrayString($dates, $indentCount - $this->backspaceCount - 4);
265+
}
266+
267+
/**
268+
* Gets the castable fields in a string from a giving fields array.
269+
*
270+
* @param string $stub
271+
* @param array $fields
272+
*
273+
* @return string
274+
*/
275+
protected function getCasts($stub, array $fields)
276+
{
277+
$casts = [];
278+
$indentCount = $this->getIndent($stub, $this->getTemplateVariable('casts'));
279+
$indent = $this->Indent($indentCount - $this->backspaceCount);
280+
foreach ($fields as $field) {
281+
if ($field->isCastable()) {
282+
$casts[$field->name] = sprintf("%s'%s' => '%s'", $indent, $field->name, $field->castAs);
233283
}
234284
}
285+
286+
return $this->makeArrayString($casts, $indentCount - $this->backspaceCount - 4);
287+
}
288+
289+
/**
290+
* Gets array ready string
291+
*
292+
* @param array $name
293+
* @param int $index
294+
*
295+
* @return string
296+
*/
297+
protected function makeArrayString(array $names, $index = 0)
298+
{
299+
$string = implode(',' . PHP_EOL, $names);
300+
301+
if(!empty($string)) {
302+
return sprintf('[%s%s%s%s]', PHP_EOL, $string, PHP_EOL, $this->indent($index));
303+
}
235304

236-
return sprintf('[%s]', implode(',', $dates));
305+
return '[]';
237306
}
238307

239308
/**
@@ -308,8 +377,7 @@ protected function getRelationMethods(array $relationships, array $fields)
308377
}
309378

310379
foreach ($fields as $field) {
311-
312-
if ( $field->hasForeignRelation() ) {
380+
if ($field->hasForeignRelation()) {
313381
$relation = $field->getForeignRelation();
314382
$methods[$relation->name] = $this->getRelationshipMethod($relation);
315383
}
@@ -466,8 +534,7 @@ protected function getNewPrimaryKey($primaryKey)
466534
*/
467535
protected function getTimeStampsStub($shouldUseTimeStamps)
468536
{
469-
if($shouldUseTimeStamps)
470-
{
537+
if ($shouldUseTimeStamps) {
471538
return null;
472539
}
473540
return $this->getStubContent('model-timestamps');
@@ -548,6 +615,21 @@ protected function replaceDateFields(&$stub, $dates)
548615
return $this;
549616
}
550617

618+
/**
619+
* Replaces the casts for the given stub.
620+
*
621+
* @param string $stub
622+
* @param string $casts
623+
*
624+
* @return $this
625+
*/
626+
protected function replaceCasts(&$stub, $casts)
627+
{
628+
$stub = $this->strReplace('casts', $casts, $stub);
629+
630+
return $this;
631+
}
632+
551633
/**
552634
* Replaces the delimiter for the given stub.
553635
*
@@ -627,7 +709,7 @@ protected function replaceSoftDelete(&$stub, $shouldUseSoftDelete)
627709
*/
628710
protected function replaceFillable(&$stub, $fillable)
629711
{
630-
$stub = $this->strReplace('fillable', !empty($fillable) ? $fillable : '[]', $stub);
712+
$stub = $this->strReplace('fillable', $fillable, $stub);
631713

632714
return $this;
633715
}

src/Models/Field.php

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,13 @@ class Field
240240
*/
241241
public $onUpdate;
242242

243+
/**
244+
* Field placeholder
245+
*
246+
* @var string
247+
*/
248+
public $castAs = '';
249+
243250
/**
244251
* Creates a new field instance.
245252
*
@@ -372,6 +379,16 @@ public function hasForeignRelation()
372379
return ! is_null($this->foreignRelation);
373380
}
374381

382+
/**
383+
* Checks if the field shoudl be casted.
384+
*
385+
* @return bool
386+
*/
387+
public function isCastable()
388+
{
389+
return ! empty($this->castAs);
390+
}
391+
375392
/**
376393
* Checks if the field has a foreign relation.
377394
*
@@ -561,6 +578,8 @@ public function toArray()
561578
'is-auto-increment' => $this->isAutoIncrement,
562579
'is-inline-options' => $this->isInlineOptions,
563580
'is-multiple-answers' => $this->isMultipleAnswers,
581+
'is-date' => $this->isDate,
582+
'cast-as' => $this->castAs,
564583
'placeholder' => $this->placeHolder,
565584
'delimiter' => $this->optionsDelimiter,
566585
'range' => $this->range,
@@ -625,7 +644,8 @@ public function isPrimary()
625644
*/
626645
public function isDateTime()
627646
{
628-
return in_array($this->dataType, ['dateTime','dateTimeTz']) || in_array($this->name, ['created_at','updated_at','deleted_at']);
647+
return in_array($this->dataType, ['dateTime','dateTimeTz'])
648+
|| in_array($this->name, ['created_at','updated_at','deleted_at']);
629649
}
630650

631651
/**
@@ -649,23 +669,23 @@ public function isTime()
649669
}
650670

651671
/**
652-
* Checks if the field's type is any valid date.
672+
* Checks if the data type is time stamp.
653673
*
654674
* @return bool
655675
*/
656-
public function isDateOrTime()
676+
public function isTimeStamp()
657677
{
658-
return $this->isDate() || $this->isDateTime() || $this->isTime();
678+
return in_array($this->dataType, ['timestamp','timestampTz']);
659679
}
660680

661681
/**
662-
* Checks if the data type is time stamp.
682+
* Checks if the field's type is any valid date.
663683
*
664684
* @return bool
665685
*/
666-
public function isTimeStamp()
686+
public function isDateOrTime()
667687
{
668-
return in_array($this->dataType, ['timestamp','timestampTz']);
688+
return $this->isDate() || $this->isDateTime() || $this->isTime() || $this->isTimeStamp();
669689
}
670690

671691
/**

0 commit comments

Comments
 (0)