Filterable
Home
πŸ“¦ Installation
GitHub
Home
πŸ“¦ Installation
GitHub
  • Home
  • Introduction
  • Installation
  • How It Works
  • Engines

    • Invokable
    • Tree
    • Ruleset
    • Expression
  • Features

    • Header-Driven Filter Mode
    • Auto Register Filterable Macro
    • Conditional Logic
    • Filter Aliases
    • Through callbacks
    • Auto Binding
  • Execution

    • Invoker
  • API

    • Payload
    • Sorter
  • Profiler
  • Sorting
  • Authorization
  • Validation
  • Sanitization

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.
Edit this page
Last Updated:
Contributors: Abdalrhman Emad Saad
Prev
Auto Register Filterable Macro
Next
Filter Aliases