EventSaucePHP / ObjectHydrator

Object Hydration library to create Command and Query objects.
MIT License
316 stars 23 forks source link

Outermost class never uses a default caster #73

Open axlon opened 2 months ago

axlon commented 2 months ago

What's wrong

When hydrating a payload back to an object, the hydrator never checks if a default caster exists for the outer most object. Typically this isn't a problem, because most of the time the class you are hydrating consists of properties that the mapper can hydrate (either through casters or just by using reflection). However this also means that you can't use a caster to call another caster (by calling $hydrator->hydrateObject(Some::Class, $somePayload)).

My use case

My current use case for calling one caster from another is that I'm trying to support array shapes, this involves providing an attribute for each key in the array that needs to be cast to a type (e.g. #[CastArrayItem('foo', Foo::class)]), as well as using this package's #[SerializeArrayItems]. For the most part this works fine, but I'm currently unable to hydrate any values that require a caster.

PS. Am happy to attempt a PR if you feel this is the right direction

axlon commented 1 month ago

Some observations:

TL;DR: hydration and serialization are not entirely symmetrical, but both implementations are consistent with each other.