kasl/RegisterValidator.php

96 lines
2.3 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;
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)) {
$this->login = $login;
}
return $this;
}
private function loginIsAllowed($login)
{
$disallowed = [
'Мироздатель',
'Мусорщик',
'Комментатор',
];
$d = implode('|', $disallowed);
$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) {
$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 verify()
{
if (!empty($this->login) && !empty($this->email) && !empty($this->password) && !empty($this->birthday)) {
return [
'login' => $this->login,
'email' => $this->email,
'password' => $this->password,
'birthday' => $this->birthday,
'sex' => $this->sex,
];
}
return [];
}
}
$rv = new RegisterValidator();
$values = $rv
->setLogin($login)
->setPassword($pass1, $pass2)
->setEmail($email)
->setBirthday($birthday)
->setSex($sex)
->verify();