[8.x] Custom casts can implement increment/decrement logic #34964
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.
This PR allows developers to add support of increment and decrement operations on their custom castable attributes. There are no breaking change, new functionality is covered with tests.
Reasoning
Consider a custom caster that casts the value to a value object, for example Money.
As we all know, PHP doesn't support operator overloading, and even if it once will - there's no guarantee that it will be implemented in value object implementation.
Which leads us to the following problem when trying to increment or decrement castable attribute:
Object of class Money\Money could not be converted to number
inModel::incrementOrDecrement
method:Why do we ever need this?
Naming problem
I've used word deviate as aggregate term for both increment and decrement. For example:
It looks much more clean and, in my opinion, still perfectly describes logic behind it.
I'm welcome to adopt a better alternative if any.
Conclusion
This is my first PR ever, so please don't judge too much.
For my test I've reused Decimal value object class and DecimalCaster from PR #34702.
Also mentioned PR was good starting point and reference for my work.