It is better that \App\Factory\UserFactory::getDefaults return an entity object.
Sample code:
protected function getDefaults(): object
{
$entity = new User();
$entity->setUsername(self::faker()->userName());
// other properties...
return $entity;
}
I think it will be more readable and IDE friendly.
Now we can use a workaround method to do the same things, but it is better do it in the lib itself.
// Inject Symfony\Component\Serializer\Normalizer\NormalizerInterface via constructor.
protected function getDefaults(): object
{
$entity = new User();
$entity->setUsername(self::faker()->userName());
// other properties...
return $this->normalizer->normalize($entity, 'array');
}
I do see what you are getting at here. Allowing objects here would certainly make renaming entity fields easier (with an IDE).
I don't see how we can achieve in a BC way though. The getDefaults() returning an array (or closure that returns an array) is meant to be merged with different factory states (which also return an array) and finally merged with the array passed to Factory::create(). This final array is then used to construct the object.
my 2 cents here: while I think it could be a good idea, I don't see how we could have the same nice DX we have today in Foundry with this approach...
I don't think we want to support this