Hello!
I’m working with entities that use Value Objects and dates in their constructors, and I wondered whether Foundry could support automatic instantiation of values passed to factories.
For example, imagine an entity like this:
<?php
namespace App\Entity;
use App\ValueObject\KilowattHour;
class Generation
{
public function __construct(
public KilowattHour $energy,
public \DateTimeImmutable $date,
) {
}
}
Currently, creating an instance manually looks like this:
<?php
use App\Entity\Generation;
use App\ValueObject\KilowattHour;
$generation = new Generation(
new KilowattHour(10),
new \DateTimeImmutable('2025-01-01'),
);
And with Foundry:
<?php
use App\Factory\GenerationFactory;
use App\ValueObject\KilowattHour;
$generation = GenerationFactory::createOne([
'energy' => new KilowattHour(10),
'date' => new \DateTimeImmutable('2025-01-01'),
]);
It would be great if Foundry could automatically instantiate the supported objects.
Something like:
<?php
use App\Factory\GenerationFactory;
$generation = GenerationFactory::createOne([
'energy' => 10,
'date' => '2025-01-01',
]);
Where Foundry would internally resolve:
10 -> new KilowattHour(10)
'2025-01-01' -> new \DateTimeImmutable('2025-01-01')
Hello!
I’m working with entities that use Value Objects and dates in their constructors, and I wondered whether Foundry could support automatic instantiation of values passed to factories.
For example, imagine an entity like this:
Currently, creating an instance manually looks like this:
And with Foundry:
It would be great if Foundry could automatically instantiate the supported objects.
Something like:
Where Foundry would internally resolve:
10->new KilowattHour(10)'2025-01-01'->new \DateTimeImmutable('2025-01-01')