@@ -129,65 +129,67 @@ This command will generate a ``PostFactory`` class that looks like this:
129
129
130
130
use App\Entity\Post;
131
131
use App\Repository\PostRepository;
132
- use Zenstruck\Foundry\RepositoryProxy ;
133
- use Zenstruck\Foundry\ModelFactory ;
134
- use Zenstruck\Foundry\Proxy ;
132
+ use Zenstruck\Foundry\Persistence\PersistentProxyObjectFactory ;
133
+ use Zenstruck\Foundry\Persistence\Proxy ;
134
+ use Zenstruck\Foundry\Persistence\ProxyRepositoryDecorator ;
135
135
136
136
/**
137
- * @extends ModelFactory <Post >
137
+ * @extends PersistentProxyObjectFactory <Post >
138
138
*
139
- * @method Post|Proxy create(array|callable $attributes = [])
140
- * @method static Post|Proxy createOne(array $attributes = [])
141
- * @method static Post|Proxy find(object|array|mixed $criteria)
142
- * @method static Post|Proxy findOrCreate(array $attributes)
143
- * @method static Post|Proxy first(string $sortedField = 'id')
144
- * @method static Post|Proxy last(string $sortedField = 'id')
145
- * @method static Post|Proxy random(array $attributes = [])
146
- * @method static Post|Proxy randomOrCreate(array $attributes = []) )
147
- * @method static PostRepository|RepositoryProxy repository()
148
- * @method static Post[]|Proxy[] all()
149
- * @method static Post[]|Proxy[] createMany(int $number, array|callable $attributes = [])
150
- * @method static Post[]& Proxy[] createSequence(iterable|callable $sequence)
151
- * @method static Post[]|Proxy[] findBy(array $attributes)
152
- * @method static Post[]|Proxy[] randomRange(int $min, int $max, array $attributes = []) )
153
- * @method static Post[]|Proxy[] randomSet(int $number, array $attributes = []) )
139
+ * @method Post|Proxy create(array|callable $attributes = [])
140
+ * @method static Post|Proxy createOne(array $attributes = [])
141
+ * @method static Post|Proxy find(object|array|mixed $criteria)
142
+ * @method static Post|Proxy findOrCreate(array $attributes)
143
+ * @method static Post|Proxy first(string $sortedField = 'id')
144
+ * @method static Post|Proxy last(string $sortedField = 'id')
145
+ * @method static Post|Proxy random(array $attributes = [])
146
+ * @method static Post|Proxy randomOrCreate(array $attributes = [])
147
+ * @method static PostRepository|ProxyRepositoryDecorator repository()
148
+ * @method static Post[]|Proxy[] all()
149
+ * @method static Post[]|Proxy[] createMany(int $number, array|callable $attributes = [])
150
+ * @method static Post[]| Proxy[] createSequence(iterable|callable $sequence)
151
+ * @method static Post[]|Proxy[] findBy(array $attributes)
152
+ * @method static Post[]|Proxy[] randomRange(int $min, int $max, array $attributes = [])
153
+ * @method static Post[]|Proxy[] randomSet(int $number, array $attributes = [])
154
154
*/
155
- final class PostFactory extends ModelFactory
155
+ final class PostFactory extends PersistentProxyObjectFactory
156
156
{
157
157
/**
158
- * @see https://github .com/zenstruck/foundry #factories-as-services
158
+ * @see https://symfony .com/bundles/ZenstruckFoundryBundle/current/index.html #factories-as-services
159
159
*
160
160
* @todo inject services if required
161
161
*/
162
162
public function __construct()
163
163
{
164
- parent::__construct();
164
+ }
165
+
166
+ public static function class(): string
167
+ {
168
+ return Post::class;
165
169
}
166
170
167
171
/**
168
- * @see https://github .com/zenstruck/foundry #model-factories
172
+ * @see https://symfony .com/bundles/ZenstruckFoundryBundle/current/index.html #model-factories
169
173
*
170
174
* @todo add your default values here
171
175
*/
172
- protected function getDefaults (): array
176
+ protected function defaults (): array|callable
173
177
{
174
- return [];
178
+ return [
179
+ 'createdAt' => \DateTimeImmutable::createFromMutable(self::faker()->dateTime()),
180
+ 'title' => self::faker()->text(255),
181
+ ];
175
182
}
176
183
177
184
/**
178
- * @see https://github .com/zenstruck/foundry #initialization
185
+ * @see https://symfony .com/bundles/ZenstruckFoundryBundle/current/index.html #initialization
179
186
*/
180
- protected function initialize(): self
187
+ protected function initialize(): static
181
188
{
182
189
return $this
183
- // ->afterInstantiate(function(Post $post) {})
190
+ // ->afterInstantiate(function(Post $post): void {})
184
191
;
185
192
}
186
-
187
- protected static function getClass(): string
188
- {
189
- return Post::class;
190
- }
191
193
}
192
194
193
195
.. tip ::
@@ -207,45 +209,10 @@ This command will generate a ``PostFactory`` class that looks like this:
207
209
zenstruck_foundry :
208
210
make_factory :
209
211
default_namespace : ' App\\MyFactories'
210
- make_story :
211
- default_namespace : ' App\\MyStories'
212
212
213
213
You can override this configuration by using the ``--namespace `` option.
214
214
215
-
216
- .. note ::
217
-
218
- The generated ``@method `` docblocks above enable autocompletion with PhpStorm but
219
- cause errors with PHPStan and Psalm. To support PHPStan or Psalm for your factory's, you need to *also *
220
- add the following dockblocks (replace ``phpstan- `` prefix by ``psalm- `` accordingly to your static analysis tool):
221
-
222
- .. code-block :: php
223
-
224
- /**
225
- * ...
226
- *
227
- * @phpstan-method Proxy<Post > create(array|callable $attributes = [])
228
- * @phpstan-method static Proxy<Post > createOne(array $attributes = [])
229
- * @phpstan-method static Proxy<Post > find(object|array|mixed $criteria)
230
- * @phpstan-method static Proxy<Post > findOrCreate(array $attributes)
231
- * @phpstan-method static Proxy<Post > first(string $sortedField = 'id')
232
- * @phpstan-method static Proxy<Post > last(string $sortedField = 'id')
233
- * @phpstan-method static Proxy<Post > random(array $attributes = [])
234
- * @phpstan-method static Proxy<Post > randomOrCreate(array $attributes = [])
235
- * @phpstan-method static RepositoryProxy<Post > repository()
236
- * @phpstan-method static list<Proxy <Post >> all()
237
- * @phpstan-method static list<Proxy <Post >> createMany(int $number, array|callable $attributes = [])
238
- * @phpstan-method static list<Proxy <Post >> createSequence(iterable|callable $sequence)
239
- * @phpstan-method static list<Proxy <Post >> findBy(array $attributes)
240
- * @phpstan-method static list<Proxy <Post >> randomRange(int $min, int $max, array $attributes = [])
241
- * @phpstan-method static list<Proxy <Post >> randomSet(int $number, array $attributes = [])
242
- */
243
- final class PostFactory extends ModelFactory
244
- {
245
- // ...
246
- }
247
-
248
- In the ``getDefaults() ``, you can return an array of all default values that any new object
215
+ In the ``defaults() ``, you can return an array of all default values that any new object
249
216
should have. `Faker `_ is available to easily get random data:
250
217
251
218
.. code-block :: php
0 commit comments