aegisora/emptiness-rule-guardian

Emptiness Rule Guardian provides a simple shortcut for emptiness validation using aegisora/guardian and aegisora/emptiness-rule

Maintainers

Package info

github.com/Aegisora/emptiness-rule-guardian

Language:Shell

pkg:composer/aegisora/emptiness-rule-guardian

Transparency log

Statistics

Installs: 1

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2026-08-21 15:09 UTC

This package is auto-updated.

Last update: 2026-08-21 15:12:57 UTC


README

Latest Version Total Downloads Code Coverage Badge Software License PHPStan Badge

Emptiness Rule Guardian provides a simple shortcut for emptiness validation using aegisora/guardian and aegisora/emptiness-rule.

It is designed for cases where you want to quickly check whether a value is empty or is not empty, without manually building an EmptyRule / NotEmptyRule and a validation pipeline by hand.

This package is built on top of:

โœจ Features

  • ๐Ÿ”น Simple shortcut API for EmptyRule and NotEmptyRule
  • ๐Ÿ”น Validates that a value is empty via checkEmpty()
  • ๐Ÿ”น Validates that a value is not empty via checkNotEmpty()
  • ๐Ÿ”น Works with scalars, arrays and countable objects
  • ๐Ÿ”น Uses aegisora/guardian internally
  • ๐Ÿ”น Uses aegisora/emptiness-rule internally
  • ๐Ÿ”น Supports a custom validation exception
  • ๐Ÿ”น 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/emptiness-rule-guardian

๐Ÿš€ Core Concept

This package wraps the common emptiness validation flow:

$guardian->check(
    $value,
    EmptyRule::create(),
    new ValueIsNotEmptyException()
);

$guardian->check(
    $value,
    NotEmptyRule::create(),
    new ValueIsEmptyException()
);

into a dedicated shortcut class:

$emptinessRuleGuardian->checkEmpty($value, new ValueIsNotEmptyException());
$emptinessRuleGuardian->checkNotEmpty($value, new ValueIsEmptyException());

Instead of manually creating an EmptyRule / NotEmptyRule and passing it to Guardian, you can use EmptinessRuleGuardian directly.

๐Ÿ—๏ธ Basic Usage

use Aegisora\Guardian\Guardian;
use Aegisora\Guardian\Exceptions\GuardianValidationException;
use Aegisora\RuleGuardians\EmptinessRuleGuardian\EmptinessRuleGuardian;

$guardian = new Guardian();

$emptinessRuleGuardian = new EmptinessRuleGuardian($guardian);

try {
    $emptinessRuleGuardian->checkEmpty($value);
    // $value is empty
} catch (GuardianValidationException $exception) {
    // $value is not empty
}

try {
    $emptinessRuleGuardian->checkNotEmpty($value);
    // $value is not empty
} catch (GuardianValidationException $exception) {
    // $value is empty
}

checkEmpty() passes when $value is empty, and fails otherwise.

checkNotEmpty() passes when $value is not empty, and fails otherwise.

โœ… How the emptiness check works

A value is considered empty when it is null, an empty string, an empty array or an empty countable object. Every other value โ€” including 0, 0.0, false and non-countable objects โ€” is considered not empty:

$emptinessRuleGuardian->checkEmpty(null);                // passes (null)
$emptinessRuleGuardian->checkEmpty('');                  // passes (empty string)
$emptinessRuleGuardian->checkEmpty([]);                  // passes (empty array)
$emptinessRuleGuardian->checkEmpty(new ArrayObject([])); // passes (empty countable object)

$emptinessRuleGuardian->checkEmpty('foo');               // fails (non-empty string)
$emptinessRuleGuardian->checkEmpty([1]);                 // fails (non-empty array)
$emptinessRuleGuardian->checkEmpty(0);                   // fails (int)
$emptinessRuleGuardian->checkEmpty(0.0);                 // fails (float)
$emptinessRuleGuardian->checkEmpty(false);               // fails (bool)
$emptinessRuleGuardian->checkEmpty(new stdClass());      // fails (non-countable object)
$emptinessRuleGuardian->checkEmpty(new ArrayObject([1]));// fails (non-empty countable object)
$emptinessRuleGuardian->checkEmpty(static fn () => null);// fails (callable)

