2
2
3
3
namespace LaravelLegends \InteractiveModel ;
4
4
5
+ use Carbon \Carbon ;
6
+ use Carbon \Exceptions \InvalidFormatException ;
5
7
use Illuminate \Console \Command ;
6
8
use Illuminate \Database \Eloquent \Model ;
7
9
13
15
14
16
class InteractiveModelCommand extends Command
15
17
{
16
- protected $ signature = 'll :interactive-model {model} ' ;
18
+ protected $ signature = 'model :interactive {model} ' ;
17
19
18
20
protected $ description = 'Insert data in your model interactively ' ;
19
21
@@ -23,11 +25,9 @@ class InteractiveModelCommand extends Command
23
25
*/
24
26
public function handle ()
25
27
{
26
-
27
28
$ instance = $ this ->getModelInstance ();
28
29
29
30
foreach ($ instance ->getFillable () as $ field ) {
30
-
31
31
$ value = $ this ->getFieldValue ($ instance , $ field );
32
32
33
33
$ instance ->setAttribute ($ field , $ value );
@@ -78,15 +78,15 @@ protected function getModelInstance(): Model
78
78
*
79
79
* @param Model $model
80
80
* @param string $field
81
- * @return string
81
+ * @return mixed
82
82
*/
83
- protected function getFieldValue (Model $ model , string $ field ): ? string
83
+ protected function getFieldValue (Model $ model , string $ field )
84
84
{
85
- $ method = in_array ( $ field , $ model ->getHidden ()) ? ' secret ' : ' ask ' ;
85
+ $ castType = $ model ->getCasts ()[ $ field ] ?? ' string ' ;
86
86
87
- $ value = $ this ->$ method ( " Type the \" $ field \" value " , false );
87
+ $ value = $ this ->getByCastType ( $ model , $ castType , $ field );
88
88
89
- return $ value === false ? null : $ value ;
89
+ return $ value ;
90
90
}
91
91
92
92
/**
@@ -105,4 +105,59 @@ protected function showInsertedModel(Model $model): void
105
105
106
106
$ this ->table (array_keys ($ attributes ), [$ attributes ]);
107
107
}
108
- }
108
+
109
+
110
+ protected function getByCastType (Model $ model , string $ castType , string $ field )
111
+ {
112
+ if (in_array ($ castType , ['datetime ' , 'date ' , 'custom_datetime ' ])) {
113
+
114
+ $ value = $ this ->ask ($ this ->getAskForField ($ field ));
115
+
116
+ try {
117
+ $ value = Carbon::parse ($ value );
118
+ } catch (InvalidFormatException $ e ) {
119
+ $ value = $ this ->getByCastType ($ model , $ castType , $ field );
120
+ }
121
+
122
+ return $ value ;
123
+
124
+ } elseif (in_array ($ castType , ['json ' , 'array ' ])) {
125
+ $ value = $ this ->ask ($ this ->getAskForField ($ field ));
126
+
127
+ $ value = json_decode ($ value , true );
128
+
129
+ if (json_last_error () > 0 ) {
130
+ $ value = $ this ->getByCastType ($ model , $ castType , $ field );
131
+ }
132
+
133
+ return $ value ;
134
+
135
+ } elseif (in_array ($ castType , ['bool ' , 'boolean ' ])) {
136
+
137
+ $ value = strtolower (
138
+ $ this ->ask ($ this ->getAskForField ($ field ))
139
+ );
140
+
141
+ if (! in_array ($ value , ['true ' , 'false ' ])) {
142
+ $ value = strtolower (
143
+ $ this ->ask ($ this ->getAskForField ($ field ))
144
+ );
145
+ }
146
+
147
+ return $ value === 'true ' ;
148
+
149
+ }
150
+
151
+ $ method = in_array ($ field , $ model ->getHidden ()) ? 'secret ' : 'ask ' ;
152
+
153
+ $ value = $ this ->$ method ($ this ->getAskForField ($ field ));
154
+
155
+ return $ value === 'NULL ' ? null : $ value ;
156
+
157
+ }
158
+
159
+ protected function getAskForField (string $ field )
160
+ {
161
+ return "Type the \"$ field \" value " ;
162
+ }
163
+ }
0 commit comments