@@ -114,11 +114,11 @@ abstract class Factory
114
114
public static $ namespace = 'Database \\Factories \\' ;
115
115
116
116
/**
117
- * The default model name resolver .
117
+ * The default model name resolvers .
118
118
*
119
- * @var callable(self): class-string<TModel>
119
+ * @var array<class-string, callable(self): class-string<TModel> >
120
120
*/
121
- protected static $ modelNameResolver ;
121
+ protected static $ modelNameResolvers = [] ;
122
122
123
123
/**
124
124
* The factory name resolver.
@@ -274,7 +274,7 @@ public function createMany(int|iterable|null $records = null)
274
274
*/
275
275
public function createManyQuietly (int |iterable |null $ records = null )
276
276
{
277
- return Model::withoutEvents (fn () => $ this ->createMany ($ records ));
277
+ return Model::withoutEvents (fn () => $ this ->createMany ($ records ));
278
278
}
279
279
280
280
/**
@@ -314,7 +314,7 @@ public function create($attributes = [], ?Model $parent = null)
314
314
*/
315
315
public function createQuietly ($ attributes = [], ?Model $ parent = null )
316
316
{
317
- return Model::withoutEvents (fn () => $ this ->create ($ attributes , $ parent ));
317
+ return Model::withoutEvents (fn () => $ this ->create ($ attributes , $ parent ));
318
318
}
319
319
320
320
/**
@@ -326,7 +326,7 @@ public function createQuietly($attributes = [], ?Model $parent = null)
326
326
*/
327
327
public function lazy (array $ attributes = [], ?Model $ parent = null )
328
328
{
329
- return fn () => $ this ->create ($ attributes , $ parent );
329
+ return fn () => $ this ->create ($ attributes , $ parent );
330
330
}
331
331
332
332
/**
@@ -469,7 +469,7 @@ protected function getRawAttributes(?Model $parent)
469
469
protected function parentResolvers ()
470
470
{
471
471
return $ this ->for
472
- ->map (fn (BelongsToRelationship $ for ) => $ for ->recycle ($ this ->recycle )->attributesFor ($ this ->newModel ()))
472
+ ->map (fn (BelongsToRelationship $ for ) => $ for ->recycle ($ this ->recycle )->attributesFor ($ this ->newModel ()))
473
473
->collapse ()
474
474
->all ();
475
475
}
@@ -519,7 +519,7 @@ public function state($state)
519
519
{
520
520
return $ this ->newInstance ([
521
521
'states ' => $ this ->states ->concat ([
522
- is_callable ($ state ) ? $ state : fn () => $ state ,
522
+ is_callable ($ state ) ? $ state : fn () => $ state ,
523
523
]),
524
524
]);
525
525
}
@@ -580,7 +580,8 @@ public function has(self $factory, $relationship = null)
580
580
{
581
581
return $ this ->newInstance ([
582
582
'has ' => $ this ->has ->concat ([new Relationship (
583
- $ factory , $ relationship ?? $ this ->guessRelationship ($ factory ->modelName ())
583
+ $ factory ,
584
+ $ relationship ?? $ this ->guessRelationship ($ factory ->modelName ())
584
585
)]),
585
586
]);
586
587
}
@@ -653,7 +654,7 @@ public function recycle($model)
653
654
->merge (
654
655
Collection::wrap ($ model instanceof Model ? func_get_args () : $ model )
655
656
->flatten ()
656
- )->groupBy (fn ($ model ) => get_class ($ model )),
657
+ )->groupBy (fn ($ model ) => get_class ($ model )),
657
658
]);
658
659
}
659
660
@@ -810,18 +811,20 @@ public function modelName()
810
811
return $ this ->model ;
811
812
}
812
813
813
- $ resolver = static ::$ modelNameResolver ?? function (self $ factory ) {
814
+ $ resolver = static ::$ modelNameResolvers [ static ::class] ?? function (self $ factory ) {
814
815
$ namespacedFactoryBasename = Str::replaceLast (
815
- 'Factory ' , '' , Str::replaceFirst (static ::$ namespace , '' , get_class ($ factory ))
816
+ 'Factory ' ,
817
+ '' ,
818
+ Str::replaceFirst (static ::$ namespace , '' , get_class ($ factory ))
816
819
);
817
820
818
821
$ factoryBasename = Str::replaceLast ('Factory ' , '' , class_basename ($ factory ));
819
822
820
823
$ appNamespace = static ::appNamespace ();
821
824
822
- return class_exists ($ appNamespace. 'Models \\' . $ namespacedFactoryBasename )
823
- ? $ appNamespace. 'Models \\' . $ namespacedFactoryBasename
824
- : $ appNamespace. $ factoryBasename ;
825
+ return class_exists ($ appNamespace . 'Models \\' . $ namespacedFactoryBasename )
826
+ ? $ appNamespace . 'Models \\' . $ namespacedFactoryBasename
827
+ : $ appNamespace . $ factoryBasename ;
825
828
};
826
829
827
830
return $ resolver ($ this );
@@ -835,7 +838,7 @@ public function modelName()
835
838
*/
836
839
public static function guessModelNamesUsing (callable $ callback )
837
840
{
838
- static ::$ modelNameResolver = $ callback ;
841
+ static ::$ modelNameResolvers [ static ::class] = $ callback ;
839
842
}
840
843
841
844
/**
@@ -898,11 +901,11 @@ public static function resolveFactoryName(string $modelName)
898
901
$ resolver = static ::$ factoryNameResolver ?? function (string $ modelName ) {
899
902
$ appNamespace = static ::appNamespace ();
900
903
901
- $ modelName = Str::startsWith ($ modelName , $ appNamespace. 'Models \\' )
902
- ? Str::after ($ modelName , $ appNamespace. 'Models \\' )
904
+ $ modelName = Str::startsWith ($ modelName , $ appNamespace . 'Models \\' )
905
+ ? Str::after ($ modelName , $ appNamespace . 'Models \\' )
903
906
: Str::after ($ modelName , $ appNamespace );
904
907
905
- return static ::$ namespace. $ modelName. 'Factory ' ;
908
+ return static ::$ namespace . $ modelName . 'Factory ' ;
906
909
};
907
910
908
911
return $ resolver ($ modelName );
0 commit comments