-
Notifications
You must be signed in to change notification settings - Fork 11.8k
[12.x] Add until to tap
#58491
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
base: 12.x
Are you sure you want to change the base?
[12.x] Add until to tap
#58491
Conversation
|
Thanks for submitting a PR! Note that draft PRs are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface. Pull requests that are abandoned in draft may be closed due to inactivity. |
|
The tests are constantly timing out for reasons I don't understand |
|
I was thinking something like this is much clearer than having to specify arbitrary number, albeit class must be tappable. $obj = new Object();
$obj->tap->requestRecords()
->tap->applyChanges()
->tap->commit()
->all(); |
|
That works for classes, but not so much for the plain helper on non-tappable objects: $obj = new Object();
tap(tap(tap($obj->requestRecords())->applyChanges())->commit()->all(); |
|
What about this: $tappable = tap($object);
$results = $tappable->tap->requestRecords()
->tap->applyChanges()
->tap->commit()
->all();Basically, extend the original proxy object to allow tapping into the object and returning the same tap instance regardless of the result, or getting the tapped object using public static __call(string $name, array $arguments)
{
if ($name === 'tap') {
return new static($this);
}
if ($name === 'object') {
return $this->value;
}
// ...
} |
Changes
tap_until()helper with callback that returns theHigherOrderTapProxyas long as the given condition is met, otherwise it returns the value as usualtap()to allow theHigherOrderTapProxyto be called a given number of times before returning the value (as shortcut fortap_until())Examples
Tap n times
Tap until arbitrary condition is met
Infinite tap/unlimited tap (essentially @DarkGhostHunter's multi tap)
Previous art
tap()but instead of a callback, it was a simple boolean which made it inflexible and not much different to thewhen()helper as pointed out in the comments of said PR ([5.6] Add optional condition to tap() #23452 (comment))HigherOrderTapProxybut with no limit, which would have been a breaking changeuntilalso does this, but in another way), which might look inconvenient