Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
Username
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
1 / 1
 validate
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3declare(strict_types=1);
4
5namespace Engelsystem\Http\Validation\Rules;
6
7use Respect\Validation\Rules\AbstractRule;
8use Respect\Validation\Validator;
9use RuntimeException;
10
11/**
12 * Username validation.
13 * Usernames must have 1-24 chars and NOT match the regular expression defined under the config key "username_regex".
14 */
15class Username extends AbstractRule
16{
17    public function validate(mixed $input): bool
18    {
19        $regex = config('username_regex');
20
21        if ($regex === null) {
22            throw new RuntimeException('username_regex not set in config');
23        }
24
25        return Validator::length(1, 24)->validate($input)
26            && Validator::not(Validator::regex($regex))->validate($input);
27    }
28}