aegisora / string-length-rule
String Length Rule provides a simple, rule-based string length validation implementation for the Aegisora ecosystem
Requires
- php: >=7.4
- aegisora/rule-contract: ^1.0
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^9.6
- squizlabs/php_codesniffer: ^4.0
README
String Length Rule provides a simple, rule-based string length 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, passwords, API request parameters, database column constraints, and any other string that must satisfy a length boundary.
๐ Table of Contents
- Features
- Installation
- Core Concept
- Basic Usage
- Valid vs Invalid
- Validation Result
- Guardian Usage
- Real-World Examples
- Factory Methods
- Architecture
- License
- Contributing
- Support
โจ Features
- ๐น Lightweight and dependency-free except
aegisora/rule-contract - ๐น Validates a string length against a lower bound, an upper bound, or a range
- ๐น Supports strict (
>,<) and inclusive (>=,<=) comparisons - ๐น Counts characters (not bytes) via native
mb_strlen(), so multibyte strings are measured correctly - ๐น Rejects non-string input as an invalid context
- ๐น Fully compatible with Aegisora validation pipeline
- ๐น Strict
ContextโResultvalidation flow - ๐น No raw booleans โ only structured results
- ๐น Safe execution via base
Ruleabstraction - ๐น Expressive factory API for every boundary variation
- ๐น Ready to use out of the box
๐ฆ Installation
composer require aegisora/string-length-rule
๐ Core Concept
This package implements a single validation rule with several factory variations:
- accepts a string value via
Context - checks whether the string length satisfies the configured boundary
- returns a standardized
Result
Under the hood it wraps the common boilerplate:
$length = mb_strlen($value); if ($length < $min || $length > $max) { // length is out of the allowed boundary }
into a reusable rule that reports its outcome through a Result object instead of a raw boolean.
๐๏ธ Basic Usage
use Aegisora\RuleContract\Models\Context; use Aegisora\Rules\StringLengthRule; $result = StringLengthRule::createGreaterThanOrEqualTo(8)->validate(Context::create('super-secret')); if ($result->isValid()) { // length satisfies the boundary } else { // length is out of the allowed boundary }
โ Valid vs Invalid
The rule passes when the string length satisfies the configured boundary and fails otherwise. The length is measured in characters via mb_strlen().
Lower bound
StringLengthRule::createGreaterThan(3)->validate(Context::create('abcd')); // valid โ 4 > 3 StringLengthRule::createGreaterThan(3)->validate(Context::create('abc')); // invalid โ 3 is not > 3 StringLengthRule::createGreaterThanOrEqualTo(3)->validate(Context::create('abc')); // valid โ 3 >= 3 StringLengthRule::createGreaterThanOrEqualTo(3)->validate(Context::create('ab')); // invalid โ 2 < 3
Upper bound
StringLengthRule::createLessThan(3)->validate(Context::create('ab')); // valid โ 2 < 3 StringLengthRule::createLessThan(3)->validate(Context::create('abc')); // invalid โ 3 is not < 3 StringLengthRule::createLessThanOrEqualTo(3)->validate(Context::create('abc')); // valid โ 3 <= 3 StringLengthRule::createLessThanOrEqualTo(3)->validate(Context::create('abcd')); // invalid โ 4 > 3
Range
StringLengthRule::createBetween(2, 4)->validate(Context::create('abc')); // valid โ 2 <= 3 <= 4 StringLengthRule::createBetween(2, 4)->validate(Context::create('a')); // invalid โ 1 < 2 StringLengthRule::createBetweenExclusive(2, 4)->validate(Context::create('abc')); // valid โ 2 < 3 < 4 StringLengthRule::createBetweenExclusive(2, 4)->validate(Context::create('ab')); // invalid โ 2 is not > 2 StringLengthRule::createBetweenMinExclusive(2, 4)->validate(Context::create('abcd')); // valid โ 2 < 4 <= 4 StringLengthRule::createBetweenMinExclusive(2, 4)->validate(Context::create('ab')); // invalid โ 2 is not > 2 StringLengthRule::createBetweenMaxExclusive(2, 4)->validate(Context::create('ab')); // valid โ 2 <= 2 < 4 StringLengthRule::createBetweenMaxExclusive(2, 4)->validate(Context::create('abcd')); // invalid โ 4 is not < 4
๐งช Validation Result
If the length satisfies the boundary, the rule returns a valid result.
$result->isValid(); // true
If the length is out of the boundary, the rule returns an invalid result.
$result->isValid(); // false $result->getFailedRuleCode(); // string_length_rule
If the context value is not a string, the rule throws:
Aegisora\RuleContract\Exceptions\InvalidRuleContextException
๐ Guardian Usage
This rule can be used together with aegisora/guardian to build fluent validation pipelines.
use Aegisora\Guardian\Guardian; use Aegisora\Rules\StringLengthRule; use App\Exceptions\InvalidUsernameException; $guardian = new Guardian(); $guardian ->that($username) ->must(StringLengthRule::createBetween(3, 32), new InvalidUsernameException()) ->validate();
If the length is out of the allowed boundary, Guardian throws the provided domain exception.
๐งญ Real-World Examples
String Length Rule is useful for enforcing length constraints before values are persisted or processed.
Examples
User Registration:
require a username between 3 and 32 characters
Security:
require a password of at least 8 characters
Database:
ensure a value fits a VARCHAR column limit
API:
reject request parameters that exceed a maximum length
๐งฉ Factory Methods
StringLengthRule::createGreaterThan($length);
- passes when the string length is strictly greater than
$length
StringLengthRule::createGreaterThanOrEqualTo($length);
- passes when the string length is greater than or equal to
$length
StringLengthRule::createLessThan($length);
- passes when the string length is strictly less than
$length
StringLengthRule::createLessThanOrEqualTo($length);
- passes when the string length is less than or equal to
$length
StringLengthRule::createBetween($min, $max);
- passes when the string length is between
$minand$max, both boundaries inclusive ($min <= length <= $max)
StringLengthRule::createBetweenExclusive($min, $max);
- passes when the string length is between
$minand$max, both boundaries exclusive ($min < length < $max)
StringLengthRule::createBetweenMinExclusive($min, $max);
- passes when the string length is between
$min(exclusive) and$max(inclusive) ($min < length <= $max)
StringLengthRule::createBetweenMaxExclusive($min, $max);
- passes when the string length is between
$min(inclusive) and$max(exclusive) ($min <= length < $max)
StringLengthRule::createGreaterThanOrEqualTo($length)->validate($context);
$contextโContextwrapping the string value to validate
๐๏ธ Architecture
This package relies on aegisora/rule-contract.
Flow:
validate()is calledContextis passed in- The string value is extracted from context (non-strings raise
InvalidRuleContextException) - The length is measured with
mb_strlen() - The length is compared against the configured boundary
Resultis returned โ valid on success, invalid with thestring_length_rulecode on failure
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.