2018-02-11 22:39:50 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Rules;
|
|
|
|
|
|
|
|
use Illuminate\Contracts\Validation\Rule;
|
|
|
|
|
|
|
|
class Username implements Rule
|
|
|
|
{
|
2018-02-18 19:29:28 +00:00
|
|
|
/**
|
|
|
|
* Regex to use when validating usernames.
|
|
|
|
*/
|
2018-02-11 22:39:50 +00:00
|
|
|
public const VALIDATION_REGEX = '/^[a-z0-9]([\w\.-]+)[a-z0-9]$/';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate that a username contains only the allowed characters and starts/ends
|
2022-10-14 16:59:20 +00:00
|
|
|
* with alphanumeric characters.
|
2018-02-11 22:39:50 +00:00
|
|
|
*
|
|
|
|
* Allowed characters: a-z0-9_-.
|
|
|
|
*
|
|
|
|
* @param string $attribute
|
2021-01-28 04:52:11 +00:00
|
|
|
* @param mixed $value
|
2018-02-11 22:39:50 +00:00
|
|
|
*/
|
|
|
|
public function passes($attribute, $value): bool
|
|
|
|
{
|
|
|
|
return preg_match(self::VALIDATION_REGEX, mb_strtolower($value));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a validation message for use when this rule fails.
|
|
|
|
*/
|
|
|
|
public function message(): string
|
|
|
|
{
|
|
|
|
return 'The :attribute must start and end with alpha-numeric characters and
|
|
|
|
contain only letters, numbers, dashes, underscores, and periods.';
|
|
|
|
}
|
2018-02-18 19:29:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert the rule to a validation string. This is necessary to avoid
|
|
|
|
* issues with Eloquence which tries to use this rule as a string.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function __toString(): string
|
2018-02-18 19:29:28 +00:00
|
|
|
{
|
|
|
|
return 'p_username';
|
|
|
|
}
|
2018-02-11 22:39:50 +00:00
|
|
|
}
|