1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ChinLeung\Factories; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Illuminate\Support\Arr; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
|
9
|
|
|
abstract class Builder |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* The model class that has been registered in the factory. |
13
|
|
|
* |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $model; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The list of properties to pass to the factory. |
20
|
|
|
* |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $properties = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Create a new instance of a factory builder. |
27
|
|
|
*/ |
28
|
|
|
public function __construct() |
29
|
|
|
{ |
30
|
|
|
if (is_null($this->model)) { |
31
|
|
|
$this->model = config('factories.namespace').'\\'.Str::replaceLast( |
32
|
|
|
'Factory', |
33
|
|
|
'', |
34
|
|
|
class_basename(get_called_class()) |
35
|
|
|
); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Build the models with the parameters. |
41
|
|
|
* |
42
|
|
|
* @param bool $persistent |
43
|
|
|
* @param int|null $count |
44
|
|
|
* @return mixed |
45
|
|
|
*/ |
46
|
|
|
private function build(bool $persistent, ?int $count) |
47
|
|
|
{ |
48
|
|
|
$method = $persistent ? 'create' : 'make'; |
49
|
|
|
|
50
|
|
|
$models = factory($this->model, $count ?? 1)->{$method}( |
51
|
|
|
$this->getProperties() |
52
|
|
|
); |
53
|
|
|
|
54
|
|
|
$models->each(fn ($model) => $this->created($model, $persistent)); |
|
|
|
|
55
|
|
|
|
56
|
|
|
return is_null($count) ? $models->first() : $models; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Create the model. |
61
|
|
|
* |
62
|
|
|
* @param int $count |
63
|
|
|
* @return mixed |
64
|
|
|
*/ |
65
|
|
|
public function create(int $count = null) |
66
|
|
|
{ |
67
|
|
|
return $this->build(true, $count); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Hook to alter the model after it has been created. |
72
|
|
|
* |
73
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
74
|
|
|
* @return \Illuminate\Database\Eloquent\Model |
75
|
|
|
*/ |
76
|
|
|
protected function created(Model $model): Model |
77
|
|
|
{ |
78
|
|
|
return $model; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Clear out the existing properties. |
83
|
|
|
* |
84
|
|
|
* @return self |
85
|
|
|
*/ |
86
|
|
|
public function fresh(): self |
87
|
|
|
{ |
88
|
|
|
$this->properties = []; |
89
|
|
|
|
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Retrieve the properties for the factory. |
95
|
|
|
* |
96
|
|
|
* @return array |
97
|
|
|
*/ |
98
|
|
|
public function getProperties(): array |
99
|
|
|
{ |
100
|
|
|
return $this->properties; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Create the model without saving it in the database. |
105
|
|
|
* |
106
|
|
|
* @param int $count |
107
|
|
|
* @return mixed |
108
|
|
|
*/ |
109
|
|
|
public function make(int $count = null) |
110
|
|
|
{ |
111
|
|
|
return $this->build(false, $count); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Set a property to pass to the factory. |
116
|
|
|
* |
117
|
|
|
* @param string $property |
118
|
|
|
* @param mixed $value |
119
|
|
|
* @return self |
120
|
|
|
*/ |
121
|
|
|
public function setProperty(string $property, $value): self |
122
|
|
|
{ |
123
|
|
|
Arr::set($this->properties, $property, $value); |
124
|
|
|
|
125
|
|
|
return $this; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|