public static function validate($data, $isLogin = false) { $table = self::$table; if (isset($data['username']) && !empty($data['username'])) { if (!$isLogin && strlen($data['username']) < 3) { throw new Validation_Exception('Login', 2, 3); } else { if (!$isLogin && strlen($data['username']) > 20) { throw new Validation_Exception('Login', 3, 20); } } if (!preg_match('#[0-9a-zA-Z\\s-]+#', $data['username'])) { throw new Validation_Exception('Login', 6); } $duplicate = Database::getDB()->users->findOne(['username' => $data['username']]); if ($duplicate && !$isLogin) { throw new Validation_Exception("Login", 8); } } else { throw new Validation_Exception('Login', 1); } if (!$isLogin) { if (isset($data['email']) && !empty($data['email'])) { if (!filter_var($data['email'], FILTER_VALIDATE_EMAIL)) { throw new Validation_Exception('Email', 5); } $duplicate = Database::getDB()->users->findOne(['email' => $data['email']]); if ($duplicate && !$isLogin) { throw new Validation_Exception("Email", 8); } } else { throw new Validation_Exception('Email', 1); } } if (isset($data['password']) && !empty($data['password'])) { if (!$isLogin && strlen($data['password']) < 3) { throw new Validation_Exception('Hasło', 2, 3); } else { if (!$isLogin && strlen($data['password']) > 20) { throw new Validation_Exception('Hasło', 3, 20); } } } else { throw new Validation_Exception('Hasło', 1); } if (!$isLogin) { if (isset($data['repeat_password']) && !empty($data['repeat_password'])) { if ($data['repeat_password'] !== $data['password']) { throw new Validation_Exception('Powtórz hasło', 4); } } else { throw new Validation_Exception('Powtórz hasło', 1); } } return true; }
public function __construct() { $this->db = Database::getDB(); }