| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Framework\Models\Factories; |
| 4 |
|
| 5 |
use Closure; |
| 6 |
use Exception; |
| 7 |
use Faker\Generator; |
| 8 |
use Give\Framework\Database\DB; |
| 9 |
|
| 10 |
/** |
| 11 |
* @template M |
| 12 |
*/ |
| 13 |
abstract class ModelFactory |
| 14 |
{ |
| 15 |
/** |
| 16 |
* @var class-string<M> |
| 17 |
*/ |
| 18 |
protected $model; |
| 19 |
|
| 20 |
/** |
| 21 |
* @var Generator |
| 22 |
*/ |
| 23 |
protected $faker; |
| 24 |
|
| 25 |
/** |
| 26 |
* @var int The number of models to create. |
| 27 |
*/ |
| 28 |
protected $count = 1; |
| 29 |
|
| 30 |
/** |
| 31 |
* @since 2.20.0 |
| 32 |
* |
| 33 |
* @param class-string<M> $model |
| 34 |
* |
| 35 |
* @return void |
| 36 |
*/ |
| 37 |
public function __construct($model) |
| 38 |
{ |
| 39 |
$this->model = $model; |
| 40 |
$this->faker = $this->withFaker(); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Define the model's default state. |
| 45 |
*/ |
| 46 |
abstract public function definition(): array; |
| 47 |
|
| 48 |
/** |
| 49 |
* @since 2.20.0 |
| 50 |
* |
| 51 |
* @param array $attributes |
| 52 |
* |
| 53 |
* @return M|M[] |
| 54 |
*/ |
| 55 |
public function make(array $attributes = []) |
| 56 |
{ |
| 57 |
$results = []; |
| 58 |
for ($i = 0; $i < $this->count; $i++) { |
| 59 |
$instance = $this->makeInstance($attributes); |
| 60 |
|
| 61 |
$results[] = $instance; |
| 62 |
} |
| 63 |
|
| 64 |
return $this->count === 1 ? $results[0] : $results; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* @since 4.0.0 |
| 69 |
*/ |
| 70 |
public function makeAndResolveTo($property): Closure |
| 71 |
{ |
| 72 |
return function() use ($property) { |
| 73 |
return is_array($results = $this->make()) |
| 74 |
? array_column($results, $property) |
| 75 |
: $results->$property; |
| 76 |
}; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* @since 2.20.0 |
| 81 |
* |
| 82 |
* @param array $attributes |
| 83 |
* |
| 84 |
* @return M|M[] |
| 85 |
* @throws Exception |
| 86 |
*/ |
| 87 |
public function create(array $attributes = []) |
| 88 |
{ |
| 89 |
$instances = $this->make($attributes); |
| 90 |
$instances = is_array($instances) ? $instances : [$instances]; |
| 91 |
|
| 92 |
DB::transaction(function () use ($instances) { |
| 93 |
foreach ($instances as $instance) { |
| 94 |
$instance->save(); |
| 95 |
} |
| 96 |
}); |
| 97 |
|
| 98 |
return $this->count === 1 ? $instances[0] : $instances; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* @since 4.0.0 |
| 103 |
*/ |
| 104 |
public function createAndResolveTo($property): Closure |
| 105 |
{ |
| 106 |
return function() use ($property) { |
| 107 |
return is_array($results = $this->create()) |
| 108 |
? array_column($results, $property) |
| 109 |
: $results->$property; |
| 110 |
}; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Creates an instance of the model from the attributes and definition. |
| 115 |
* |
| 116 |
* @since 4.0.0 Add support for resolving Closures. |
| 117 |
* @since 2.23.0 |
| 118 |
* |
| 119 |
* @return M |
| 120 |
*/ |
| 121 |
protected function makeInstance(array $attributes) |
| 122 |
{ |
| 123 |
return new $this->model(array_map(function($attribute) { |
| 124 |
return $attribute instanceof Closure ? $attribute() : $attribute; |
| 125 |
}, array_merge($this->definition(), $attributes))); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Get a new Faker instance. |
| 130 |
* |
| 131 |
* @since 2.20.0 |
| 132 |
*/ |
| 133 |
protected function withFaker(): Generator |
| 134 |
{ |
| 135 |
return give()->make(Generator::class); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Configure the factory. |
| 140 |
* |
| 141 |
* @since 2.20.0 |
| 142 |
*/ |
| 143 |
public function configure(): self |
| 144 |
{ |
| 145 |
return $this; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Sets the number of models to generate. |
| 150 |
* |
| 151 |
* @since 2.20.0 |
| 152 |
*/ |
| 153 |
public function count(int $count): self |
| 154 |
{ |
| 155 |
$this->count = $count; |
| 156 |
|
| 157 |
return $this; |
| 158 |
} |
| 159 |
} |
| 160 |
|