aegisora / email-rule-guardian
A simple shortcut for email validation, built on top of aegisora/guardian and aegisora/email-rule.
Requires
- php: >=7.4
- aegisora/email-rule: ^1.0
- aegisora/guardian: ^1.0
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^9.6
- squizlabs/php_codesniffer: ^4.0
README
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/guardianinternally - ๐น Uses
aegisora/email-ruleinternally - ๐น 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, defaultnull) โ 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:
EmailRuleGuardian::check()is calledEmailRule::create()is createdGuardianexecutes the rule- If validation succeeds, execution continues normally
- If validation fails, the custom exception or
GuardianValidationExceptionis thrown - If rule execution fails,
GuardianExecutingRuleExceptionis thrown
Internal flow:
Value โ EmailRuleGuardian โ Guardian โ EmailRule โ Result โ Exception
๐ Related Packages
- aegisora/guardian โ validation execution orchestrator
- aegisora/email-rule โ rule-based email validation
- aegisora/rule-contract โ base rule contract and validation result architecture
โ๏ธ 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.