Skip to content

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

Merged
taylorotwell merged 6 commits into
laravel:9.xfrom
ralphjsmit:conditionable-with-condition-from-target-object
Jul 29, 2022
Merged

[9.x] Support conditionables that get condition from target object#43449
taylorotwell merged 6 commits into
laravel:9.xfrom
ralphjsmit:conditionable-with-condition-from-target-object

Conversation

@ralphjsmit

@ralphjsmit ralphjsmit commented Jul 27, 2022

Copy link
Copy Markdown
Contributor

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
* @param bool $condition
* @return void
*/
public function __construct($target, $condition)

Copy link
Copy Markdown
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
Copy Markdown
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?

* @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
Copy Markdown
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 <taylor@laravel.com>
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