@@ -36,14 +36,14 @@ public function output(Tree $tree): array
36
36
37
37
/** @var \Blueprint\Models\Model $model */
38
38
foreach ($ tree ->models () as $ model ) {
39
- if (! Blueprint::isLaravel8OrHigher ()) {
39
+ if (!Blueprint::isLaravel8OrHigher ()) {
40
40
$ this ->addImport ($ model , 'Faker\Generator as Faker ' );
41
41
}
42
42
$ this ->addImport ($ model , $ model ->fullyQualifiedClassName ());
43
43
44
44
$ path = $ this ->getPath ($ model );
45
45
46
- if (! $ this ->files ->exists (dirname ($ path ))) {
46
+ if (!$ this ->files ->exists (dirname ($ path ))) {
47
47
$ this ->files ->makeDirectory (dirname ($ path ), 0755 , true );
48
48
}
49
49
@@ -64,22 +64,26 @@ protected function getPath(Model $model)
64
64
{
65
65
$ path = $ model ->name ();
66
66
if ($ model ->namespace ()) {
67
- $ path = str_replace ('\\' , '/ ' , $ model ->namespace ()). '/ ' . $ path ;
67
+ $ path = str_replace ('\\' , '/ ' , $ model ->namespace ()) . '/ ' . $ path ;
68
68
}
69
69
70
- return 'database/factories/ ' . $ path. 'Factory.php ' ;
70
+ return 'database/factories/ ' . $ path . 'Factory.php ' ;
71
71
}
72
72
73
73
protected function populateStub (string $ stub , Model $ model )
74
74
{
75
75
$ stub = str_replace ('{{ model }} ' , $ model ->name (), $ stub );
76
76
$ stub = str_replace ('// ' , $ this ->buildDefinition ($ model ), $ stub );
77
- if (Blueprint::isLaravel8OrHigher ()) {
78
- $ stub = str_replace ('use {{ namespacedModel }}; ' , $ this ->buildImports ($ model ), $ stub );
79
- } else {
80
- $ stub = str_replace ('use Faker\Generator as Faker; ' .PHP_EOL .'use {{ namespacedModel }}; ' , $ this ->buildImports ($ model ), $ stub );
77
+
78
+ if (!Blueprint::isLaravel8OrHigher ()) {
79
+ $ stub = str_replace ([
80
+ "use Faker\Generator as Faker; \r\nuse " ,
81
+ "use Faker\Generator as Faker; \nuse "
82
+ ], "use " , $ stub );
81
83
}
82
84
85
+ $ stub = str_replace ('use {{ namespacedModel }}; ' , $ this ->buildImports ($ model ), $ stub );
86
+
83
87
return $ stub ;
84
88
}
85
89
@@ -109,66 +113,66 @@ protected function buildDefinition(Model $model)
109
113
} elseif ($ foreign !== 'foreign ' ) {
110
114
$ table = $ foreign ;
111
115
112
- if (Str::startsWith ($ column ->name (), $ foreign. '_ ' )) {
113
- $ key = Str::after ($ column ->name (), $ foreign. '_ ' );
114
- } elseif (Str::startsWith ($ column ->name (), Str::snake (Str::singular ($ foreign )). '_ ' )) {
115
- $ key = Str::after ($ column ->name (), Str::snake (Str::singular ($ foreign )). '_ ' );
116
- } elseif (! Str::endsWith ($ column ->name (), '_id ' )) {
116
+ if (Str::startsWith ($ column ->name (), $ foreign . '_ ' )) {
117
+ $ key = Str::after ($ column ->name (), $ foreign . '_ ' );
118
+ } elseif (Str::startsWith ($ column ->name (), Str::snake (Str::singular ($ foreign )) . '_ ' )) {
119
+ $ key = Str::after ($ column ->name (), Str::snake (Str::singular ($ foreign )) . '_ ' );
120
+ } elseif (!Str::endsWith ($ column ->name (), '_id ' )) {
117
121
$ key = $ column ->name ();
118
122
}
119
123
}
120
124
121
125
$ class = Str::studly (Str::singular ($ table ));
122
126
123
127
if (Blueprint::isLaravel8OrHigher ()) {
124
- $ this ->addImport ($ model , $ model ->fullyQualifiedNamespace (). '\\' . $ class );
128
+ $ this ->addImport ($ model , $ model ->fullyQualifiedNamespace () . '\\' . $ class );
125
129
}
126
130
if ($ key === 'id ' ) {
127
131
if (Blueprint::isLaravel8OrHigher ()) {
128
- $ definition .= str_repeat (self ::INDENT , 3 ). "' {$ column ->name ()}' => " ;
132
+ $ definition .= str_repeat (self ::INDENT , 3 ) . "' {$ column ->name ()}' => " ;
129
133
$ definition .= sprintf ('%s::factory() ' , $ class );
130
- $ definition .= ', ' . PHP_EOL ;
134
+ $ definition .= ', ' . PHP_EOL ;
131
135
} else {
132
- $ definition .= str_repeat (self ::INDENT , 2 ). "' {$ column ->name ()}' => " ;
133
- $ definition .= sprintf ('factory(%s::class) ' , '\\' . $ model ->fullyQualifiedNamespace (). '\\' . $ class );
134
- $ definition .= ', ' . PHP_EOL ;
136
+ $ definition .= str_repeat (self ::INDENT , 2 ) . "' {$ column ->name ()}' => " ;
137
+ $ definition .= sprintf ('factory(%s::class) ' , '\\' . $ model ->fullyQualifiedNamespace () . '\\' . $ class );
138
+ $ definition .= ', ' . PHP_EOL ;
135
139
}
136
140
} else {
137
141
if (Blueprint::isLaravel8OrHigher ()) {
138
- $ definition .= str_repeat (self ::INDENT , 3 ). "' {$ column ->name ()}' => " ;
142
+ $ definition .= str_repeat (self ::INDENT , 3 ) . "' {$ column ->name ()}' => " ;
139
143
$ definition .= sprintf ('%s::factory()->create()->%s ' , $ class , $ key );
140
- $ definition .= ', ' . PHP_EOL ;
144
+ $ definition .= ', ' . PHP_EOL ;
141
145
} else {
142
- $ definition .= str_repeat (self ::INDENT , 2 ). "' {$ column ->name ()}' => function () { " ;
146
+ $ definition .= str_repeat (self ::INDENT , 2 ) . "' {$ column ->name ()}' => function () { " ;
143
147
$ definition .= PHP_EOL ;
144
- $ definition .= str_repeat (self ::INDENT , 3 ). sprintf ('return factory(%s::class)->create()->%s; ' , '\\' . $ model ->fullyQualifiedNamespace (). '\\' . $ class , $ key );
148
+ $ definition .= str_repeat (self ::INDENT , 3 ) . sprintf ('return factory(%s::class)->create()->%s; ' , '\\' . $ model ->fullyQualifiedNamespace () . '\\' . $ class , $ key );
145
149
$ definition .= PHP_EOL ;
146
- $ definition .= str_repeat (self ::INDENT , 2 ). '}, ' . PHP_EOL ;
150
+ $ definition .= str_repeat (self ::INDENT , 2 ) . '}, ' . PHP_EOL ;
147
151
}
148
152
}
149
153
} elseif ($ column ->dataType () === 'id ' || ($ column ->dataType () === 'uuid ' && Str::endsWith ($ column ->name (), '_id ' ))) {
150
154
$ name = Str::beforeLast ($ column ->name (), '_id ' );
151
155
$ class = Str::studly ($ column ->attributes ()[0 ] ?? $ name );
152
156
153
157
if (Blueprint::isLaravel8OrHigher ()) {
154
- $ this ->addImport ($ model , $ model ->fullyQualifiedNamespace (). '\\' . $ class );
155
- $ definition .= str_repeat (self ::INDENT , 3 ). "' {$ column ->name ()}' => " ;
158
+ $ this ->addImport ($ model , $ model ->fullyQualifiedNamespace () . '\\' . $ class );
159
+ $ definition .= str_repeat (self ::INDENT , 3 ) . "' {$ column ->name ()}' => " ;
156
160
$ definition .= sprintf ('%s::factory() ' , $ class );
157
161
} else {
158
- $ definition .= str_repeat (self ::INDENT , 2 ). "' {$ column ->name ()}' => " ;
159
- $ definition .= sprintf ('factory(%s::class) ' , '\\' . $ model ->fullyQualifiedNamespace (). '\\' . $ class );
162
+ $ definition .= str_repeat (self ::INDENT , 2 ) . "' {$ column ->name ()}' => " ;
163
+ $ definition .= sprintf ('factory(%s::class) ' , '\\' . $ model ->fullyQualifiedNamespace () . '\\' . $ class );
160
164
}
161
- $ definition .= ', ' . PHP_EOL ;
162
- } elseif (in_array ($ column ->dataType (), ['enum ' , 'set ' ]) && ! empty ($ column ->attributes ())) {
165
+ $ definition .= ', ' . PHP_EOL ;
166
+ } elseif (in_array ($ column ->dataType (), ['enum ' , 'set ' ]) && !empty ($ column ->attributes ())) {
163
167
$ faker = FakerRegistry::fakerData ($ column ->name ()) ?? FakerRegistry::fakerDataType ($ column ->dataType ());
164
168
if (Blueprint::isLaravel8OrHigher ()) {
165
- $ definition .= str_repeat (self ::INDENT , 3 ). "' {$ column ->name ()}' => " ;
166
- $ definition .= '$this->faker-> ' . $ faker ;
169
+ $ definition .= str_repeat (self ::INDENT , 3 ) . "' {$ column ->name ()}' => " ;
170
+ $ definition .= '$this->faker-> ' . $ faker ;
167
171
} else {
168
- $ definition .= str_repeat (self ::INDENT , 2 ). "' {$ column ->name ()}' => " ;
169
- $ definition .= '$faker-> ' . $ faker ;
172
+ $ definition .= str_repeat (self ::INDENT , 2 ) . "' {$ column ->name ()}' => " ;
173
+ $ definition .= '$faker-> ' . $ faker ;
170
174
}
171
- $ definition .= ', ' . PHP_EOL ;
175
+ $ definition .= ', ' . PHP_EOL ;
172
176
$ definition = str_replace (
173
177
"/** {$ column ->dataType ()}_attributes **/ " ,
174
178
json_encode ($ column ->attributes ()),
@@ -177,28 +181,28 @@ protected function buildDefinition(Model $model)
177
181
} elseif (in_array ($ column ->dataType (), ['decimal ' , 'double ' , 'float ' ])) {
178
182
$ faker = FakerRegistry::fakerData ($ column ->name ()) ?? FakerRegistry::fakerDataType ($ column ->dataType ());
179
183
if (Blueprint::isLaravel8OrHigher ()) {
180
- $ definition .= str_repeat (self ::INDENT , 3 ). "' {$ column ->name ()}' => " ;
181
- $ definition .= '$this->faker-> ' . $ faker ;
184
+ $ definition .= str_repeat (self ::INDENT , 3 ) . "' {$ column ->name ()}' => " ;
185
+ $ definition .= '$this->faker-> ' . $ faker ;
182
186
} else {
183
- $ definition .= str_repeat (self ::INDENT , 2 ). "' {$ column ->name ()}' => " ;
184
- $ definition .= '$faker-> ' . $ faker ;
187
+ $ definition .= str_repeat (self ::INDENT , 2 ) . "' {$ column ->name ()}' => " ;
188
+ $ definition .= '$faker-> ' . $ faker ;
185
189
}
186
- $ definition .= ', ' . PHP_EOL ;
190
+ $ definition .= ', ' . PHP_EOL ;
187
191
188
192
$ precision = min ([65 , intval ($ column ->attributes ()[0 ] ?? 10 )]);
189
193
$ scale = min ([30 , max ([0 , intval ($ column ->attributes ()[1 ] ?? 0 )])]);
190
194
191
195
$ definition = str_replace (
192
196
"/** {$ column ->dataType ()}_attributes **/ " ,
193
- implode (', ' , [$ scale , 0 , (str_repeat (9 , $ precision - $ scale ). '. ' . str_repeat (9 , $ scale ))]),
197
+ implode (', ' , [$ scale , 0 , (str_repeat (9 , $ precision - $ scale ) . '. ' . str_repeat (9 , $ scale ))]),
194
198
$ definition
195
199
);
196
200
} elseif (in_array ($ column ->dataType (), ['json ' , 'jsonb ' ])) {
197
201
$ default = $ column ->defaultValue () ?? "'{}' " ;
198
202
if (Blueprint::isLaravel8OrHigher ()) {
199
- $ definition .= str_repeat (self ::INDENT , 3 ). "' {$ column ->name ()}' => {$ default }, " . PHP_EOL ;
203
+ $ definition .= str_repeat (self ::INDENT , 3 ) . "' {$ column ->name ()}' => {$ default }, " . PHP_EOL ;
200
204
} else {
201
- $ definition .= str_repeat (self ::INDENT , 2 ). "' {$ column ->name ()}' => {$ default }, " . PHP_EOL ;
205
+ $ definition .= str_repeat (self ::INDENT , 2 ) . "' {$ column ->name ()}' => {$ default }, " . PHP_EOL ;
202
206
}
203
207
} elseif ($ column ->dataType () === 'morphs ' ) {
204
208
if ($ column ->isNullable ()) {
@@ -213,22 +217,22 @@ protected function buildDefinition(Model $model)
213
217
}
214
218
} elseif ($ column ->dataType () === 'rememberToken ' ) {
215
219
if (Blueprint::isLaravel8OrHigher ()) {
216
- $ definition .= str_repeat (self ::INDENT , 3 ). "' {$ column ->name ()}' => " ;
220
+ $ definition .= str_repeat (self ::INDENT , 3 ) . "' {$ column ->name ()}' => " ;
217
221
} else {
218
222
$ this ->addImport ($ model , 'Illuminate\Support\Str ' );
219
- $ definition .= str_repeat (self ::INDENT , 2 ). "' {$ column ->name ()}' => " ;
223
+ $ definition .= str_repeat (self ::INDENT , 2 ) . "' {$ column ->name ()}' => " ;
220
224
}
221
225
$ definition .= 'Str::random(10) ' ;
222
- $ definition .= ', ' . PHP_EOL ;
226
+ $ definition .= ', ' . PHP_EOL ;
223
227
} else {
224
228
if (Blueprint::isLaravel8OrHigher ()) {
225
- $ definition .= str_repeat (self ::INDENT , 3 ). "' {$ column ->name ()}' => " ;
229
+ $ definition .= str_repeat (self ::INDENT , 3 ) . "' {$ column ->name ()}' => " ;
226
230
} else {
227
- $ definition .= str_repeat (self ::INDENT , 2 ). "' {$ column ->name ()}' => " ;
231
+ $ definition .= str_repeat (self ::INDENT , 2 ) . "' {$ column ->name ()}' => " ;
228
232
}
229
233
$ type = $ column ->dataType ();
230
234
if ($ column ->isUnsigned ()) {
231
- $ type = 'unsigned ' . $ type ;
235
+ $ type = 'unsigned ' . $ type ;
232
236
}
233
237
234
238
$ faker = FakerRegistry::fakerData ($ column ->name ()) ?? (FakerRegistry::fakerDataType ($ type ) ?? FakerRegistry::fakerDataType ($ column ->dataType ()));
@@ -238,11 +242,11 @@ protected function buildDefinition(Model $model)
238
242
}
239
243
240
244
if (Blueprint::isLaravel8OrHigher ()) {
241
- $ definition .= '$this->faker-> ' . $ faker ;
245
+ $ definition .= '$this->faker-> ' . $ faker ;
242
246
} else {
243
- $ definition .= '$faker-> ' . $ faker ;
247
+ $ definition .= '$faker-> ' . $ faker ;
244
248
}
245
- $ definition .= ', ' . PHP_EOL ;
249
+ $ definition .= ', ' . PHP_EOL ;
246
250
}
247
251
}
248
252
@@ -255,7 +259,7 @@ protected function buildImports(Model $model)
255
259
sort ($ imports );
256
260
257
261
return implode (PHP_EOL , array_map (function ($ class ) {
258
- return 'use ' . $ class. '; ' ;
262
+ return 'use ' . $ class . '; ' ;
259
263
}, $ imports ));
260
264
}
261
265
@@ -271,7 +275,7 @@ private function fillableColumns(array $columns): array
271
275
}
272
276
273
277
return array_filter ($ columns , function (Column $ column ) {
274
- return ! in_array ('nullable ' , $ column ->modifiers ());
278
+ return !in_array ('nullable ' , $ column ->modifiers ());
275
279
});
276
280
}
277
281
}
0 commit comments