Cast using expected type
This is a breaking change, since the interface of the caster changes. So probably need to be queued for a major release.
In my casters I sometimes need the expected type. Eg: when casting a EventSauce Id It is cumbersome to pass the expected ID class to the caster when it is already typehinted in the constructor
final readonly class SomeCommand
{
public function __construct(
#[IdCaster(AuthorId::class)]
public AuthorId $authorId,
) {}
}
The expected type is already known in the hydrator, so with this PR we can change the caster to:
final readonly class SomeCommand
{
public function __construct(
#[IdCaster]
public AuthorId $authorId,
) {}
}
Or even register it is default caster, so we don't have to annotate our ID's anymore.
This is a breaking change, since the interface of the caster changes
@Robertbaelde I am looking for similar functionality, wouldn't it be possible to do this without a breaking change if we created 2 new interfaces and deprecated the older ones (whilst still supporting them)?
I quite like the potential solution outlined in #68. Rather than just giving the type as a string passing in the full context of the definition could allow all sorts of fun things.
public function cast(
mixed $value,
ObjectMapper $hydrator,
?PropertyHydrationDefinition $context = null,
): mixed
Would be nice to get some sort of solution in for value objects though. Whatever route is taken.