99use Illuminate \Container \Container ;
1010use Illuminate \Support \Collection ;
1111use Illuminate \Support \Traits \Conditionable ;
12+ use Illuminate \Support \Traits \Macroable ;
1213
1314class Factory
1415{
1516 use Conditionable;
17+ use Macroable {
18+ __call as macroCall;
19+ }
1620
1721 /**
1822 * The current Faker instance.
@@ -35,7 +39,7 @@ public function __construct(
3539 /**
3640 * Get a new dummy factory instance for the given attributes.
3741 */
38- public static function new (Closure |array $ attributes = []): static
42+ public static function new (callable |array $ attributes = []): static
3943 {
4044 return (new static )->state ($ attributes )->configure ();
4145 }
@@ -83,7 +87,7 @@ protected function collect(array $instances = []): mixed
8387 /**
8488 * Get the raw attributes generated by the factory.
8589 */
86- public function raw (Closure |array $ attributes = []): array
90+ public function raw (callable |array $ attributes = []): array
8791 {
8892 if ($ this ->count === null ) {
8993 return $ this ->state ($ attributes )->getExpandedAttributes ();
@@ -97,7 +101,7 @@ public function raw(Closure|array $attributes = []): array
97101 /**
98102 * Create a collection of classes.
99103 */
100- public function make (Closure |array $ attributes = []): mixed
104+ public function make (callable |array $ attributes = []): mixed
101105 {
102106 if (! empty ($ attributes )) {
103107 return $ this ->state ($ attributes )->make ();
@@ -122,6 +126,42 @@ public function make(Closure|array $attributes = []): mixed
122126 return $ instances ;
123127 }
124128
129+ /**
130+ * Make a single instance of the class.
131+ */
132+ public function makeOne (callable |array $ attributes = []): mixed
133+ {
134+ return $ this ->count (null )->make ($ attributes );
135+ }
136+
137+ /**
138+ * Create a collection of classes.
139+ */
140+ public function makeMany (iterable |int |null $ records = null ): mixed
141+ {
142+ $ records ??= ($ this ->count ?? 1 );
143+
144+ $ this ->count = null ;
145+
146+ if (is_numeric ($ records )) {
147+ $ records = array_fill (0 , $ records , []);
148+ }
149+
150+ return $ this ->collect (
151+ (new Collection ($ records ))->map (function ($ record ) {
152+ return $ this ->state ($ record )->make ();
153+ })->all ()
154+ );
155+ }
156+
157+ /**
158+ * Create a callback that makes the class when invoked.
159+ */
160+ public function lazy (array $ attributes = []): Closure
161+ {
162+ return fn () => $ this ->make ($ attributes );
163+ }
164+
125165 /**
126166 * Make an instance of the class with the given attributes.
127167 */
@@ -179,17 +219,27 @@ protected function expandAttributes(array $definition): array
179219 /**
180220 * Add a new state transformation to the class definition.
181221 */
182- public function state (mixed $ state ): static
222+ public function state (callable | array $ state ): static
183223 {
184224 return $ this ->newInstance ([
185225 'states ' => $ this ->states ->concat ([
186- is_callable ($ state ) ? $ state : function () use ($ state ) {
187- return $ state ;
188- },
226+ is_callable ($ state ) ? $ state : fn () => $ state ,
189227 ]),
190228 ]);
191229 }
192230
231+ /**
232+ * Prepend a new state transformation to the class definition.
233+ */
234+ public function prependState (callable |array $ state ): static
235+ {
236+ return $ this ->newInstance ([
237+ 'states ' => $ this ->states ->prepend (
238+ is_callable ($ state ) ? $ state : fn () => $ state ,
239+ ),
240+ ]);
241+ }
242+
193243 /**
194244 * Set a single class attribute.
195245 */
@@ -242,6 +292,16 @@ public function afterMaking(Closure $callback): static
242292 ]);
243293 }
244294
295+ /**
296+ * Remove the "after making" callbacks from the factory.
297+ */
298+ public function withoutAfterMaking (): static
299+ {
300+ return $ this ->newInstance ([
301+ 'afterMaking ' => new Collection ,
302+ ]);
303+ }
304+
245305 /**
246306 * Call the "after making" callbacks for the given class instances.
247307 */
@@ -299,8 +359,12 @@ protected function newFaker(): Generator
299359 /**
300360 * Handle dynamic state method calls.
301361 */
302- public function __call (string $ method , array $ parameters ): static
362+ public function __call (string $ method , array $ parameters ): mixed
303363 {
364+ if (static ::hasMacro ($ method )) {
365+ return $ this ->macroCall ($ method , $ parameters );
366+ }
367+
304368 if (! $ this ->class ) {
305369 throw new BadMethodCallException ('Cannot call state methods on a factory without a using class. ' );
306370 }
0 commit comments