Closed vitek-dev closed 2 years ago
I just came across this and I think the way cast works isn't properly explained in the documentation. All it does is instantiate an object and then map an array to public properties. It also won't work with any type that has constructor arguments.
I think you should use normalization - the following works:
$schema = Expect::structure([
'created_at' => Expect::type(DateTime::class)->before(function ($date) {
return new DateTime($data);
}),
]);
Bug Description
I am not sure, if I am using this right, but I am trying to convert input string to result DateTime object
Steps To Reproduce
https://fiddle.nette.org/nette/#4f2cd78abf
Will result in the error:
Expected Behavior
Input string will be successfully mapped to target class DateTime property.