Allow casters to parse null values and provide default or Injected values #62
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In my project I'm using the ObjectHydrator in order to parse api calls to commands and queries.
One caster that would really improve DX would be a "AuthenticatedUser" caster, providing the UserId that is authenticated in the session.
In code, usage of it would look like this:
And the "caster" itself would look something like this:
In order for this to work, we need to skip the null check before casters are triggered, and allow casters to process a value of null. This PR enables that.
Technically we could discuss if providing the Authenticated User ID is a "caster". However I wouldn't know what else to call it, and it seems a bit heavy to introduce a lot of code to support this simple use-case. Open to suggestions though!
Note, this is a breaking change, since current casters might not expect to handle
null
as value.See the change I had to make to src/PropertyCasters/CastToDateTimeImmutable.php as example.