aegisora / required-rule-guardian
Required Rule Guardian provides a simple shortcut for ensuring a value is present (not null)
Package info
github.com/Aegisora/required-rule-guardian
Language:Shell
pkg:composer/aegisora/required-rule-guardian
Requires
- php: >=7.4
- aegisora/guardian: ^1.0
- aegisora/required-rule: ^1.0
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^9.6
- squizlabs/php_codesniffer: ^4.0
README
Required Rule Guardian provides a simple shortcut for ensuring a value is present (not null) using aegisora/guardian and aegisora/required-rule.
It is designed for cases where you want to quickly check whether a value is required without manually creating validation pipelines.
This package is built on top of:
โจ Features
- ๐น Simple shortcut API for
RequiredRule - ๐น Validates whether a value is present (not
null) - ๐น Uses
aegisora/guardianinternally - ๐น Uses
aegisora/required-ruleinternally - ๐น Supports custom validation exceptions
- ๐น Fully compatible with the Aegisora ecosystem
- ๐น Ready to use out of the box
๐ฆ Installation
composer require aegisora/required-rule-guardian
๐ Core Concept
This package wraps the common validation flow:
$guardian->check($value, RequiredRule::create(), new InvalidValueException());
into a dedicated shortcut class:
$requiredRuleGuardian->check($value, new InvalidValueException());
Instead of manually creating RequiredRule and passing it to Guardian, you can use RequiredRuleGuardian directly.
๐๏ธ Basic Usage
use Aegisora\Guardian\Exceptions\GuardianValidationException; use Aegisora\Guardian\Guardian; use Aegisora\RuleGuardians\RequiredRule\RequiredRuleGuardian; $guardian = new Guardian(); $requiredRuleGuardian = new RequiredRuleGuardian($guardian); try { $requiredRuleGuardian->check('foo'); // value is present } catch (GuardianValidationException $exception) { // value is null }
๐งฉ Usage with Custom Exception
You may provide your own exception for validation failure.
use Aegisora\Guardian\Guardian; use Aegisora\RuleGuardians\RequiredRule\RequiredRuleGuardian; use App\Exceptions\InvalidValueException; $guardian = new Guardian(); $requiredRuleGuardian = new RequiredRuleGuardian($guardian); $requiredRuleGuardian->check(null, new InvalidValueException());
If the value is null, the provided exception will be thrown.
This is useful when validation errors should have domain-specific meaning.
๐งช Example in Application Service
use Aegisora\RuleGuardians\RequiredRule\RequiredRuleGuardian; use App\Exceptions\InvalidValueException; final class UserService { private RequiredRuleGuardian $requiredRuleGuardian; public function __construct( RequiredRuleGuardian $requiredRuleGuardian ) { $this->requiredRuleGuardian = $requiredRuleGuardian; } /** * @param mixed $value */ public function process($value): void { $this->requiredRuleGuardian->check($value, new InvalidValueException()); // business logic for a present value } }
๐จ 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 { $requiredRuleGuardian->check($value); } catch (GuardianException $exception) { // handles GuardianValidationException and GuardianExecutingRuleException }
GuardianValidationException
Thrown when validation fails and no custom exception is provided.
use Aegisora\Guardian\Exceptions\GuardianValidationException; try { $requiredRuleGuardian->check(null); } catch (GuardianValidationException $exception) { echo $exception->getRuleCode(); // "required_rule" }
GuardianExecutingRuleException
Thrown when the underlying rule execution fails.
Aegisora\Guardian\Exceptions\GuardianExecutingRuleException
๐งฉ API
RequiredRuleGuardian::check()
/** * @param mixed $value */ public function check( $value, ?\Throwable $exception = null ): void
Parameters:
$value(mixed) โ value to validate; considered valid when it is notnull$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 providedGuardianExecutingRuleExceptionโ the underlying rule failed to execute- the provided custom exception โ validation failed and a custom exception was passed
Example:
$requiredRuleGuardian->check('foo');
With custom exception:
$requiredRuleGuardian->check(null, new InvalidValueException());
๐๏ธ Architecture
This package is a small shortcut layer over the Aegisora validation pipeline.
Flow:
RequiredRuleGuardian::check()is calledRequiredRule::create()is createdGuardianexecutes the rule- If validation succeeds, execution continues normally
- If validation fails, custom exception or
GuardianValidationExceptionis thrown - If rule execution fails,
GuardianExecutingRuleExceptionis thrown
Internal flow:
Value โ RequiredRuleGuardian โ Guardian โ RequiredRule โ Result โ Exception
๐ Related Packages
- aegisora/guardian โ validation execution orchestrator
- aegisora/required-rule โ rule-based required value 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.