Skip to content

Conversation

@rochamarcelo
Copy link
Contributor

Added else callable for Collection::when and Collection::unless

Now we have the else part of a condition

   $result = $collection->when(
        $user->is_admin,
        fn(Collection $it) => $it->filter(fn($v) => $v > 2),
        fn(Collection $it) => $it->filter(fn($v) => $v > 4),
   )->toList();

* @inheritDoc
*/
public function unless(mixed $condition, callable $callback): CollectionInterface
public function unless(mixed $condition, callable $callback, ?callable $else = null): CollectionInterface
Copy link
Member

@ADmad ADmad Dec 19, 2025

Choose a reason for hiding this comment

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

So unless() is now just when() with 2nd and 3rd arguments swapped. We can avoid increasing the API surface by only keeping when() will 2nd and 3rd arguments both nullable (we can throw an exception if at least one of them is not passed).

Copy link
Contributor Author

@rochamarcelo rochamarcelo Dec 19, 2025

Choose a reason for hiding this comment

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

so if the user wants a unless behavior they should use this?

$collection->when($user->is_admin, else: fn($it) => $it->filter(....))

I'm not sure if this is clear

Copy link
Member

Choose a reason for hiding this comment

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

Why do unless() and when() need support for else? The two methods already give you a way to do if/else style filtering logic.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

when is normally a conditional 'flow' with two parts

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If your condition is the result of a function you don't need to assign the value to variable,

//One method call
$collection->when(
    $Table->exists(['id' => 10]), 
    fn($it) => $it->filter(....), 
    fn($it) => $it->filter(....)
);

//instead of multiple calls
$collection->when(
    $Table->exists(['id' => 10]), 
    fn($it) => $it->filter(....),
)->unless(
    $Table->exists(['id' => 10]), 
    fn($it) => $it->filter(....),
);

//instead of using a var
$condition = $Table->exists(['id' => 10]);
$collection->when(
    $condition, 
    fn($it) => $it->filter(....),
)->unless(
    $condition,
    fn($it) => $it->filter(....),
);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To be fair I would prefer to negate the condition and have
$collection->when(!$user->is_admin, fn($it) => $it->filter(....)) than $collection->when($user->is_admin, else: fn($it) => $it->filter(....))

@markstory markstory added this to the 5.3.0 milestone Dec 19, 2025
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