@@ -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 }
0 commit comments