aegisora/numeric-range-rule-guardian

Numeric Range Rule Guardian provides simple shortcuts for ensuring a numeric value falls within an expected range

Maintainers

Package info

github.com/Aegisora/numeric-range-rule-guardian

pkg:composer/aegisora/numeric-range-rule-guardian

Transparency log

Statistics

Installs: 1

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2026-08-30 12:12 UTC

This package is auto-updated.

Last update: 2026-08-30 12:16:07 UTC


README

Latest Version Total Downloads Code Coverage Badge Software License PHPStan Badge

Numeric Range Rule Guardian provides simple shortcuts for ensuring a numeric value falls within an expected range using aegisora/guardian and aegisora/numeric-range-rule.

It is designed for cases where you want to quickly check whether a value is greater than, less than, or between given bounds without manually creating validation pipelines.

This package is built on top of:

โœจ Features

  • ๐Ÿ”น Simple shortcut API for NumericRangeRule
  • ๐Ÿ”น Validates lower bounds (checkGreaterThan, checkGreaterThanOrEqualTo)
  • ๐Ÿ”น Validates upper bounds (checkLessThan, checkLessThanOrEqualTo)
  • ๐Ÿ”น Validates ranges with inclusive/exclusive bounds (checkBetween, checkBetweenExclusive, checkBetweenMinExclusive, checkBetweenMaxExclusive)
  • ๐Ÿ”น Works with integers, floats and numeric strings
  • ๐Ÿ”น Uses aegisora/guardian internally
  • ๐Ÿ”น Uses aegisora/numeric-range-rule internally
  • ๐Ÿ”น Supports custom validation exceptions
  • ๐Ÿ”น Fully compatible with the Aegisora ecosystem
  • ๐Ÿ”น Ready to use out of the box

๐Ÿ“ฆ Installation

composer require aegisora/numeric-range-rule-guardian

๐Ÿš€ Core Concept

This package wraps the common validation flow:

$guardian->check($value, NumericRangeRule::createBetween($min, $max), new InvalidValueException());

into a dedicated shortcut class:

$numericRangeRuleGuardian->checkBetween($value, $min, $max, new InvalidValueException());

Instead of manually creating NumericRangeRule and passing it to Guardian, you can use NumericRangeRuleGuardian directly.

๐Ÿ—๏ธ Basic Usage

use Aegisora\Guardian\Exceptions\GuardianValidationException;
use Aegisora\Guardian\Guardian;
use Aegisora\RuleGuardians\NumericRangeRule\NumericRangeRuleGuardian;

$guardian = new Guardian();

$numericRangeRuleGuardian = new NumericRangeRuleGuardian($guardian);

try {
    $numericRangeRuleGuardian->checkBetween(3, 2, 4);
    // value is within the range
} catch (GuardianValidationException $exception) {
    // value is out of the range
}

๐Ÿงฉ Usage with Custom Exception

You may provide your own exception for validation failure.

use Aegisora\Guardian\Guardian;
use Aegisora\RuleGuardians\NumericRangeRule\NumericRangeRuleGuardian;
use App\Exceptions\InvalidValueException;

$guardian = new Guardian();

$numericRangeRuleGuardian = new NumericRangeRuleGuardian($guardian);

$numericRangeRuleGuardian->checkGreaterThan(1, 3, new InvalidValueException());

If the value is out of the range, the provided exception will be thrown.

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

๐Ÿงช Example in Application Service

use Aegisora\RuleGuardians\NumericRangeRule\NumericRangeRuleGuardian;
use App\Exceptions\InvalidValueException;

final class ProductService
{
    private NumericRangeRuleGuardian $numericRangeRuleGuardian;

    public function __construct(
        NumericRangeRuleGuardian $numericRangeRuleGuardian
    ) {
        $this->numericRangeRuleGuardian = $numericRangeRuleGuardian;
    }

    /**
     * @param numeric $price
     */
    public function process($price): void
    {
        $this->numericRangeRuleGuardian->checkGreaterThan($price, 0, new InvalidValueException());

        // business logic for a value within the expected range
    }
}

๐Ÿšจ Exceptions

This package does not define its own exception types. All errors are raised by the underlying aegisora/guardian package.

Both exceptions extend the abstract base class Aegisora\Guardian\Exceptions\GuardianException, so you can catch every validation error with a single catch:

use Aegisora\Guardian\Exceptions\GuardianException;

