Skip to content

Commit 5bab12c

Browse files
committed
doc: update
1 parent 4e33e3e commit 5bab12c

File tree

1 file changed

+36
-69
lines changed

1 file changed

+36
-69
lines changed

docs/index.rst

+36-69
Original file line numberDiff line numberDiff line change
@@ -129,65 +129,67 @@ This command will generate a ``PostFactory`` class that looks like this:
129129
130130
use App\Entity\Post;
131131
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;
135135
136136
/**
137-
* @extends ModelFactory<Post>
137+
* @extends PersistentProxyObjectFactory<Post>
138138
*
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 = [])
154154
*/
155-
final class PostFactory extends ModelFactory
155+
final class PostFactory extends PersistentProxyObjectFactory
156156
{
157157
/**
158-
* @see https://github.com/zenstruck/foundry#factories-as-services
158+
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#factories-as-services
159159
*
160160
* @todo inject services if required
161161
*/
162162
public function __construct()
163163
{
164-
parent::__construct();
164+
}
165+
166+
public static function class(): string
167+
{
168+
return Post::class;
165169
}
166170
167171
/**
168-
* @see https://github.com/zenstruck/foundry#model-factories
172+
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#model-factories
169173
*
170174
* @todo add your default values here
171175
*/
172-
protected function getDefaults(): array
176+
protected function defaults(): array|callable
173177
{
174-
return [];
178+
return [
179+
'createdAt' => \DateTimeImmutable::createFromMutable(self::faker()->dateTime()),
180+
'title' => self::faker()->text(255),
181+
];
175182
}
176183
177184
/**
178-
* @see https://github.com/zenstruck/foundry#initialization
185+
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization
179186
*/
180-
protected function initialize(): self
187+
protected function initialize(): static
181188
{
182189
return $this
183-
// ->afterInstantiate(function(Post $post) {})
190+
// ->afterInstantiate(function(Post $post): void {})
184191
;
185192
}
186-
187-
protected static function getClass(): string
188-
{
189-
return Post::class;
190-
}
191193
}
192194
193195
.. tip::
@@ -207,45 +209,10 @@ This command will generate a ``PostFactory`` class that looks like this:
207209
zenstruck_foundry:
208210
make_factory:
209211
default_namespace: 'App\\MyFactories'
210-
make_story:
211-
default_namespace: 'App\\MyStories'
212212
213213
You can override this configuration by using the ``--namespace`` option.
214214

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
249216
should have. `Faker`_ is available to easily get random data:
250217

251218
.. code-block:: php

0 commit comments

Comments
 (0)