master

laravel/framework

Last updated at: 29/12/2023 09:20

Conditionable.php

TLDR

This file is a trait named Conditionable that provides two methods for conditionally applying callbacks based on a given value. The when method applies the callback if the value is truthy, while the unless method applies the callback if the value is falsy.

Methods

when

Apply the callback if the given "value" is (or resolves to) truthy.

  • Parameters:
    • $value: The value to be checked. It can be a closure that returns the value or the value itself. It is nullable.
    • $callback: The callback to be applied if the value is truthy. It is nullable.
    • $default: The callback to be applied if the value is falsy. It is nullable.
  • Return type: It returns $this or the return type of the callback.

unless

Apply the callback if the given "value" is (or resolves to) falsy.

  • Parameters:
    • $value: The value to be checked. It can be a closure that returns the value or the value itself. It is nullable.
    • $callback: The callback to be applied if the value is falsy. It is nullable.
    • $default: The callback to be applied if the value is truthy. It is nullable.
  • Return type: It returns $this or the return type of the callback.
<?php

namespace Illuminate\Support\Traits;

use Closure;
use Illuminate\Support\HigherOrderWhenProxy;

trait Conditionable
{
    /**
     * Apply the callback if the given "value" is (or resolves to) truthy.
     *
     * @template TWhenParameter
     * @template TWhenReturnType
     *
     * @param  (\Closure($this): TWhenParameter)|TWhenParameter|null  $value
     * @param  (callable($this, TWhenParameter): TWhenReturnType)|null  $callback
     * @param  (callable($this, TWhenParameter): TWhenReturnType)|null  $default
     * @return $this|TWhenReturnType
     */
    public function when($value = null, callable $callback = null, callable $default = null)
    {
        $value = $value instanceof Closure ? $value($this) : $value;

        if (func_num_args() === 0) {
            return new HigherOrderWhenProxy($this);
        }

        if (func_num_args() === 1) {
            return (new HigherOrderWhenProxy($this))->condition($value);
        }

        if ($value) {
            return $callback($this, $value) ?? $this;
        } elseif ($default) {
            return $default($this, $value) ?? $this;
        }

        return $this;
    }

    /**
     * Apply the callback if the given "value" is (or resolves to) falsy.
     *
     * @template TUnlessParameter
     * @template TUnlessReturnType
     *
     * @param  (\Closure($this): TUnlessParameter)|TUnlessParameter|null  $value
     * @param  (callable($this, TUnlessParameter): TUnlessReturnType)|null  $callback
     * @param  (callable($this, TUnlessParameter): TUnlessReturnType)|null  $default
     * @return $this|TUnlessReturnType
     */
    public function unless($value = null, callable $callback = null, callable $default = null)
    {
        $value = $value instanceof Closure ? $value($this) : $value;

        if (func_num_args() === 0) {
            return (new HigherOrderWhenProxy($this))->negateConditionOnCapture();
        }

        if (func_num_args() === 1) {
            return (new HigherOrderWhenProxy($this))->condition(! $value);
        }

        if (! $value) {
            return $callback($this, $value) ?? $this;
        } elseif ($default) {
            return $default($this, $value) ?? $this;
        }

        return $this;
    }
}