try {
    $numericRangeRuleGuardian->checkBetween($value, $min, $max);
} catch (GuardianException $exception) {
    // handles GuardianValidationException and GuardianExecutingRuleException
}

GuardianValidationException

Thrown when validation fails and no custom exception is provided.

use Aegisora\Guardian\Exceptions\GuardianValidationException;

try {
    $numericRangeRuleGuardian->checkGreaterThan(2, 3);
} catch (GuardianValidationException $exception) {
    echo $exception->getRuleCode(); // "numeric_range_rule"
}

GuardianExecutingRuleException

Thrown when the underlying rule execution fails, for example when the checked value is not numeric.

Aegisora\Guardian\Exceptions\GuardianExecutingRuleException

๐Ÿงฉ API

All methods share the same contract: they return void and communicate results through exceptions only โ€” nothing is returned on success and an exception is thrown on failure:

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

Each method also throws Aegisora\RuleContract\Exceptions\InvalidRuleContextException when the range is configured with invalid bounds (a non-numeric bound, $min greater than $max, or equal bounds where at least one side is exclusive).

NumericRangeRuleGuardian::checkGreaterThan()

/**
 * @param mixed $value
 * @param numeric $min
 */
public function checkGreaterThan(
    $value,
    $min,
    ?\Throwable $exception = null
): void

Valid when $value > $min.

NumericRangeRuleGuardian::checkGreaterThanOrEqualTo()

/**
 * @param mixed $value
 * @param numeric $min
 */
public function checkGreaterThanOrEqualTo(
    $value,
    $min,
    ?\Throwable $exception = null
): void

Valid when $value >= $min.

NumericRangeRuleGuardian::checkLessThan()

/**
 * @param mixed $value
 * @param numeric $max
 */
public function checkLessThan(
    $value,
    $max,
    ?\Throwable $exception = null
): void

Valid when $value < $max.

NumericRangeRuleGuardian::checkLessThanOrEqualTo()

/**
 * @param mixed $value
 * @param numeric $max
 */
public function checkLessThanOrEqualTo(
    $value,
    $max,
    ?\Throwable $exception = null
): void

Valid when $value <= $max.

NumericRangeRuleGuardian::checkBetween()

/**
 * @param mixed $value
 * @param numeric $min
 * @param numeric $max
 */
public function checkBetween(
    $value,
    $min,
    $max,
    ?\Throwable $exception = null
): void

Valid when $min <= $value <= $max (both bounds inclusive).

NumericRangeRuleGuardian::checkBetweenExclusive()

/**
 * @param mixed $value
 * @param numeric $min
 * @param numeric $max
 */
public function checkBetweenExclusive(
    $value,
    $min,
    $max,
    ?\Throwable $exception = null
): void

Valid when $min < $value < $max (both bounds exclusive).

NumericRangeRuleGuardian::checkBetweenMinExclusive()

/**
 * @param mixed $value
 * @param numeric $min
 * @param numeric $max
 */
public function checkBetweenMinExclusive(
    $value,
    $min,
    $max,
    ?\Throwable $exception = null
): void

Valid when $min < $value <= $max (lower bound exclusive, upper bound inclusive).

NumericRangeRuleGuardian::checkBetweenMaxExclusive()

/**
 * @param mixed $value
 * @param numeric $min
 * @param numeric $max
 */
public function checkBetweenMaxExclusive(
    $value,
    $min,
    $max,
    ?\Throwable $exception = null
): void

Valid when $min <= $value < $max (lower bound inclusive, upper bound exclusive).

Common parameters:

  • $value (mixed) โ€” value to validate; considered valid when it is numeric and satisfies the configured range
  • $min / $max (numeric) โ€” range bounds
  • $exception (?\Throwable, default null) โ€” optional custom exception thrown on validation failure

Example:

$numericRangeRuleGuardian->checkBetween(3, 2, 4);

With custom exception:

$numericRangeRuleGuardian->checkBetween(5, 2, 4, new InvalidValueException());

๐Ÿ›๏ธ Architecture

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

Flow:

  1. A NumericRangeRuleGuardian::check*() method is called
  2. The matching NumericRangeRule::create*() rule is created
  3. Guardian executes the rule
  4. If validation succeeds, execution continues normally
  5. If validation fails, custom exception or GuardianValidationException is thrown
  6. If rule execution fails, GuardianExecutingRuleException is thrown

Internal flow:

Value โ†’ NumericRangeRuleGuardian โ†’ Guardian โ†’ NumericRangeRule โ†’ 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.