aegisora/is-callable-rule-guardian

Is Callable Rule Guardian provides a simple shortcut for callable validation using aegisora/guardian and aegisora/is-callable-rule

Maintainers

Package info

github.com/Aegisora/is-callable-rule-guardian

pkg:composer/aegisora/is-callable-rule-guardian

Transparency log

Statistics

Installs: 3

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2026-08-18 16:47 UTC

This package is auto-updated.

Last update: 2026-08-18 16:50:08 UTC


README

Latest Version Total Downloads Code Coverage Badge Software License PHPStan Badge

Is Callable Rule Guardian provides a simple shortcut for callable validation using aegisora/guardian and aegisora/is-callable-rule.

It is designed for cases where you want to quickly check whether a value is callable, without manually building an IsCallableRule and a validation pipeline by hand.

This package is built on top of:

โœจ Features

  • ๐Ÿ”น Simple shortcut API for IsCallableRule
  • ๐Ÿ”น Validates that a value is callable via check()
  • ๐Ÿ”น Works with closures, invokable objects, function names and array callables
  • ๐Ÿ”น Uses aegisora/guardian internally
  • ๐Ÿ”น Uses aegisora/is-callable-rule internally
  • ๐Ÿ”น Supports a custom validation exception
  • ๐Ÿ”น Keeps rule execution errors separated from validation errors
  • ๐Ÿ”น Fully compatible with the Aegisora ecosystem
  • ๐Ÿ”น Ready to use out of the box

๐Ÿ“ฆ Installation

composer require aegisora/is-callable-rule-guardian

๐Ÿš€ Core Concept

This package wraps the common callable validation flow:

$guardian->check(
    $value,
    IsCallableRule::create(),
    new ValueIsNotCallableException()
);

into a dedicated shortcut class:

$isCallableRuleGuardian->check($value, new ValueIsNotCallableException());

Instead of manually creating an IsCallableRule and passing it to Guardian, you can use IsCallableRuleGuardian directly.

๐Ÿ—๏ธ Basic Usage

use Aegisora\Guardian\Guardian;
use Aegisora\Guardian\Exceptions\GuardianValidationException;
use Aegisora\RuleGuardians\IsCallableRule\IsCallableRuleGuardian;

$guardian = new Guardian();

$isCallableRuleGuardian = new IsCallableRuleGuardian($guardian);

try {
    $isCallableRuleGuardian->check($value);
    // $value is callable
} catch (GuardianValidationException $exception) {
    // $value is not callable
}

check() passes when $value is callable, and fails otherwise.

โœ… How the callable check works

A value is considered callable when it can be invoked as a function:

$isCallableRuleGuardian->check(static fn (): string => 'ok'); // passes (closure)
$isCallableRuleGuardian->check('trim');                       // passes (function name)
$isCallableRuleGuardian->check([SomeClass::class, 'method']); // passes (static array callable)
$isCallableRuleGuardian->check([new SomeClass(), 'method']);  // passes (instance array callable)

$isCallableRuleGuardian->check(1);                            // fails (int)
$isCallableRuleGuardian->check('fooo');                       // fails (non-callable string)
$isCallableRuleGuardian->check([]);                           // fails (array)
$isCallableRuleGuardian->check(new stdClass());               // fails (non-invokable object)
$isCallableRuleGuardian->check(['UnknownClass', 'method']);   // fails (invalid array callable)

An object is also callable when it implements the __invoke() magic method.

๐Ÿงฉ Usage with Custom Exception

You may provide your own exception for validation failure. It must be the last argument.

use Aegisora\Guardian\Guardian;
use Aegisora\RuleGuardians\IsCallableRule\IsCallableRuleGuardian;
use App\Exceptions\ValueIsNotCallableException;

$guardian = new Guardian();

$isCallableRuleGuardian = new IsCallableRuleGuardian($guardian);

$isCallableRuleGuardian->check(
    $value,
    new ValueIsNotCallableException()
);