checkNotEmpty() is the exact inverse โ€” it passes for every value listed above as fails and fails for every value listed as passes.

โš ๏ธ A resource cannot be evaluated for emptiness. Passing a resource raises a GuardianExecutingRuleException (see below) instead of a validation result.

๐Ÿงฉ Usage with Custom Exception

You may provide your own exception for validation failure. It must be the last argument.

use Aegisora\Guardian\Guardian;
use Aegisora\RuleGuardians\EmptinessRuleGuardian\EmptinessRuleGuardian;
use App\Exceptions\ValueIsEmptyException;

$guardian = new Guardian();

$emptinessRuleGuardian = new EmptinessRuleGuardian($guardian);

$emptinessRuleGuardian->checkNotEmpty(
    $value,
    new ValueIsEmptyException()
);

If the value is empty, the provided exception will be thrown instead of GuardianValidationException.

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

๐Ÿงช Example in Application Service

use Aegisora\RuleGuardians\EmptinessRuleGuardian\EmptinessRuleGuardian;
use App\Exceptions\EmptyPayloadException;

final class PayloadProcessor
{
    private EmptinessRuleGuardian $emptinessRuleGuardian;

    public function __construct(
        EmptinessRuleGuardian $emptinessRuleGuardian
    ) {
        $this->emptinessRuleGuardian = $emptinessRuleGuardian;
    }

    /**
     * @param mixed $payload
     */
    public function process($payload): void
    {
        $this->emptinessRuleGuardian->checkNotEmpty(
            $payload,
            new EmptyPayloadException()
        );

        // business logic for processing a non-empty payload
    }
}

๐Ÿšจ Exceptions

The package raises validation-related exceptions, all delegated to Guardian (the outcome of running the rule):

GuardianValidationException

Thrown when validation fails and no custom exception is provided.

The rule code for a failed checkEmpty() is empty_rule, and for a failed checkNotEmpty() it is not_empty_rule.

use Aegisora\Guardian\Exceptions\GuardianValidationException;

try {
    $emptinessRuleGuardian->checkEmpty($value);
} catch (GuardianValidationException $exception) {
    echo $exception->getRuleCode(); // "empty_rule"
}

try {
    $emptinessRuleGuardian->checkNotEmpty($value);
} catch (GuardianValidationException $exception) {
    echo $exception->getRuleCode(); // "not_empty_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\ValueIsEmptyException;

try {
    $emptinessRuleGuardian->checkNotEmpty($value, new ValueIsEmptyException());
} catch (ValueIsEmptyException $exception) {
    // domain-specific handling
}

GuardianExecutingRuleException

Thrown when the underlying rule fails to execute (raises a RuleException during validation), as opposed to simply reporting an invalid result.

Emptiness cannot be determined for a resource, so passing a resource surfaces this exception instead of a validation result:

use Aegisora\Guardian\Exceptions\GuardianExecutingRuleException;

try {
    $emptinessRuleGuardian->checkEmpty(tmpfile());
} catch (GuardianExecutingRuleException $exception) {
    // the rule could not be executed
}

๐Ÿงฉ API

EmptinessRuleGuardian::checkEmpty()

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

Validates that $value is empty.

EmptinessRuleGuardian::checkNotEmpty()

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

Validates that $value is not empty.

Arguments (both methods):

  • $value โ€” the value to validate
  • $exception โ€” an optional custom \Throwable to be thrown on validation failure

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

  • GuardianValidationException โ€” the emptiness check failed and no custom exception was provided
  • the provided custom exception โ€” the check failed and a custom exception was passed
  • GuardianExecutingRuleException โ€” the rule could not be executed

๐Ÿ›๏ธ Architecture

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

Flow:

  1. EmptinessRuleGuardian::checkEmpty() / checkNotEmpty() is called with a value and an optional exception
  2. An EmptyRule / NotEmptyRule is created (create())
  3. Guardian executes the rule against the value
  4. If the check passes, execution continues normally
  5. If the check fails, the custom exception or GuardianValidationException is thrown
  6. If the rule could not be executed, GuardianExecutingRuleException is thrown

Internal flow:

value โ†’ EmptinessRuleGuardian โ†’ Guardian โ†’ EmptyRule / NotEmptyRule โ†’ 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.