|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Zenstruck\Foundry; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* @template TModel of object |
|
7
|
|
|
* @template-extends Factory<TModel> |
|
8
|
|
|
* |
|
9
|
|
|
* @method static Proxy[]|object[] createMany(int $number, array|callable $attributes = []): array |
|
10
|
|
|
* @psalm-method static list<Proxy<TModel>> createMany(int $number, array|callable $attributes = []): array |
|
11
|
|
|
* |
|
12
|
|
|
* @author Kevin Bond <[email protected]> |
|
13
|
604 |
|
*/ |
|
14
|
|
|
abstract class ModelFactory extends Factory |
|
15
|
604 |
|
{ |
|
16
|
604 |
|
public function __construct() |
|
17
|
|
|
{ |
|
18
|
|
|
parent::__construct(static::getClass()); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param array|callable|string $defaultAttributes If string, assumes state |
|
23
|
|
|
* @param string ...$states Optionally pass default states (these must be methods on your ObjectFactory with no arguments) |
|
24
|
611 |
|
* |
|
25
|
|
|
* @return static |
|
26
|
611 |
|
*/ |
|
27
|
10 |
|
final public static function new($defaultAttributes = [], string ...$states): self |
|
28
|
10 |
|
{ |
|
29
|
|
|
if (\is_string($defaultAttributes)) { |
|
30
|
|
|
$states = \array_merge([$defaultAttributes], $states); |
|
31
|
|
|
$defaultAttributes = []; |
|
32
|
611 |
|
} |
|
33
|
16 |
|
|
|
34
|
10 |
|
try { |
|
35
|
|
|
$factory = self::isBooted() ? self::configuration()->factories()->create(static::class) : new static(); |
|
36
|
|
|
} catch (\ArgumentCountError $e) { |
|
37
|
|
|
throw new \RuntimeException('Model Factories with dependencies (Model Factory services) cannot be created before foundry is booted.', 0, $e); |
|
38
|
604 |
|
} |
|
39
|
604 |
|
|
|
40
|
604 |
|
$factory = $factory |
|
41
|
|
|
->withAttributes([$factory, 'getDefaults']) |
|
42
|
|
|
->withAttributes($defaultAttributes) |
|
43
|
604 |
|
->initialize() |
|
44
|
20 |
|
; |
|
45
|
|
|
|
|
46
|
|
|
if (!$factory instanceof static) { |
|
|
|
|
|
|
47
|
600 |
|
throw new \TypeError(\sprintf('"%1$s::initialize()" must return an instance of "%1$s".', static::class)); |
|
48
|
10 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
foreach ($states as $state) { |
|
51
|
600 |
|
$factory = $factory->{$state}(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return $factory; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* A shortcut to create a single model without states. |
|
59
|
|
|
* |
|
60
|
|
|
* @return Proxy|object |
|
61
|
|
|
* |
|
62
|
10 |
|
* @psalm-return Proxy<TModel> |
|
63
|
|
|
*/ |
|
64
|
10 |
|
final public static function createOne(array $attributes = []): Proxy |
|
65
|
10 |
|
{ |
|
66
|
|
|
return static::new()->create($attributes); |
|
67
|
|
|
} |
|
68
|
10 |
|
|
|
69
|
|
|
public static function __callStatic(string $name, array $arguments) |
|
70
|
|
|
{ |
|
71
|
|
|
if ($name !== 'createMany') { |
|
72
|
|
|
throw new \BadMethodCallException(sprintf('Call to undefined static method "%s::%s".', __CLASS__, __METHOD__)); |
|
73
|
|
|
} |
|
74
|
20 |
|
|
|
75
|
|
|
return static::new()->many($arguments[0])->create($arguments[1] ?? []); |
|
76
|
20 |
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Try and find existing object for the given $attributes. If not found, |
|
80
|
|
|
* instantiate and persist. |
|
81
|
|
|
* |
|
82
|
10 |
|
* @return Proxy|object |
|
83
|
|
|
* |
|
84
|
10 |
|
* @psalm-return Proxy<TModel> |
|
85
|
|
|
*/ |
|
86
|
|
|
final public static function findOrCreate(array $attributes): Proxy |
|
87
|
|
|
{ |
|
88
|
|
|
if ($found = static::repository()->find($attributes)) { |
|
89
|
|
|
return \is_array($found) ? $found[0] : $found; |
|
|
|
|
|
|
90
|
10 |
|
} |
|
91
|
|
|
|
|
92
|
10 |
|
return static::new()->create($attributes); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
210 |
|
* @see RepositoryProxy::random() |
|
97
|
|
|
*/ |
|
98
|
210 |
|
final public static function random(): Proxy |
|
99
|
|
|
{ |
|
100
|
|
|
return static::repository()->random(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Fetch one random object and create a new object if none exists. |
|
105
|
|
|
* |
|
106
|
600 |
|
* @return Proxy|object |
|
107
|
|
|
* |
|
108
|
600 |
|
* @psalm-return Proxy<TModel> |
|
109
|
|
|
*/ |
|
110
|
|
|
final public static function randomOrCreate(): Proxy |
|
111
|
|
|
{ |
|
112
|
|
|
try { |
|
113
|
|
|
return static::repository()->random(); |
|
114
|
|
|
} catch (\RuntimeException $e) { |
|
115
|
|
|
return static::new()->create(); |
|
116
|
30 |
|
} |
|
117
|
|
|
} |
|
118
|
30 |
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @see RepositoryProxy::randomSet() |
|
121
|
|
|
*/ |
|
122
|
|
|
final public static function randomSet(int $number): array |
|
123
|
|
|
{ |
|
124
|
|
|
return static::repository()->randomSet($number); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @see RepositoryProxy::randomRange() |
|
129
|
|
|
*/ |
|
130
|
|
|
final public static function randomRange(int $min, int $max): array |
|
131
|
|
|
{ |
|
132
|
|
|
return static::repository()->randomRange($min, $max); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** @psalm-return RepositoryProxy<TModel> */ |
|
136
|
|
|
final public static function repository(): RepositoryProxy |
|
137
|
|
|
{ |
|
138
|
|
|
return static::configuration()->repositoryFor(static::getClass()); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Override to add default instantiator and default afterInstantiate/afterPersist events. |
|
143
|
|
|
* |
|
144
|
|
|
* @return static |
|
145
|
|
|
*/ |
|
146
|
|
|
protected function initialize() |
|
147
|
|
|
{ |
|
148
|
|
|
return $this; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @param array|callable $attributes |
|
153
|
|
|
* |
|
154
|
|
|
* @return static |
|
155
|
|
|
*/ |
|
156
|
|
|
final protected function addState($attributes = []): self |
|
157
|
|
|
{ |
|
158
|
|
|
return $this->withAttributes($attributes); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** @psalm-return class-string<TModel> */ |
|
162
|
|
|
abstract protected static function getClass(): string; |
|
163
|
|
|
|
|
164
|
|
|
abstract protected function getDefaults(): array; |
|
165
|
|
|
} |
|
166
|
|
|
|