Open
Conversation
22b31e4 to
e7247c0
Compare
This adds VirtualObject for shorthand goodness using the Configure helper. However, this doesn't gel so well with strict property types introduced in PHP 7.4. We'll likely need to rework how update() and applyVirtual() works.
…ely. This permits 'strict' updates to report errors for bad types.
Lots of complexity and we don't need doc tags.
Why when attributes are always superior? This trait only exists for backward compat.
c0dd6df to
09442b9
Compare
Where the virtual trait is used without the interface. Common if using DataObject instead of Collection as the base class.
UpdateVirtualTrait was already opt-in. So now it simply overrides the default (attributes) behaviour.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
We already have a very handy
UpdateVirtualTraitthat lets us apply functions whenever updating data via constructor orupdate(). This lets us normalise or parse complex data. However, it's a little cumbersome (and broken for strong types).Attributes
Declare an attribute for properties to define how it should be marshalled into the correct type.
There are three attributes:
Such as:
Strong Types
This introduces the
UpdateVirtualInterfacewith asetVirtual()method to replace the wonkyapplyVirtual()approach.Where instead of modifying types in-place on the object (which doesn't work with strong types), instead we convert and apply virtual properties first.
The relevant update traits are updated to use the new interface. This is technically a breaking change.
Important considerations
Something like this feels redundant, and you would be right.
#[VirtualObject(SomeModel::class) public SomeModel $object;The target object is right there and we could simply use that instead of the attribute. However this scenario is also common:
#[VirtualObject(SomeModel::class) public BaseModel $object;Where
BaseModelis abstract or an interface.