kasl/RegisterValidator.php

142 lines
3.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
class RegisterValidator
{
private $login = '';
private $email = '';
private $password = '';
private $birthday = '';
private $sex = 0;
private $disallowedLogins = ['admin', 'moderator',];
private $loginLength = ['min' => 2, 'max' => 16];
private $passwordLength = 3;
public function setDisallowedLogins(array $disallowedLogins)
{
$this->disallowedLogins = $disallowedLogins;
}
public function setPasswordLength($max)
{
$max = (int)$max;
if ($max > 0) {
$this->passwordLength = $max;
}
}
public function setLoginLength($max, $min = 2)
{
$max = (int)$max;
$min = (int)$min;
if ($max > 0 && $min > 0 && $max > $min) {
$this->loginLength = ['min' => $min, 'max' => $max];
}
}
public function setLogin($login)
{
$login = preg_replace('!\s+!', ' ', $login); // remove inner spaces
$login = trim($login); // remove outer spaces
if (
$this->loginIsAllowed($login) &&
!$this->loginIsMixed($login) &&
mb_strlen($login <= $this->loginLength['min']) &&
mb_strlen($login) >= $this->loginLength['max'] &&
!strpos("!@#$%^&*()\+|/'\"", $login)
) {
$this->login = $login;
}
return $this;
}
private function loginIsAllowed($login)
{
$d = implode('|', $this->disallowedLogins);
$pattern = "/\b($d)\b/iu";
return !preg_match($pattern, $login);
}
private function loginIsMixed($login)
{
$en = preg_match("/^(([0-9A-z -])+)$/iu", $login);
$ru = preg_match("/^([а-яёіїє\s\d]*)$/iu", $login);
return ($ru && $en) || (!$ru && !$en);
}
public function setEmail($email)
{
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
$this->email = $email;
}
return $this;
}
public function setPassword($password, $passwordVerify)
{
if ($this->password === $passwordVerify && mb_strlen($password) >= $this->passwordLength) {
$this->password = md5($password);
}
return $this;
}
public function setBirthday($birthday)
{
$bdate = DateTime::createFromFormat('Y-m-d', $birthday);
if ($bdate) {
$this->birthday = $bdate->format('d.m.Y');
}
return $this;
}
public function setSex($sex)
{
if ((int)$sex > 0) {
$this->sex = $sex;
}
return $this;
}
public function get()
{
return [
'login' => $this->login,
'email' => $this->email,
'password' => $this->password,
'birthday' => $this->birthday,
'sex' => $this->sex,
];
}
}
## написано под PHP 5.6
## использование:
require_once 'path/to/RegisterValidator.php';
$rv = new RegisterValidator();
# обязательно
$rv->setDisallowedLogins([
'Мироздатель',
'Мусорщик',
'Комментатор',
]);
# необязательный параметр, по умолчанию 16.
$rv->setLoginLength(16);
# необязательный параметр, по умолчанию 3.
$rv->setPasswordLength(3);
# проверяем введённые значения и записываем результат в массив если всё ок, или пустую строку, если не ок.
# [login->string, email->string, password->md5(string), birthday->dd.mm.YYYY, sex->int]
# birthday заточен под <input type=date>, если надо в том формате, что сейчас, надо записывать в него:
# $birthday = "$yy-$mm-$dd";
$values = $rv
->setLogin($login)
->setPassword($pass1, $pass2)
->setEmail($email)
->setBirthday($birthday)
->setSex($sex)
->get();