| 1 |
<?php |
| 2 |
|
| 3 |
namespace App\Billingo\Traits; |
| 4 |
|
| 5 |
use App\Billingo\Models\BillingoModel; |
| 6 |
use App\Billingo\Service\BillingoCollection; |
| 7 |
|
| 8 |
trait WithFactory |
| 9 |
{ |
| 10 |
public static function factory($round = 1): null|BillingoModel|BillingoCollection |
| 11 |
{ |
| 12 |
if ($round < 1) { |
| 13 |
$round = 1; |
| 14 |
} |
| 15 |
|
| 16 |
$callerName = static::class; |
| 17 |
|
| 18 |
$factoryName = "App\Billingo\Factories\\" . classBasename($callerName) . 'Factory'; |
| 19 |
|
| 20 |
if (!class_exists($factoryName)) { |
| 21 |
|
| 22 |
return null; |
| 23 |
} |
| 24 |
|
| 25 |
$fakers = []; |
| 26 |
|
| 27 |
for ($i = 0; $i < $round; $i++): |
| 28 |
$fakers[] = new $callerName((new $factoryName)->definition()); |
| 29 |
endfor; |
| 30 |
|
| 31 |
$fakers = billingoCollection($fakers); |
| 32 |
|
| 33 |
return $round === 1 ? $fakers->first() : $fakers; |
| 34 |
} |
| 35 |
} |
| 36 |
|