aegisora / scalar-equality-rule-guardian
Scalar Equality Rule Guardian provides a simple shortcut for scalar equality validation using aegisora/guardian and aegisora/scalar-equality-rule
Package info
github.com/Aegisora/scalar-equality-rule-guardian
pkg:composer/aegisora/scalar-equality-rule-guardian
Requires
- php: >=7.4
- aegisora/guardian: ^1.0
- aegisora/scalar-equality-rule: ^1.0
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^9.6
- squizlabs/php_codesniffer: ^4.0
README
Scalar Equality Rule Guardian provides a simple shortcut for scalar equality validation using aegisora/guardian and aegisora/scalar-equality-rule.
It is designed for cases where you want to quickly check whether a scalar value is (or is not) strictly equal to an expected scalar value, without manually building a ScalarEqualityRule and a validation pipeline by hand.
This package is built on top of:
โจ Features
- ๐น Simple shortcut API for
ScalarEqualityRule - ๐น Validates strict equality (
===) viacheckEqual() - ๐น Validates strict inequality (
!==) viacheckNotEqual() - ๐น Works with any scalar value (
int,float,string,bool) andnull - ๐น Uses
aegisora/guardianinternally - ๐น Uses
aegisora/scalar-equality-ruleinternally - ๐น 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/scalar-equality-rule-guardian
๐ Core Concept
This package wraps the common scalar equality validation flow:
$guardian->check( $value, ScalarEqualityRule::createEqual($expectedValue), new ValuesNotEqualException() );
into a dedicated shortcut class:
$scalarEqualityRuleGuardian->checkEqual($value, $expectedValue, new ValuesNotEqualException());
Instead of manually creating a ScalarEqualityRule and passing it to Guardian, you can use ScalarEqualityRuleGuardian directly.
๐๏ธ Basic Usage
use Aegisora\Guardian\Guardian; use Aegisora\Guardian\Exceptions\GuardianValidationException; use Aegisora\RuleGuardians\ScalarEqualityRule\ScalarEqualityRuleGuardian; $guardian = new Guardian(); $scalarEqualityRuleGuardian = new ScalarEqualityRuleGuardian($guardian); try { $scalarEqualityRuleGuardian->checkEqual($value, $expectedValue); // $value is strictly equal to $expectedValue } catch (GuardianValidationException $exception) { // $value is not equal to $expectedValue }
checkEqual() passes when $value === $expectedValue, and fails otherwise.
checkNotEqual() is the exact opposite โ it passes when $value !== $expectedValue.
โ How scalar equality works
Comparison is strict (===), so both type and value must match:
$scalarEqualityRuleGuardian->checkEqual(1, 1); // passes $scalarEqualityRuleGuardian->checkEqual(1, '1'); // fails (int vs string) $scalarEqualityRuleGuardian->checkEqual(1.0, 1); // fails (float vs int) $scalarEqualityRuleGuardian->checkEqual(null, null); // passes
And the inverse for checkNotEqual():
$scalarEqualityRuleGuardian->checkNotEqual(1, '1'); // passes $scalarEqualityRuleGuardian->checkNotEqual(1, 1); // fails
Both the value and the expected value must be scalar or null. Passing an array, object, resource or callable cannot be executed as a scalar comparison and throws a rule execution exception.
๐งฉ 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\ScalarEqualityRule\ScalarEqualityRuleGuardian; use App\Exceptions\ValuesNotEqualException; $guardian = new Guardian(); $scalarEqualityRuleGuardian = new ScalarEqualityRuleGuardian($guardian); $scalarEqualityRuleGuardian->checkEqual( $value, $expectedValue, new ValuesNotEqualException() );
If the values are not equal, 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\ScalarEqualityRule\ScalarEqualityRuleGuardian; use App\Exceptions\InvalidConfirmationTokenException; final class ConfirmationService { private ScalarEqualityRuleGuardian $scalarEqualityRuleGuardian; public function __construct( ScalarEqualityRuleGuardian $scalarEqualityRuleGuardian ) { $this->scalarEqualityRuleGuardian = $scalarEqualityRuleGuardian; } public function confirm(string $providedToken, string $expectedToken): void { $this->scalarEqualityRuleGuardian->checkEqual( $providedToken, $expectedToken, new InvalidConfirmationTokenException() ); // business logic for a confirmed action } }
๐จ Exceptions
The package raises two kinds of validation-related exceptions, both 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 scalar equality check is scalar_equality_rule.
use Aegisora\Guardian\Exceptions\GuardianValidationException; try { $scalarEqualityRuleGuardian->checkEqual($value, $expectedValue); } catch (GuardianValidationException $exception) { echo $exception->getRuleCode(); // "scalar_equality_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\ValuesNotEqualException; try { $scalarEqualityRuleGuardian->checkEqual($value, $expectedValue, new ValuesNotEqualException()); } catch (ValuesNotEqualException $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.
This happens when the value or the expected value is neither scalar nor null (e.g. an array, object, resource or callable).
use Aegisora\Guardian\Exceptions\GuardianExecutingRuleException; try { $scalarEqualityRuleGuardian->checkEqual([], 1); } catch (GuardianExecutingRuleException $exception) { // the rule could not be executed }
๐งฉ API
ScalarEqualityRuleGuardian::checkEqual()
/** * @param mixed $value * @param mixed $expectedValue * @throws GuardianExecutingRuleException * @throws GuardianValidationException * @throws \Throwable */ public function checkEqual($value, $expectedValue, ?\Throwable $exception = null): void
Validates that $value is strictly equal (===) to $expectedValue.
ScalarEqualityRuleGuardian::checkNotEqual()
/** * @param mixed $value * @param mixed $expectedValue * @throws GuardianExecutingRuleException * @throws GuardianValidationException * @throws \Throwable */ public function checkNotEqual($value, $expectedValue, ?\Throwable $exception = null): void
Validates that $value is strictly not equal (!==) to $expectedValue.
Arguments (both methods):
$valueโ the scalar (ornull) value to validate$expectedValueโ the scalar (ornull) value to compare against$exceptionโ an optional custom\Throwableto be thrown on validation failure
Both methods return void. They communicate results through exceptions only โ they return nothing on success and throw on failure:
GuardianValidationExceptionโ the equality/inequality check failed and no custom exception was provided- the provided custom exception โ the check failed and a custom exception was passed
GuardianExecutingRuleExceptionโ the value or expected value is not scalar (and notnull), so the rule could not be executed
๐๏ธ Architecture
This package is a small shortcut layer over the Aegisora validation pipeline.
Flow:
ScalarEqualityRuleGuardian::checkEqual()/checkNotEqual()is called with a value, an expected value and an optional exception- A
ScalarEqualityRuleis created (createEqual()orcreateNotEqual()) Guardianexecutes the rule against the value- If the check passes, execution continues normally
- If the check fails, the custom exception or
GuardianValidationExceptionis thrown - If the value is not scalar (and not
null),GuardianExecutingRuleExceptionis thrown
Internal flow:
value + expectedValue โ ScalarEqualityRuleGuardian โ Guardian โ ScalarEqualityRule โ Result โ Exception
๐ Related Packages
- aegisora/guardian โ validation execution orchestrator
- aegisora/scalar-equality-rule โ scalar equality rule
- 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.