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