From 3d27ad7d265556eb740a24311ed52cdbf92385fd Mon Sep 17 00:00:00 2001 From: Marcus Pohorely Date: Wed, 10 Jan 2024 16:34:16 +0100 Subject: [PATCH 1/2] Introduced getCount Method, let it handle potential callables --- .../Database/Eloquent/Factories/Factory.php | 37 ++++++++++++++----- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Factories/Factory.php b/src/Illuminate/Database/Eloquent/Factories/Factory.php index d9a453359d23..02c1e6a6845d 100644 --- a/src/Illuminate/Database/Eloquent/Factories/Factory.php +++ b/src/Illuminate/Database/Eloquent/Factories/Factory.php @@ -201,13 +201,15 @@ public function configure() */ public function raw($attributes = [], ?Model $parent = null) { - if ($this->count === null) { + $count = $this->getCount(); + + if ($count === null) { return $this->state($attributes)->getExpandedAttributes($parent); } return array_map(function () use ($attributes, $parent) { return $this->state($attributes)->getExpandedAttributes($parent); - }, range(1, $this->count)); + }, range(1, $count)); } /** @@ -241,7 +243,7 @@ public function createOneQuietly($attributes = []) public function createMany(int|iterable|null $records = null) { if (is_null($records)) { - $records = $this->count ?? 1; + $records = $this->getCount() ?? 1; } if (is_numeric($records)) { @@ -382,23 +384,25 @@ public function makeOne($attributes = []) */ public function make($attributes = [], ?Model $parent = null) { + $count = $this->getCount(); + if (! empty($attributes)) { return $this->state($attributes)->make([], $parent); } - if ($this->count === null) { + if ($count === null) { return tap($this->makeInstance($parent), function ($instance) { $this->callAfterMaking(collect([$instance])); }); } - if ($this->count < 1) { + if ($count < 1) { return $this->newModel()->newCollection(); } $instances = $this->newModel()->newCollection(array_map(function () use ($parent) { return $this->makeInstance($parent); - }, range(1, $this->count))); + }, range(1, $count))); $this->callAfterMaking($instances); @@ -718,14 +722,29 @@ protected function callAfterCreating(Collection $instances, ?Model $parent = nul /** * Specify how many models should be generated. * - * @param int|null $count + * @param int|callable|null $count * @return static */ - public function count(?int $count) + public function count(int|callable|null $count) { return $this->newInstance(['count' => $count]); } + /** + * Get the count variable and maybe convert it to int + * + * @return int|null + */ + protected function getCount() { + $count = $this->count; + + if(is_callable($count)) { + return $count(); + } + + return $count; + } + /** * Specify the database connection that should be used to generate models. * @@ -792,7 +811,7 @@ public function modelName() }; return $this->model ?? $resolver($this); - } + } /** * Specify the callback that should be invoked to guess model names based on factory names. From a4ef4e45e1814ee146545bd204a117afbfb3c08e Mon Sep 17 00:00:00 2001 From: Marcus Pohorely Date: Wed, 10 Jan 2024 16:54:52 +0100 Subject: [PATCH 2/2] Style fixes --- .../Database/Eloquent/Factories/Factory.php | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Factories/Factory.php b/src/Illuminate/Database/Eloquent/Factories/Factory.php index 02c1e6a6845d..5eb2533425f1 100644 --- a/src/Illuminate/Database/Eloquent/Factories/Factory.php +++ b/src/Illuminate/Database/Eloquent/Factories/Factory.php @@ -202,7 +202,7 @@ public function configure() public function raw($attributes = [], ?Model $parent = null) { $count = $this->getCount(); - + if ($count === null) { return $this->state($attributes)->getExpandedAttributes($parent); } @@ -385,7 +385,7 @@ public function makeOne($attributes = []) public function make($attributes = [], ?Model $parent = null) { $count = $this->getCount(); - + if (! empty($attributes)) { return $this->state($attributes)->make([], $parent); } @@ -731,14 +731,15 @@ public function count(int|callable|null $count) } /** - * Get the count variable and maybe convert it to int + * Get the count variable and maybe convert it to int. * * @return int|null */ - protected function getCount() { + protected function getCount() + { $count = $this->count; - if(is_callable($count)) { + if (is_callable($count)) { return $count(); } @@ -811,7 +812,7 @@ public function modelName() }; return $this->model ?? $resolver($this); - } + } /** * Specify the callback that should be invoked to guess model names based on factory names.