Conditional Logic with when
Overview
The when()
method allows you to conditionally modify the instance based on a boolean expression. Instead of writing verbose conditionals, when()
helps you write expressive, chainable, and concise logic to update your filter configuration. Unlike immutability patterns, this method modifies the current instance directly and returns $this, making it perfect for method chaining.
β¨ Usage
$filter = Filterable::create()
->when($isAdmin, function (Filterable $filter) {
$filter->setAllowedFields(['email', 'role']);
});
In this example, the setAllowedFields() call is only executed if $isAdmin is true.
π Nesting
Filterable::create()->when(true, function ($filter) {
$filter->setAllowedFields(['name']);
$filter->when(true, function ($filter) {
$filter->setAllowedFields(['email', 'phone']);
$filter->when(true, fn($f) => $f->setAllowedFields(['address']));
});
});
π¬ Behavior
If $condition is true β callback is invoked with the current instance.
If $condition is false β nothing happens.
In both cases, the original instance is returned.
π‘ Benefits
- β¨ Cleaner Code: Eliminates the need for verbose if conditions. Just chain your logic fluently using when().
- π§ Improved Readability: The code reads naturally, e.g., βWhen the condition is true, apply this logic.β
- π§ͺ Easier Testing: Testing conditional filter logic becomes straightforward and expressive.
- π Supports Nesting: Allows deeply nested conditional logic while keeping the syntax clean and expressive.
- π Chainable Design: when() returns the same instance, enabling seamless method chaining without breaking flow.