Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose PropertyHydrationDefinition to PropertyCaster::cast() to access inferred type #68

Open
JRoblesAtQuadrant opened this issue Apr 26, 2024 · 0 comments

Comments

@JRoblesAtQuadrant
Copy link

JRoblesAtQuadrant commented Apr 26, 2024

I've got a model, that uses several VO properties, using a ValueObjectInterface.

Trying to create a generic PropertyCaster, CastToValueObjectInterface, I miss the concrete Attribute target (the property on whom is applied) to hydrate the property without need to explicity add the class to be hydrated on the attribute

class Name implements ValueObject {
     public function from(mixed $value): self
}

final readonly class Model {
    #[CastAsValueObject]
    public Name $name
}

readonly class CastToValueObjectInterface implements PropertyCaster {
    public function cast(mixed $value, ObjectMapper $hydrator, PropertyHydrationDefinition $context=null): mixed
    {
        if (!$context?->firstTypeName instanceOf ValueObject)
            throw \InvalidArgumentException("Annotated property does not implement ValueObject");
            
       return ($context->firstTypeName)::from($value);
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant