68 lines
2.2 KiB
PHP
68 lines
2.2 KiB
PHP
<?php
|
||
|
||
namespace Validator;
|
||
|
||
class Login
|
||
{
|
||
private const RESTRICTED = [
|
||
'ангел', 'angel', 'администрация', 'administration', 'Комментатор',
|
||
'Мироздатель', 'Мусорщик', 'Падальщик', 'Повелитель',
|
||
'Архивариус', 'Пересмешник', 'Волынщик', 'Лорд Разрушитель',
|
||
'Милосердие', 'Справедливость', 'Искушение', 'Вознесение',
|
||
];
|
||
public const LENGTH = ['min' => 4, 'max' => 16];
|
||
protected readonly string $login;
|
||
private int $errorcode = 0;
|
||
|
||
public function getErrorCode(): int
|
||
{
|
||
return $this->errorcode;
|
||
}
|
||
|
||
public function setLogin(string $login): self
|
||
{
|
||
$login = preg_replace('!\s+!', ' ', $login); // remove inner spaces
|
||
$login = trim($login); // remove outer spaces
|
||
$this->login = $login;
|
||
return $this;
|
||
}
|
||
|
||
public function get(): string
|
||
{
|
||
return $this->check() ? $this->login : '';
|
||
}
|
||
|
||
public function check(): bool
|
||
{
|
||
if (!$this->isAllowed()) {
|
||
$this->errorcode = 1;
|
||
} elseif ($this->isMixed()) {
|
||
$this->errorcode = 2;
|
||
} elseif (mb_strlen($this->login) < self::LENGTH['min']) {
|
||
$this->errorcode = 3;
|
||
} elseif (mb_strlen($this->login) > self::LENGTH['max']) {
|
||
$this->errorcode = 4;
|
||
} elseif (substr_count($this->login, ' ') + substr_count($this->login, '-') + substr_count($this->login, '_') > 2) {
|
||
$this->errorcode = 5;
|
||
} elseif (strpos("!@#$%^&*()\+|/'\"", $this->login) || empty($this->login)) {
|
||
$this->errorcode = 6;
|
||
}
|
||
|
||
return $this->errorcode === 0;
|
||
}
|
||
|
||
private function isAllowed(): bool
|
||
{
|
||
$d = implode('|', self::RESTRICTED);
|
||
$pattern = "/\b($d)\b/iu";
|
||
return !preg_match($pattern, $this->login);
|
||
}
|
||
|
||
private function isMixed(): bool
|
||
{
|
||
$en = preg_match("/^(([0-9A-z -])+)$/iu", $this->login);
|
||
$ru = preg_match("/^([а-яёіїє\s\d]*)$/iu", $this->login);
|
||
return ($ru && $en) || (!$ru && !$en);
|
||
}
|
||
}
|