aegisora/instanceof-rule-guardian

A simple shortcut for object type validation, built on top of aegisora/guardian and aegisora/instanceof-rule.

Maintainers

Package info

github.com/Aegisora/instanceof-rule-guardian

pkg:composer/aegisora/instanceof-rule-guardian

Statistics

Installs: 1

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2026-06-24 15:59 UTC

This package is auto-updated.

Last update: 2026-06-24 16:15:57 UTC


README

Latest Version Total Downloads Code Coverage Badge Software License PHPStan Badge

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/guardian internally
  • ๐Ÿ”น Uses aegisora/instanceof-rule internally
  • ๐Ÿ”น 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, default null) โ€” 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 provided
  • ExecutingRuleException โ€” the underlying rule failed to execute
  • ExecutingCheckException โ€” 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:

  1. InstanceofRuleGuardian::check() is called
  2. InstanceofRule::create($targetClassName) is created
  3. Guardian executes the rule
  4. If validation succeeds, execution continues normally
  5. If validation fails, custom exception or NotInstanceofException is thrown
  6. If rule execution fails, ExecutingRuleException is thrown

Internal flow:

Value โ†’ InstanceofRuleGuardian โ†’ Guardian โ†’ InstanceofRule โ†’ Result โ†’ Exception

๐Ÿ”— Related Packages

โš–๏ธ 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.