efureev/php-support

View on GitHub
src/Traits/Whener.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php

declare(strict_types=1);

namespace Php\Support\Traits;

/**
 * Trait Whener
 * @package Php\Support\Traits
 */
trait Whener
{
    /**
     * @param mixed $value
     * @param callable $callback
     * @param null|callable $default
     *
     * @return $this
     */
    public function when($value, callable $callback, ?callable $default = null): self
    {
        if ($value) {
            return $callback($this, $value) ?: $this;
        }

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

        return $this;
    }
}