PHP code example of aegisora / boolean-rule-guardian

1. Go to this page and download the library: Download aegisora/boolean-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 / boolean-rule-guardian example snippets


$guardian->check($value, BooleanRule::createTruthy(), new NotTruthyException());

$booleanRuleGuardian->checkTruthy($value, new NotTruthyException());

use Aegisora\Guardian\Guardian;
use Aegisora\Guardian\Exceptions\GuardianValidationException;
use Aegisora\RuleGuardians\BooleanRule\BooleanRuleGuardian;

$guardian = new Guardian();

$booleanRuleGuardian = new BooleanRuleGuardian($guardian);

try {
    $booleanRuleGuardian->checkTruthy(true);
    // value is strictly true
} catch (GuardianValidationException $exception) {
    // value is not true
}

$booleanRuleGuardian->checkTruthy(true);  // passes
$booleanRuleGuardian->checkTruthy(false); // fails

$booleanRuleGuardian->checkFalsy(false); // passes
$booleanRuleGuardian->checkFalsy(true);  // fails

use Aegisora\Guardian\Guardian;
use Aegisora\RuleGuardians\BooleanRule\BooleanRuleGuardian;
use App\Exceptions\NotTruthyException;

$guardian = new Guardian();

$booleanRuleGuardian = new BooleanRuleGuardian($guardian);

$booleanRuleGuardian->checkTruthy(false, new NotTruthyException());

use Aegisora\RuleGuardians\BooleanRule\BooleanRuleGuardian;
use App\Exceptions\ConsentRequiredException;

final class RegistrationService
{
    private BooleanRuleGuardian $booleanRuleGuardian;

    public function __construct(
        BooleanRuleGuardian $booleanRuleGuardian
    ) {
        $this->booleanRuleGuardian = $booleanRuleGuardian;
    }

    public function register(bool $termsAccepted): void
    {
        $this->booleanRuleGuardian->checkTruthy($termsAccepted, new ConsentRequiredException());

        // business logic for an accepted agreement
    }
}

use Aegisora\Guardian\Exceptions\GuardianValidationException;

try {
    $booleanRuleGuardian->checkTruthy(false);
} catch (GuardianValidationException $exception) {
    echo $exception->getRuleCode(); // "boolean_rule"
}

use App\Exceptions\NotTruthyException;

try {
    $booleanRuleGuardian->checkTruthy(false, new NotTruthyException());
} catch (NotTruthyException $exception) {
    // domain-specific handling
}

use Aegisora\Guardian\Exceptions\GuardianExecutingRuleException;

try {
    $booleanRuleGuardian->checkTruthy(1); // not a real boolean
} catch (GuardianExecutingRuleException $exception) {
    // the rule could not be executed
}

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

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

$booleanRuleGuardian->checkTruthy(true);

$booleanRuleGuardian->checkFalsy(true, new NotFalsyException());