PHP code example of aegisora / regex-rule

1. Go to this page and download the library: Download aegisora/regex-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 / regex-rule example snippets


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

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
}

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

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 

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

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

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

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