aegisora/regex-rule

Regex Rule provides a simple, rule-based regular expression validation implementation for the Aegisora ecosystem

Maintainers

Package info

github.com/Aegisora/regex-rule

Language:Shell

pkg:composer/aegisora/regex-rule

Transparency log

Statistics

Installs: 1

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2026-08-24 16:25 UTC

This package is auto-updated.

Last update: 2026-08-24 16:28:45 UTC


README

Latest Version Total Downloads Code Coverage Badge Software License PHPStan Badge

Regex Rule provides a simple, rule-based regular expression validation implementation for the Aegisora ecosystem.

It is built on top of aegisora/rule-contract and follows its strict validation architecture, ensuring consistent and predictable behavior across applications.

This rule is useful for validating user input, form fields, usernames, slugs, email addresses, phone numbers, API request parameters, and any other string that must match a specific pattern.

πŸ“‘ Table of Contents

✨ Features

  • πŸ”Ή Lightweight and dependency-free except aegisora/rule-contract
  • πŸ”Ή Validates a string against any PCRE regular expression
  • πŸ”Ή Supports the full pattern syntax and flags (i, u, m, s, ...)
  • πŸ”Ή Rejects non-string input as an invalid context
  • πŸ”Ή Surfaces broken patterns and runtime PCRE failures as execution errors instead of a silent false
  • πŸ”Ή Fully compatible with Aegisora validation pipeline
  • πŸ”Ή Strict Context β†’ Result validation flow
  • πŸ”Ή No raw booleans β€” only structured results
  • πŸ”Ή Safe execution via base Rule abstraction
  • πŸ”Ή Expressive factory API
  • πŸ”Ή Ready to use out of the box

πŸ“¦ Installation

composer require aegisora/regex-rule

πŸš€ Core Concept

This package implements a single validation rule:

  • accepts a string value via Context
  • checks whether the string matches the configured regular expression
  • returns a standardized Result

Under the hood it wraps the common boilerplate:

if (preg_match($pattern, $value) !== 1) {
    // value does not match the pattern
}

into a reusable rule that reports its outcome through a Result object instead of a raw boolean, and turns PCRE failures into explicit exceptions.

πŸ—οΈ Basic Usage

use Aegisora\RuleContract\Models\Context;
use Aegisora\Rules\RegexRule;

$result = RegexRule::create('/^[a-z0-9_-]+$/')->validate(Context::create('user_name-1'));

if ($result->isValid()) {
    // value matches the pattern
} else {
    // value does not match the pattern
}

The rule can also be instantiated directly:

$result = (new RegexRule('/^[a-z0-9_-]+$/'))->validate(Context::create('user_name-1'));

βœ… Valid vs Invalid

The rule passes when the string matches the configured pattern and fails otherwise.

Anchored patterns

RegexRule::create('/^[a-z]+$/')->validate(Context::create('abc'));      // valid   β€” the whole string matches
RegexRule::create('/^[a-z]+$/')->validate(Context::create('abc123'));   // invalid β€” digits are not allowed

RegexRule::create('/^\d+$/')->validate(Context::create('12345'));       // valid   β€” only digits
RegexRule::create('/^\d+$/')->validate(Context::create(''));            // invalid β€” at least one digit is required

Unanchored patterns

RegexRule::create('/\d+/')->validate(Context::create('abc123'));        // valid   β€” a digit is found somewhere
RegexRule::create('/\d+/')->validate(Context::create('abcdef'));        // invalid β€” no digit found

Flags

RegexRule::create('/^abc$/i')->validate(Context::create('ABC'));        // valid   β€” case-insensitive match
RegexRule::create('/^abc$/')->validate(Context::create('ABC'));         // invalid β€” case matters without the i flag

RegexRule::create('/^[Π°-яё]+$/ui')->validate(Context::create('ΠŸΡ€ΠΈΠ²Π΅Ρ‚')); // valid  β€” u flag enables UTF-8 mode

πŸ§ͺ Validation Result

If the string matches the pattern, the rule returns a valid result.

$result->isValid(); // true

If the string does not match the pattern, the rule returns an invalid result.

$result->isValid(); // false
$result->getFailedRuleCode(); // regex_rule

If the context value is not a string, the rule throws:

Aegisora\RuleContract\Exceptions\InvalidRuleContextException

If the pattern is invalid, or the match fails at runtime (e.g. the backtrack limit is exceeded or the subject is not valid UTF-8 under the u flag), the rule throws:

Aegisora\RuleContract\Exceptions\RuleExecutionException

πŸ”— Guardian Usage

This rule can be used together with aegisora/guardian to build fluent validation pipelines.

use Aegisora\Guardian\Guardian;
use Aegisora\Rules\RegexRule;
use App\Exceptions\InvalidUsernameException;

$guardian = new Guardian();

$guardian
    ->that($username)
    ->must(RegexRule::create('/^[a-z0-9_-]{3,32}$/'), new InvalidUsernameException())
    ->validate();

If the value does not match the pattern, Guardian throws the provided domain exception.

🧭 Real-World Examples

Regex Rule is useful for enforcing format constraints before values are persisted or processed.

Examples

User Registration:

require a username of lowercase letters, digits, underscores and hyphens
Slugs:

ensure a URL slug contains only lowercase letters, digits and hyphens
Identifiers:

validate that a code matches a fixed structured format
API:

reject request parameters that do not match the expected shape

🧩 Factory Methods

RegexRule::create($pattern);

  • creates a rule that passes when the value matches the PCRE $pattern (delimiters and flags included)

new RegexRule($pattern);

  • equivalent to RegexRule::create($pattern)

RegexRule::create($pattern)->validate($context);

  • $context β€” Context wrapping the string value to validate

πŸ›οΈ Architecture

This package relies on aegisora/rule-contract.

Flow:

  1. validate() is called
  2. Context is passed in
  3. The configured pattern is checked; a broken pattern raises RuleExecutionException
  4. The string value is extracted from context (non-strings raise InvalidRuleContextException)
  5. The value is matched against the pattern with preg_match(); a PCRE runtime failure raises RuleExecutionException
  6. Result is returned β€” valid on match, invalid with the regex_rule code on no match

All logic is safely handled by Rule contract.

βš–οΈ 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.