PHPackages                             aegisora/rule-contract - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [PSR &amp; Standards](/categories/psr-standards)
4. /
5. aegisora/rule-contract

ActiveLibrary[PSR &amp; Standards](/categories/psr-standards)

aegisora/rule-contract
======================

Contracts for rule-based validation in Aegisora ecosystem

v1.0.0(1mo ago)0845↓100%1MITPHPPHP &gt;=7.4CI passing

Since Apr 26Pushed 1mo agoCompare

[ Source](https://github.com/Aegisora/rule-contract)[ Packagist](https://packagist.org/packages/aegisora/rule-contract)[ Docs](https://github.com/Aegisora/rule-contract)[ RSS](/packages/aegisora-rule-contract/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (1)Dependencies (3)Versions (2)Used By (1)

Aegisora Rule Contract
======================

[](#aegisora-rule-contract)

[![Code Coverage Badge](./badge.svg)](./badge.svg)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE)[![PHPStan Badge](https://camo.githubusercontent.com/83dd3d35cebed0eab9ee97ff1a5849c1344cda6a8ee9cac2cda20f5aa55b67bd/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048505374616e2d6c6576656c253230392d627269676874677265656e2e7376673f7374796c653d666c6174)](https://camo.githubusercontent.com/83dd3d35cebed0eab9ee97ff1a5849c1344cda6a8ee9cac2cda20f5aa55b67bd/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048505374616e2d6c6576656c253230392d627269676874677265656e2e7376673f7374796c653d666c6174)

**Rule Contract** defines the core abstractions for building validation rules in the Aegisora ecosystem.

It provides:

- a minimal, stable, and framework-agnostic contract that allows rules to be shared across packages and projects
- a strict contract for implementing rules
- consistent result handling
- unified exception management

---

✨ Features
----------

[](#-features)

- 🔹 Lightweight, framework-agnostic design and dependency-free
- 🔹 Stable contract for validation rules
- 🔹 Unified validation result structure
- 🔹 Safe exception handling with execution wrapping
- 🔹 Automatic rule code generation
- 🔹 Supports both simple and complex rules
- 🔹 Designed for extensibility
- 🔹 Compatible with Aegisora ecosystem (`guardian`, `rules`, etc.)

---

📦 Installation
--------------

[](#-installation)

```
composer require aegisora/rule-contract
```

---

🚀 Core Concept
--------------

[](#-core-concept)

Each rule:

- receives a `Context`
- performs validation logic
- returns a `Result`
- never returns raw booleans
- never throws unstructured exceptions

This ensures predictable and testable validation flow.

---

🏗️ Basic Usage
--------------

[](#️-basic-usage)

### Creating a Rule

[](#creating-a-rule)

Extend the abstract `Rule` class (simple example):

```
class UserAgeRule extends Rule
{
    protected function executeValidate(Context $context): Result
    {
        $age = $context->getValue();

        if ($age < 18) {
            return $this->getDefaultInvalidResult();
        }

        return $this->getDefaultValidResult();
    }
}
```

### Running a Rule

[](#running-a-rule)

```
$rule = new UserAgeRule();
$result = $rule->validate(Context::create(20));
if ($result->isValid()) {
    // valid
}
```

---

🏛️ Architecture
---------------

[](#️-architecture)

---

### RuleInterface

[](#ruleinterface)

Defines the contract for all rules:

- `validate(Context $context): Result`

May throw:

- `InvalidRuleContextException`
- `RuleException`
- `RuleExecutionException`

---

### Rule (Abstract Class)

[](#rule-abstract-class)

Base implementation that provides:

- Safe execution layer
    - wraps execution in `try/catch`
    - rethrows domain exceptions as-is
    - wraps unexpected errors into `RuleExecutionException`
- Default helpers
    - `getDefaultValidResult()`
    - `getDefaultInvalidResult()`
- Automatic rule code generation - generates `snake_case` code from class name:
    - `UserAgeRule` → `user_age_rule`

#### Execution Flow

[](#execution-flow)

1. `validate()` is called
2. `executeValidate()` runs
3. Result handling:
    - `RuleException` → rethrown
    - `Throwable` → wrapped into `RuleExecutionException`
4. `Result` is returned

---

### Context

[](#context)

Encapsulates input data for rule execution.

`Context::create($value);`

- stores `mixed` value
- provides `getValue()` access

Used to decouple rules from application structures.

---

### Result

[](#result)

Standardized validation result object.

Structure

- `isValid: bool`
- `failedRuleCode: ?string`

Factory methods

- `Result::valid()`
- `Result::invalid('rule_code')`

---

### Exception Handling

[](#exception-handling)

#### `RuleException`

[](#ruleexception)

Base exception for all rule-related errors.

#### `InvalidRuleContextException`

[](#invalidrulecontextexception)

Thrown when context is invalid for a rule.

#### `RuleExecutionException`

[](#ruleexecutionexception)

Thrown when unexpected runtime error occurs during rule execution.

Contains:

- rule class name (`getRuleClassName()`)
- original exception

---

### Design Principles

[](#design-principles)

This package enforces:

- predictable execution flow
- strict separation of concerns
- consistent validation results
- safe error boundaries
- framework independence
- testable business rules

---

⚖️ License
----------

[](#️-license)

This package is open-source and licensed under the MIT License. See the LICENSE for details.

---

🌱 Contributing
--------------

[](#-contributing)

Contributions are welcome and greatly appreciated!. See the CONTRIBUTING for details.

---

🌟 Support
---------

[](#-support)

If you find this project useful, please consider giving it a star on GitHub!

It helps the project grow and motivates further development.

###  Health Score

40

—

FairBetter than 86% of packages

Maintenance91

Actively maintained with recent releases

Popularity20

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity33

Early-stage or recently created project

 Bus Factor1

Top contributor holds 77.2% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

44d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/81402dcd0a07ad550b7f80f5871e7c302770b29d4c73a52fc35ba697f702d56e?d=identicon)[arslanim](/maintainers/arslanim)

---

Top Contributors

[![arslanim](https://avatars.githubusercontent.com/u/22678154?v=4)](https://github.com/arslanim "arslanim (122 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (36 commits)")

---

Tags

aegisoraaegisora-ecosystemphprule-based-predicate-checks-of-input-datarule-contractphpcontractsvalidationrulesaegisoraaegisora-ecosystemrule-contract

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/aegisora-rule-contract/health.svg)

```
[![Health](https://phpackages.com/badges/aegisora-rule-contract/health.svg)](https://phpackages.com/packages/aegisora-rule-contract)
```

###  Alternatives

[marwanalsoltany/mighty

The last validation library you will ever need!

591.3k](/packages/marwanalsoltany-mighty)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
