PHPackages                             aegisora/regex-rule-guardian - 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. [Validation &amp; Sanitization](/categories/validation)
4. /
5. aegisora/regex-rule-guardian

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

aegisora/regex-rule-guardian
============================

Regex Rule Guardian provides a simple shortcut for string validation against a regular expression using aegisora/guardian and aegisora/regex-rule

v1.0.0(today)01↑2900%MITPHPPHP &gt;=7.4

Since Aug 25Pushed todayCompare

[ Source](https://github.com/Aegisora/regex-rule-guardian)[ Packagist](https://packagist.org/packages/aegisora/regex-rule-guardian)[ Docs](https://github.com/Aegisora/regex-rule-guardian)[ RSS](/packages/aegisora-regex-rule-guardian/feed)WikiDiscussions main Synced today

READMEChangelog (1)Dependencies (5)Versions (2)Used By (0)

Aegisora Regex Rule Guardian
============================

[](#aegisora-regex-rule-guardian)

[![Latest Version](https://camo.githubusercontent.com/42cfd4b1b5c81bf3b7b726be69ad218107882db7fa7cb02a2e6347311d2dbac3/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f61656769736f72612f72656765782d72756c652d677561726469616e3f7374796c653d666c61742d737175617265)](https://packagist.org/packages/aegisora/regex-rule-guardian)[![Total Downloads](https://camo.githubusercontent.com/dc46886e8752a9e1b43c1c2a28b10e89f17788d7ef9c6e47c48b52e2c749d201/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f61656769736f72612f72656765782d72756c652d677561726469616e3f7374796c653d666c61742d737175617265)](https://packagist.org/packages/aegisora/regex-rule-guardian)[![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)

Regex Rule Guardian provides a simple shortcut for string validation against a regular expression using `aegisora/guardian` and `aegisora/regex-rule`.

It is designed for cases where you want to quickly check whether a value matches a given regex pattern without manually creating validation pipelines.

This package is built on top of:

- [aegisora/guardian](https://github.com/Aegisora/guardian)
- [aegisora/regex-rule](https://github.com/Aegisora/regex-rule)

---

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

[](#-features)

- 🔹 Simple shortcut API for `RegexRule`
- 🔹 Validates whether a value matches a regular expression
- 🔹 Uses `aegisora/guardian` internally
- 🔹 Uses `aegisora/regex-rule` internally
- 🔹 Supports custom validation exceptions
- 🔹 Fully compatible with the Aegisora ecosystem
- 🔹 Ready to use out of the box

---

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

[](#-installation)

```
composer require aegisora/regex-rule-guardian
```

---

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

[](#-core-concept)

This package wraps the common validation flow:

```
$guardian->check($value, RegexRule::create($pattern), new InvalidValueException());
```

into a dedicated shortcut class:

```
$regexRuleGuardian->check($value, $pattern, new InvalidValueException());
```

Instead of manually creating `RegexRule` and passing it to `Guardian`, you can use `RegexRuleGuardian` directly.

---

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

[](#️-basic-usage)

```
use Aegisora\Guardian\Exceptions\GuardianValidationException;
use Aegisora\Guardian\Guardian;
use Aegisora\RuleGuardians\RegexRule\RegexRuleGuardian;

$guardian = new Guardian();

$regexRuleGuardian = new RegexRuleGuardian($guardian);

try {
    $regexRuleGuardian->check('abc123', '/^[a-z0-9]+$/');
    // value matches the pattern
} catch (GuardianValidationException $exception) {
    // value does not match the pattern
}
```

---

🧩 Usage with Custom Exception
-----------------------------

[](#-usage-with-custom-exception)

You may provide your own exception for validation failure.

```
use Aegisora\Guardian\Guardian;
use Aegisora\RuleGuardians\RegexRule\RegexRuleGuardian;
use App\Exceptions\InvalidValueException;

$guardian = new Guardian();

$regexRuleGuardian = new RegexRuleGuardian($guardian);

$regexRuleGuardian->check('abc 123', '/^[a-z0-9]+$/', new InvalidValueException());
```

If the value does not match the pattern, the provided exception will be thrown.

This is useful when validation errors should have domain-specific meaning.

---

🧪 Example in Application Service
--------------------------------

[](#-example-in-application-service)

```
use Aegisora\RuleGuardians\RegexRule\RegexRuleGuardian;
use App\Exceptions\InvalidValueException;

final class SlugService
{
    private const SLUG_PATTERN = '/^[a-z0-9-]+$/';

    private RegexRuleGuardian $regexRuleGuardian;

    public function __construct(
        RegexRuleGuardian $regexRuleGuardian
    ) {
        $this->regexRuleGuardian = $regexRuleGuardian;
    }

    /**
     * @param mixed $value
     */
    public function process($value): void
    {
        $this->regexRuleGuardian->check($value, self::SLUG_PATTERN, new InvalidValueException());

        // business logic for a value matching the pattern
    }
}
```

---

🚨 Exceptions
------------

[](#-exceptions)

This package does not define its own exception types. All errors are raised by the underlying `aegisora/guardian` package.

Both exceptions extend the abstract base class `Aegisora\Guardian\Exceptions\GuardianException`, so you can catch every validation error with a single `catch`:

```
use Aegisora\Guardian\Exceptions\GuardianException;

try {
    $regexRuleGuardian->check($value, $pattern);
} catch (GuardianException $exception) {
    // handles GuardianValidationException and GuardianExecutingRuleException
}
```

### `GuardianValidationException`

[](#guardianvalidationexception)

Thrown when validation fails and no custom exception is provided.

```
use Aegisora\Guardian\Exceptions\GuardianValidationException;

try {
    $regexRuleGuardian->check('abc 123', '/^[a-z0-9]+$/');
} catch (GuardianValidationException $exception) {
    echo $exception->getRuleCode(); // "regex_rule"
}
```

### `GuardianExecutingRuleException`

[](#guardianexecutingruleexception)

Thrown when the underlying rule execution fails, for example when the value is not a string or the pattern is not a valid regular expression.

`Aegisora\Guardian\Exceptions\GuardianExecutingRuleException`

---

🧩 API
-----

[](#-api)

### `RegexRuleGuardian::check()`

[](#regexruleguardiancheck)

```
/**
 * @param mixed $value
 */
public function check(
    $value,
    string $pattern,
    ?\Throwable $exception = null
): void
```

Parameters:

- `$value` *(mixed)* — value to validate against the pattern
- `$pattern` *(string)* — regular expression (including delimiters and flags) the value must match
- `$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:

- `GuardianValidationException` — validation failed and no custom exception was provided
- `GuardianExecutingRuleException` — the underlying rule failed to execute (e.g. the value is not a string or the pattern is invalid)
- the provided custom exception — validation failed and a custom exception was passed

Example:

```
$regexRuleGuardian->check('abc123', '/^[a-z0-9]+$/');
```

With custom exception:

```
$regexRuleGuardian->check('abc 123', '/^[a-z0-9]+$/', new InvalidValueException());
```

---

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

[](#️-architecture)

This package is a small shortcut layer over the Aegisora validation pipeline.

Flow:

1. `RegexRuleGuardian::check()` is called
2. `RegexRule::create($pattern)` is created
3. `Guardian` executes the rule
4. If validation succeeds, execution continues normally
5. If validation fails, custom exception or `GuardianValidationException` is thrown
6. If rule execution fails, `GuardianExecutingRuleException` is thrown

Internal flow:

```
Value → RegexRuleGuardian → Guardian → RegexRule → Result → Exception

```

---

🔗 Related Packages
------------------

[](#-related-packages)

- [aegisora/guardian](https://github.com/Aegisora/guardian) — validation execution orchestrator
- [aegisora/regex-rule](https://github.com/Aegisora/regex-rule) — rule-based regular expression validation
- [aegisora/rule-contract](https://github.com/Aegisora/rule-contract) — base rule contract and validation result architecture

---

⚖️ 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

37

—

LowBetter than 81% of packages

Maintenance100

Actively maintained with recent releases

Popularity2

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity33

Early-stage or recently created project

 Bus Factor1

Top contributor holds 75% 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

0d 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 (12 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (4 commits)")

---

Tags

aegisoraaegisora-ecosystemguardianpatternsphpregexregex-validationregular-expressionrule-guardianrulesvalidationphpvalidationregexregular expressionrulepatternguardianaegisoraaegisora-ecosystemrule-guardianregex-validation

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

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

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

PHPackages © 2026

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