Is it possible to force casting to be applied #352
-
Hi, I'm in the process of trying to move from My DTO looks like this:
with the
if I try to do the following:
I get the following exception:
If I pass a string in, then the casting works fine. Am I misunderstanding how I should cast a property from other class types? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Could you try this again with the newest version of the package? I cannot reproduce it: it('cacas', function () {
class CarbonImmutableCaster implements Cast
{
public function cast(DataProperty $property, mixed $value, array $context): mixed
{
if ($value instanceof CarbonImmutable) {
return $value;
}
if ($value instanceof DateTime) {
return CarbonImmutable::createFromMutable($value);
}
if (is_string($value)) {
return CarbonImmutable::parse($value);
}
return new Uncastable();
}
}
$dataClass = new class extends Data {
#[WithCast(CarbonImmutableCaster::class)]
public CarbonImmutable $retrievedAt;
};
dd($dataClass::from([
'retrievedAt' => new DateTime()
]));
}); Seems like you're still using v1 if I'm correct? A lot has changed since then 😄 |
Beta Was this translation helpful? Give feedback.
Could you try this again with the newest version of the package?
I cannot reproduce it: