Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A 'virtual properties' attribute #3

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from
Draft

Conversation

gwillz
Copy link
Collaborator

@gwillz gwillz commented Nov 2, 2022

We already have a very handy UpdateVirtualTrait that lets us apply functions whenever updating data via constructor or update(). This lets us normalise or parse complex data. However, it's a little cumbersome.

This lets one declare an attribute for properties that have a respective method that applies these normalisations, instead of defining them in the virtual() helper.

There are two core attributes:

  • VirtualProperty - this defines a method to call when updating this property.
  • VirtualObject - this wraps a value (or array of values) in the target object.

Such as:

class MyModel extends DataObject
{
   use UpdateTrait;
   use AttributesVirtualTrait;

   #[VirtualProperty('setComplex')]
   public $complex;

   #[VirtualObject(OtherModel::class)]
   public $object;
   
   public function setComplex($value)
   {
      this->complex = new Complex($value);
   }
}

This PR also includes a helper trait (VirtualHelperTrait) that creates helpers to configure objects on update().

Draft because:

  • no tests
  • I want to rename VirtualHelperTrait to something more obvious? Or just bundle it with the existing UpdateVirtualTrait?
  • I figure the post-modification style applyVirtual() isn't appropriate for strict typings in PHP 8+. This is fixed in the feat/hooks branch.

@gwillz gwillz force-pushed the feat/virtual-attributes branch 2 times, most recently from cc345ac to 22b31e4 Compare November 4, 2022 03:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant