aegisora/email-rule-guardian

A simple shortcut for email validation, built on top of aegisora/guardian and aegisora/email-rule.

Maintainers

Package info

github.com/Aegisora/email-rule-guardian

pkg:composer/aegisora/email-rule-guardian

Statistics

Installs: 3

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2026-06-29 16:40 UTC

This package is auto-updated.

Last update: 2026-06-29 17:02:30 UTC


README

Latest Version Total Downloads Code Coverage Badge Software License PHPStan Badge

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

It is designed for cases where you want to quickly check whether a value is a valid email address without manually creating validation pipelines.

This package is built on top of:

โœจ Features

  • ๐Ÿ”น Simple shortcut API for EmailRule
  • ๐Ÿ”น Validates whether a value is a valid email address
  • ๐Ÿ”น Uses aegisora/guardian internally
  • ๐Ÿ”น Uses aegisora/email-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/email-rule-guardian

๐Ÿš€ Core Concept

This package wraps the common validation flow:

$guardian->check($value, EmailRule::create(), new InvalidEmailException());

into a dedicated shortcut class:

$emailRuleGuardian->check($value, new InvalidEmailException());

Instead of manually creating EmailRule and passing it to Guardian, you can use EmailRuleGuardian directly.

๐Ÿ—๏ธ Basic Usage

use Aegisora\Guardian\Guardian;
use Aegisora\Guardian\Exceptions\GuardianValidationException;
use Aegisora\RuleGuardians\EmailRule\EmailRuleGuardian;

$guardian = new Guardian();

$emailRuleGuardian = new EmailRuleGuardian($guardian);

try {
    $emailRuleGuardian->check('test@example.com');
    // value is a valid email
} catch (GuardianValidationException $exception) {
    // value is not a valid email
}

๐Ÿงฉ Usage with Custom Exception

You may provide your own exception for validation failure.

use Aegisora\Guardian\Guardian;
use Aegisora\RuleGuardians\EmailRule\EmailRuleGuardian;
use App\Exceptions\InvalidEmailException;

$guardian = new Guardian();

$emailRuleGuardian = new EmailRuleGuardian($guardian);

$emailRuleGuardian->check('plainaddress', new InvalidEmailException());

If the value is not a valid email, the provided exception will be thrown.

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

๐Ÿงช Example in Application Service

use Aegisora\RuleGuardians\EmailRule\EmailRuleGuardian;
use App\Exceptions\InvalidEmailException;

final class RegistrationService
{
    private EmailRuleGuardian $emailRuleGuardian;

    public function __construct(
        EmailRuleGuardian $emailRuleGuardian
    ) {
        $this->emailRuleGuardian = $emailRuleGuardian;
    }

    /**
     * @param mixed $email
     */
    public function register($email): void
    {
        $this->emailRuleGuardian->check($email, new InvalidEmailException());

        // business logic for valid email
    }
}

๐Ÿšจ 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 email validation is email_rule.

use Aegisora\Guardian\Exceptions\GuardianValidationException;

try {
    $emailRuleGuardian->check('plainaddress');
} catch (GuardianValidationException $exception) {
    echo $exception->getRuleCode(); // "email_rule"
}

Custom exception

When a custom exception is passed as the second argument, it is thrown instead of GuardianValidationException on validation failure.

use App\Exceptions\InvalidEmailException;

try {
    $emailRuleGuardian->check('plainaddress', new InvalidEmailException());
} catch (InvalidEmailException $exception) {
    // domain-specific handling
}

GuardianExecutingRuleException

Thrown when the underlying rule execution fails.

use Aegisora\Guardian\Exceptions\GuardianExecutingRuleException;

try {
    $emailRuleGuardian->check($value);
} catch (GuardianExecutingRuleException $exception) {
    // the rule could not be executed
}

๐Ÿงฉ API

EmailRuleGuardian::check()

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

Parameters:

  • $value (mixed) โ€” value to validate as an email address
  • $exception (?\Throwable, default null) โ€” optional custom exception thrown on validation failure

Returns void. The method communicates results through exceptions only โ€” it returns nothing on success and throws 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

Example:

$emailRuleGuardian->check('test@example.com');

With custom exception:

$emailRuleGuardian->check('test@example.com', new InvalidEmailException());

๐Ÿ›๏ธ Architecture

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

Flow:

  1. EmailRuleGuardian::check() is called
  2. EmailRule::create() 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 โ†’ EmailRuleGuardian โ†’ Guardian โ†’ EmailRule โ†’ 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.