PHP code example of aegisora / is-callable-rule-guardian

1. Go to this page and download the library: Download aegisora/is-callable-rule-guardian library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

aegisora / is-callable-rule-guardian example snippets


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

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

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
}

$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)

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()
);

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
    }
}

use Aegisora\Guardian\Exceptions\GuardianValidationException;

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

use App\Exceptions\ValueIsNotCallableException;

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

use Aegisora\Guardian\Exceptions\GuardianExecutingRuleException;

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

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