Skip to content

Automatic create objects #1119

Description

@TheDevick

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')

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions