aegisora/boolean-rule-guardian

A simple shortcut for boolean value validation using aegisora/guardian and aegisora/boolean-rule.

Maintainers

Package info

github.com/Aegisora/boolean-rule-guardian

Language:Shell

pkg:composer/aegisora/boolean-rule-guardian

Transparency log

Statistics

Installs: 1

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2026-07-31 15:19 UTC

This package is auto-updated.

Last update: 2026-07-31 15:21:50 UTC


README

Latest Version Total Downloads Code Coverage Badge Software License PHPStan Badge

Boolean Rule Guardian provides a simple shortcut for boolean value validation using aegisora/guardian and aegisora/boolean-rule.

It is designed for cases where you want to quickly check whether a value is strictly true or strictly false without manually creating validation pipelines.

This package is built on top of:

โœจ Features

  • ๐Ÿ”น Simple shortcut API for BooleanRule
  • ๐Ÿ”น Validates whether a value is strictly true or strictly false
  • ๐Ÿ”น Uses strict comparison (===) โ€” no type juggling
  • ๐Ÿ”น Uses aegisora/guardian internally
  • ๐Ÿ”น Uses aegisora/boolean-rule internally
  • ๐Ÿ”น Supports custom validation exceptions
  • ๐Ÿ”น 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/boolean-rule-guardian

๐Ÿš€ Core Concept

This package wraps the common validation flow:

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

into a dedicated shortcut class:

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

Instead of manually creating BooleanRule and passing it to Guardian, you can use BooleanRuleGuardian directly.

๐Ÿ—๏ธ Basic Usage

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
}

โœ… Truthy vs Falsy

The package exposes two methods that differ only in the expected boolean value.

checkTruthy()

Passes when the value is strictly true.

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

checkFalsy()

Passes when the value is strictly false.

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

Both methods use strict comparison (===). No type juggling is performed โ€” only real booleans are accepted as valid input (see Exceptions).

๐Ÿงฉ Usage with Custom Exception

You may provide your own exception for validation failure.

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

If the value is not true, the provided exception will be thrown.

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

๐Ÿงช Example in Application Service

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

๐Ÿšจ Exceptions

This package does not define its own exception types. It delegates execution to Guardian and re-throws the exceptions raised by the underlying pipeline.

GuardianValidationException

Thrown when validation fails and no custom exception is provided.

The rule code for failed boolean validation is boolean_rule.

use Aegisora\Guardian\Exceptions\GuardianValidationException;

try {
    $booleanRuleGuardian->checkTruthy(false);
} catch (GuardianValidationException $exception) {
    echo $exception->getRuleCode(); // "boolean_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\NotTruthyException;

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

GuardianExecutingRuleException

Thrown when the underlying rule execution fails.

This happens, for example, when the value is not a real boolean (e.g. 1, 'true', '', [], null). The rule only accepts true or false; any other type is treated as an execution error, not a validation failure.

use Aegisora\Guardian\Exceptions\GuardianExecutingRuleException;

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

๐Ÿงฉ API

BooleanRuleGuardian::checkTruthy()

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

Validates that $value is strictly true (=== true).

BooleanRuleGuardian::checkFalsy()

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

Validates that $value is strictly false (=== false).

Parameters (both methods):

  • $value (mixed) โ€” value to validate; must be a real boolean
  • $exception (?\Throwable, default null) โ€” optional custom exception thrown on validation failure

Both methods return void. They communicate results through exceptions only โ€” they return nothing on success and throw on failure:

  • GuardianValidationException โ€” validation failed and no custom exception was provided
  • the provided custom exception โ€” validation failed and a custom exception was passed
  • GuardianExecutingRuleException โ€” the underlying rule failed to execute (e.g. the value is not a real boolean)

Example:

$booleanRuleGuardian->checkTruthy(true);

With custom exception:

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

๐Ÿ›๏ธ Architecture

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

Flow:

  1. BooleanRuleGuardian::checkTruthy() / checkFalsy() is called
  2. BooleanRule::createTruthy() / BooleanRule::createFalsy() is created
  3. Guardian executes the rule
  4. If validation succeeds, execution continues normally
  5. If validation fails, the custom exception or GuardianValidationException is thrown
  6. If rule execution fails, GuardianExecutingRuleException is thrown

Internal flow:

Value โ†’ BooleanRuleGuardian โ†’ Guardian โ†’ BooleanRule โ†’ 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.