aegisora/instanceof-rule-guardian
Composer 安装命令:
composer require aegisora/instanceof-rule-guardian
包简介
A simple shortcut for object type validation, built on top of aegisora/guardian and aegisora/instanceof-rule.
关键字:
README 文档
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.
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-06-24