1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zenstruck\Foundry; |
4
|
|
|
|
5
|
|
|
use Faker; |
6
|
|
|
use ProxyManager\Proxy\ValueHolderInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @see Factory::__construct() |
10
|
|
|
* |
11
|
|
|
* @template TObject as object |
12
|
|
|
* @psalm-param class-string<TObject> $class |
13
|
|
|
* @psalm-return AnonymousFactory<TObject> |
14
|
|
|
*/ |
15
|
|
|
function factory(string $class, $defaultAttributes = []): AnonymousFactory |
16
|
110 |
|
{ |
17
|
|
|
return new AnonymousFactory($class, $defaultAttributes); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @see Factory::create() |
22
|
|
|
* |
23
|
|
|
* @return Proxy|object |
24
|
|
|
* |
25
|
|
|
* @template TObject of object |
26
|
|
|
* @psalm-param class-string<TObject> $class |
27
|
|
|
* @psalm-return Proxy<TObject> |
28
|
|
|
*/ |
29
|
|
|
function create(string $class, $attributes = []): Proxy |
30
|
50 |
|
{ |
31
|
|
|
return factory($class)->create($attributes); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @see Factory::createMany() |
36
|
|
|
* |
37
|
|
|
* @return Proxy[]|object[] |
38
|
|
|
* |
39
|
|
|
* @template TObject of object |
40
|
|
|
* @psalm-param class-string<TObject> $class |
41
|
|
|
* @psalm-return list<Proxy<TObject>> |
42
|
|
|
*/ |
43
|
|
|
function create_many(int $number, string $class, $attributes = []): array |
44
|
10 |
|
{ |
45
|
|
|
return factory($class)->many($number)->create($attributes); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Instantiate object without persisting. |
50
|
|
|
* |
51
|
|
|
* @return Proxy|object "unpersisted" Proxy wrapping the instantiated object |
52
|
|
|
* |
53
|
|
|
* @template TObject of object |
54
|
|
|
* @psalm-param class-string<TObject> $class |
55
|
|
|
* @psalm-return Proxy<TObject> |
56
|
|
|
*/ |
57
|
|
|
function instantiate(string $class, $attributes = []): Proxy |
58
|
10 |
|
{ |
59
|
|
|
return factory($class)->withoutPersisting()->create($attributes); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Instantiate X objects without persisting. |
64
|
|
|
* |
65
|
|
|
* @return Proxy[]|object[] "unpersisted" Proxy's wrapping the instantiated objects |
66
|
|
|
* |
67
|
|
|
* @template TObject of object |
68
|
|
|
* @psalm-param class-string<TObject> $class |
69
|
|
|
* @psalm-return list<Proxy<TObject>> |
70
|
|
|
*/ |
71
|
|
|
function instantiate_many(int $number, string $class, $attributes = []): array |
72
|
10 |
|
{ |
73
|
|
|
return factory($class)->withoutPersisting()->many($number)->create($attributes); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @see Configuration::repositoryFor() |
78
|
|
|
*/ |
79
|
|
|
function repository($objectOrClass): RepositoryProxy |
80
|
140 |
|
{ |
81
|
|
|
return Factory::configuration()->repositoryFor($objectOrClass); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @see Factory::faker() |
86
|
|
|
*/ |
87
|
|
|
function faker(): Faker\Generator |
88
|
10 |
|
{ |
89
|
|
|
return Factory::faker(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Enables autorefresh on the model. |
94
|
|
|
* |
95
|
|
|
* @param Proxy|ValueHolderInterface $model |
96
|
|
|
*/ |
97
|
|
|
function enable_autorefresh(object $model): void |
98
|
|
|
{ |
99
|
|
|
if ($model instanceof Proxy) { |
100
|
|
|
$model->enableAutoRefresh(); |
101
|
|
|
|
102
|
|
|
return; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if ($model instanceof ValueHolderInterface) { |
106
|
|
|
$realModel = $model->getWrappedValueHolderValue(); |
107
|
|
|
if (!$realModel) { |
108
|
|
|
return; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$realModel->_foundry_autoRefresh = true; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Disables autorefresh on the model. |
117
|
|
|
* |
118
|
|
|
* @param Proxy|ValueHolderInterface $model |
119
|
|
|
*/ |
120
|
|
|
function disable_autorefresh(object $model): void |
121
|
|
|
{ |
122
|
|
|
if ($model instanceof Proxy) { |
123
|
|
|
$model->disableAutoRefresh(); |
124
|
|
|
|
125
|
|
|
return; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
if ($model instanceof ValueHolderInterface) { |
129
|
|
|
$realModel = $model->getWrappedValueHolderValue(); |
130
|
|
|
if (!$realModel) { |
131
|
|
|
return; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$realModel->_foundry_autoRefresh = false; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Ensures "autoRefresh" is disabled when executing $callback. Re-enables |
140
|
|
|
* "autoRefresh" after executing callback if it was enabled. |
141
|
|
|
* |
142
|
|
|
* @param Proxy|ValueHolderInterface $model |
143
|
|
|
* @param callable $callback (Proxy|ValueHolderInterface $model): void |
144
|
|
|
* |
145
|
|
|
* @template TModel as Proxy|ValueHolderInterface |
146
|
|
|
* @psalm-param TModel $model |
147
|
|
|
* @psalm-param callable(TModel):void $callback |
148
|
|
|
*/ |
149
|
|
|
function without_autorefresh(object $model, callable $callback): void |
150
|
|
|
{ |
151
|
|
|
if ($model instanceof Proxy) { |
152
|
|
|
$model->withoutAutoRefresh($callback); |
153
|
|
|
|
154
|
|
|
return; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
$originalValue = null; |
158
|
|
|
if ($model instanceof ValueHolderInterface) { |
159
|
|
|
$realModel = get_real_object($model); |
160
|
|
|
|
161
|
|
|
$originalValue = \property_exists($realModel, '_foundry_autoRefresh') ? $realModel->_foundry_autoRefresh : true; |
162
|
|
|
$realModel->_foundry_autoRefresh = false; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
($callback)($model); |
166
|
|
|
|
167
|
|
|
if (null !== $originalValue) { |
168
|
|
|
$realModel->_foundry_autoRefresh = $originalValue; |
|
|
|
|
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
function force_set(object $model, string $property, $value): void |
173
|
|
|
{ |
174
|
|
|
force_set_all($model, [$property => $value]); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
function force_set_all(object $model, array $properties): void |
178
|
|
|
{ |
179
|
|
|
foreach ($properties as $property => $value) { |
180
|
|
|
Instantiator::forceSet(get_real_object($model), $property, $value); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @return mixed |
186
|
|
|
*/ |
187
|
|
|
function force_get(object $model, string $property) |
188
|
|
|
{ |
189
|
|
|
return Instantiator::forceGet(get_real_object($model), $property); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @template T of object |
194
|
|
|
* @psalm-param T|Proxy<T>|ValueHolderInterface<T> $proxyObject |
195
|
|
|
* @psalm-return T |
196
|
|
|
* |
197
|
|
|
* @psalm-suppress InvalidReturnType |
198
|
|
|
* @psalm-suppress InvalidReturnStatement |
199
|
|
|
*/ |
200
|
|
|
function get_real_object(object $proxyObject): object |
201
|
|
|
{ |
202
|
|
|
if ($proxyObject instanceof Proxy) { |
203
|
|
|
return $proxyObject->object(); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
if ($proxyObject instanceof ValueHolderInterface && $valueHolder = $proxyObject->getWrappedValueHolderValue()) { |
207
|
|
|
return $valueHolder; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
return $proxyObject; |
211
|
|
|
} |
212
|
|
|
|