PHP code example of aegisora / string-length-rule

1. Go to this page and download the library: Download aegisora/string-length-rule library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

aegisora / string-length-rule example snippets


$length = mb_strlen($value);

if ($length < $min || $length > $max) {
    // length is out of the allowed boundary
}

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
}

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

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

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

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

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();