2018-02-11 16:39:50 -06:00
|
|
|
<?php
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2018-02-11 16:39:50 -06:00
|
|
|
namespace Pterodactyl\Rules;
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
use Closure;
|
|
|
|
|
use Illuminate\Contracts\Validation\ValidationRule;
|
2018-02-11 16:39:50 -06:00
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
class Username implements ValidationRule
|
2018-02-11 16:39:50 -06:00
|
|
|
{
|
2018-02-18 13:29:28 -06:00
|
|
|
/**
|
|
|
|
|
* Regex to use when validating usernames.
|
|
|
|
|
*/
|
2026-10-05 18:10:18 -04:00
|
|
|
public const string VALIDATION_REGEX = '/^[a-z0-9]([\w\.-]+)[a-z0-9]$/';
|
2018-02-11 16:39:50 -06:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Validate that a username contains only the allowed characters and starts/ends
|
2022-10-14 10:59:20 -06:00
|
|
|
* with alphanumeric characters.
|
2018-02-11 16:39:50 -06:00
|
|
|
*
|
|
|
|
|
* Allowed characters: a-z0-9_-.
|
|
|
|
|
*/
|
2026-10-05 18:10:18 -04:00
|
|
|
public function validate(string $attribute, mixed $value, Closure $fail): void
|
2018-02-11 16:39:50 -06:00
|
|
|
{
|
2026-10-05 18:10:18 -04:00
|
|
|
if (is_string($value) && preg_match(self::VALIDATION_REGEX, mb_strtolower($value)) === 1) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-02-11 16:39:50 -06:00
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
$fail('The :attribute must start and end with alpha-numeric characters and contain only letters, numbers, dashes, underscores, and periods.');
|
2018-02-18 13:29:28 -06:00
|
|
|
}
|
2018-02-11 16:39:50 -06:00
|
|
|
}
|