foundry icon indicating copy to clipboard operation
foundry copied to clipboard

Calling setters with more than one argument?

Open mpdude opened this issue 2 years ago • 2 comments

Is is possible to define attributes in a way that the corresponding method (a setter) is called with more than just one argument?

For example, assume I have an Order class that has an appendLineItem($item, $quantity) method.

Can I define my OrderFactory to include a method like...

class OrderFactory {

  public function addItem($item, $quantity): self
  { 
    return $this->addState(['appendLineItem' => [$item, $quantity]]);
  }

The idea is that the attribute value is an array and would be mapped to parameters for the appendLineItem method.

mpdude avatar Jul 05 '23 13:07 mpdude

Would a post-instantiate hook work for this?

kbond avatar Jul 10 '23 10:07 kbond

I would indeed use a hook for this.

The requested behavior is currently not possible, and I think it could be complex to implement, since we use Symfony's property accessor to hydrate objects. And presently we don't have any clue to know if the array should be use as is or if it should be destructed and each item in the array would represent a parameter of the target method

nikophil avatar Jul 11 '23 08:07 nikophil

I'm closing this since it's a stale issue, and I don't think we will ever support this (beside of using a post instantiate hook)

nikophil avatar Jun 23 '24 16:06 nikophil