If the value is not callable, the provided exception will be thrown instead of GuardianValidationException.

This is useful when validation errors should have domain-specific meaning.

๐Ÿงช Example in Application Service

use Aegisora\RuleGuardians\IsCallableRule\IsCallableRuleGuardian;
use App\Exceptions\InvalidHandlerException;

final class HandlerRegistry
{
    private IsCallableRuleGuardian $isCallableRuleGuardian;

    public function __construct(
        IsCallableRuleGuardian $isCallableRuleGuardian
    ) {
        $this->isCallableRuleGuardian = $isCallableRuleGuardian;
    }

    /**
     * @param mixed $handler
     */
    public function register(string $name, $handler): void
    {
        $this->isCallableRuleGuardian->check(
            $handler,
            new InvalidHandlerException()
        );

        // business logic for registering a callable handler
    }
}

๐Ÿšจ Exceptions

The package raises validation-related exceptions, all delegated to Guardian (the outcome of running the rule):

GuardianValidationException

Thrown when validation fails and no custom exception is provided.

The rule code for a failed callable check is is_callable_rule.

use Aegisora\Guardian\Exceptions\GuardianValidationException;

try {
    $isCallableRuleGuardian->check($value);
} catch (GuardianValidationException $exception) {
    echo $exception->getRuleCode(); // "is_callable_rule"
}

Custom exception

When a custom exception is passed as the last argument, it is thrown instead of GuardianValidationException on validation failure.

use App\Exceptions\ValueIsNotCallableException;

try {
    $isCallableRuleGuardian->check($value, new ValueIsNotCallableException());
} catch (ValueIsNotCallableException $exception) {
    // domain-specific handling
}

GuardianExecutingRuleException

Thrown when the underlying rule fails to execute (raises a RuleException during validation), as opposed to simply reporting an invalid result.

The callable check accepts any value type and reports non-callable values as an invalid result, so this exception is not triggered by the input itself โ€” it is surfaced only if Guardian fails to execute the rule.

use Aegisora\Guardian\Exceptions\GuardianExecutingRuleException;

try {
    $isCallableRuleGuardian->check($value);
} catch (GuardianExecutingRuleException $exception) {
    // the rule could not be executed
}

๐Ÿงฉ API

IsCallableRuleGuardian::check()

/**
 * @param mixed $value
 * @throws GuardianExecutingRuleException
 * @throws GuardianValidationException
 * @throws \Throwable
 */
public function check($value, ?\Throwable $exception = null): void

Validates that $value is callable.

Arguments:

  • $value โ€” the value to validate
  • $exception โ€” an optional custom \Throwable to be thrown on validation failure

The method returns void. It communicates results through exceptions only โ€” it returns nothing on success and throws on failure:

  • GuardianValidationException โ€” the callable check failed and no custom exception was provided
  • the provided custom exception โ€” the check failed and a custom exception was passed
  • GuardianExecutingRuleException โ€” the rule could not be executed

๐Ÿ›๏ธ Architecture

This package is a small shortcut layer over the Aegisora validation pipeline.

Flow:

  1. IsCallableRuleGuardian::check() is called with a value and an optional exception
  2. An IsCallableRule is created (create())
  3. Guardian executes the rule against the value
  4. If the check passes, execution continues normally
  5. If the check fails, the custom exception or GuardianValidationException is thrown
  6. If the rule could not be executed, GuardianExecutingRuleException is thrown

Internal flow:

value โ†’ IsCallableRuleGuardian โ†’ Guardian โ†’ IsCallableRule โ†’ Result โ†’ Exception

๐Ÿ”— Related Packages

โš–๏ธ License

This package is open-source and licensed under the MIT License. See the LICENSE for details.

๐ŸŒฑ Contributing

Contributions are welcome and greatly appreciated!. See the CONTRIBUTING for details.

๐ŸŒŸ Support

If you find this project useful, please consider giving it a star on GitHub!

It helps the project grow and motivates further development.