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

[9.x] Support conditionables that get condition from target object #43449

Conversation

ralphjsmit
Copy link
Contributor

@ralphjsmit ralphjsmit commented Jul 27, 2022

This PR allows people to use the ->when() and ->unless() methods without passing in any parameters.

Currently, people are required to pass in the condition as the first parameter:

Storage::disk('orders')
    ->when($condition = fn(Filesystem $disk) => $disk->missing('someDirectory'))
    ->makeDirectory('someDirectory');

The Conditionable will execute the closure and store that as the condition. The next method call ("makeDirectory") will be proxied to the original object based on that condition.

This PR allows to create a proxy without an initial condition. The first method call to the proxy will become the condition, and the second call will be proxied to the original object based on that condition.

With this change, the above example would become:

Storage::disk('orders')
    ->when()->missing('someDirectory')
    ->makeDirectory('someDirectory');

(In this example I'll assume that the #43450 PR gets merged first, but you certainly can get the general idea.)

Thanks!

@GrahamCampbell GrahamCampbell changed the title Support conditionables that get condition from target object [9.x] Support conditionables that get condition from target object Jul 27, 2022
* @return void
*/
public function __construct($target, $condition)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing this is a breaking change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right that it is a breaking change, though isn't it considered part of the internal API?

* @param (callable($this, TWhenParameter): TWhenReturnType)|null $callback
* @param (callable($this, TWhenParameter): TWhenReturnType)|null $default
* @return $this|TWhenReturnType
*/
public function when($value, callable $callback = null, callable $default = null)
public function when($value = null, callable $callback = null, callable $default = null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing the method signature here is a breaking change.

@taylorotwell taylorotwell merged commit 0047667 into laravel:9.x Jul 29, 2022
Ken-vdE pushed a commit to Ken-vdE/framework that referenced this pull request Aug 9, 2022
…aravel#43449)

* Support conditionables that get condition from target object

* Style

* Rename method to be more explicit

* Add test & support for properties

* Update SupportConditionableTest.php

* formatting

Co-authored-by: Taylor Otwell <[email protected]>
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.

3 participants