aegisora / instanceof-rule-guardian
A simple shortcut for object type validation, built on top of aegisora/guardian and aegisora/instanceof-rule.
Package info
github.com/Aegisora/instanceof-rule-guardian
pkg:composer/aegisora/instanceof-rule-guardian
Requires
- php: >=7.4
- aegisora/guardian: ^1.0
- aegisora/instanceof-rule: ^1.0
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^9.6
- squizlabs/php_codesniffer: ^4.0
This package is auto-updated.
Last update: 2026-06-24 16:15:57 UTC
README
Instanceof Rule Guardian provides a simple shortcut for object type validation using aegisora/guardian and aegisora/instanceof-rule.
It is designed for cases where you want to quickly check whether a value is an instance of a given class without manually creating validation pipelines.
This package is built on top of:
โจ Features
- ๐น Simple shortcut API for
InstanceofRule - ๐น Validates whether a value is an instance of a given class
- ๐น Uses
aegisora/guardianinternally - ๐น Uses
aegisora/instanceof-ruleinternally - ๐น Supports custom validation exceptions
- ๐น Provides package-specific exception types
- ๐น 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/instanceof-rule-guardian
๐ Core Concept
This package wraps the common validation flow:
$guardian->check($value, InstanceofRule::create(User::class), new InvalidUserObjectException());
into a dedicated shortcut class:
$instanceofRuleGuardian->check($value, User::class, new InvalidUserObjectException());
Instead of manually creating InstanceofRule and passing it to Guardian, you can use InstanceofRuleGuardian directly.
๐๏ธ Basic Usage
use Aegisora\Guardian\Guardian; use Aegisora\RuleGuardians\InstanceofRule\InstanceofRuleGuardian; use Aegisora\RuleGuardians\InstanceofRule\Exceptions\NotInstanceofException; class User {} $guardian = new Guardian(); $instanceofRuleGuardian = new InstanceofRuleGuardian($guardian); try { $instanceofRuleGuardian->check(new User(), User::class); // value is instance of User } catch (NotInstanceofException $exception) { // value is not instance of User }
๐งฉ Usage with Custom Exception
You may provide your own exception for validation failure.
use Aegisora\Guardian\Guardian; use Aegisora\RuleGuardians\InstanceofRule\InstanceofRuleGuardian; use App\Exceptions\InvalidUserObjectException; class User {} $guardian = new Guardian(); $instanceofRuleGuardian = new InstanceofRuleGuardian($guardian); $instanceofRuleGuardian->check(new \stdClass(), User::class, new InvalidUserObjectException());
If the value is not an instance of User, the provided exception will be thrown.
This is useful when validation errors should have domain-specific meaning.
๐งช Example in Application Service
use Aegisora\RuleGuardians\InstanceofRule\InstanceofRuleGuardian; use App\Exceptions\InvalidUserObjectException; class User {} final class UserService { private InstanceofRuleGuardian $instanceofRuleGuardian; public function __construct( InstanceofRuleGuardian $instanceofRuleGuardian ) { $this->instanceofRuleGuardian = $instanceofRuleGuardian; } /** * @param mixed $value */ public function process($value): void { $this->instanceofRuleGuardian->check($value, User::class, new InvalidUserObjectException()); // business logic for valid User object } }
๐จ Exceptions
This package provides dedicated exceptions for predictable error handling.
All exceptions extend the abstract base class
Aegisora\RuleGuardians\InstanceofRule\Exceptions\InstanceofRuleGuardianException,
so you can catch every error raised by this package with a single catch:
use Aegisora\RuleGuardians\InstanceofRule\Exceptions\InstanceofRuleGuardianException; try { $instanceofRuleGuardian->check($value, User::class); } catch (InstanceofRuleGuardianException $exception) { // handles NotInstanceofException, ExecutingRuleException and ExecutingCheckException }
NotInstanceofException
Thrown when validation fails and no custom exception is provided.
use Aegisora\RuleGuardians\InstanceofRule\Exceptions\NotInstanceofException; try { $instanceofRuleGuardian->check(new \stdClass(), User::class); } catch (NotInstanceofException $exception) { echo $exception->getRuleCode(); }
ExecutingRuleException
Thrown when the underlying rule execution fails.
Internally, Guardian converts rule execution failures into GuardianExecutingRuleException. This package wraps it into:
Aegisora\RuleGuardians\InstanceofRule\Exceptions\ExecutingRuleException
ExecutingCheckException
Thrown when an unexpected error occurs during the shortcut check process.
Aegisora\RuleGuardians\InstanceofRule\Exceptions\ExecutingCheckException
๐งฉ API
InstanceofRuleGuardian::check()
/** * @param mixed $value */ public function check( $value, string $targetClassName, ?\Throwable $exception = null ): void
Parameters:
$value(mixed) โ value to validate$targetClassName(string) โ fully qualified class name to check against$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:
NotInstanceofExceptionโ validation failed and no custom exception was providedExecutingRuleExceptionโ the underlying rule failed to executeExecutingCheckExceptionโ an unexpected error occurred during the check
Example:
$instanceofRuleGuardian->check($value, User::class);
With custom exception:
$instanceofRuleGuardian->check($value, User::class, new InvalidUserObjectException());
๐๏ธ Architecture
This package is a small shortcut layer over the Aegisora validation pipeline.
Flow:
InstanceofRuleGuardian::check()is calledInstanceofRule::create($targetClassName)is createdGuardianexecutes the rule- If validation succeeds, execution continues normally
- If validation fails, custom exception or
NotInstanceofExceptionis thrown - If rule execution fails,
ExecutingRuleExceptionis thrown
Internal flow:
Value โ InstanceofRuleGuardian โ Guardian โ InstanceofRule โ Result โ Exception
๐ Related Packages
- aegisora/guardian โ validation execution orchestrator
- aegisora/instanceof-rule โ rule-based object type 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.