| Server IP : 65.108.144.40 / Your IP : 216.73.217.165 Web Server : Apache/2.4.52 (Ubuntu) System : Linux ubuntu-8gb-hel1-1 5.15.0-173-generic #183-Ubuntu SMP Fri Mar 6 13:29:34 UTC 2026 x86_64 User : dev ( 1000) PHP Version : 8.2.30 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /var/www/html/project-slim/app/Validators/ |
Upload File : |
<?php
namespace App\Validators;
use App\Exceptions\ValidationException;
class AuthValidator extends BaseValidator
{
/**
* Main validation method
*/
public function validate(array $data): void
{
$this->data = $data;
$this->clearErrors();
// This method can be used for general validation
}
/**
* Validate login data
*/
public function validateLogin(array $data): void
{
$this->data = $data;
$this->clearErrors();
$this->required('email', 'Email address is required')
->email('email', 'Please enter a valid email address');
$this->required('password', 'Password is required')
->minLength('password', 6, 'Password must be at least 6 characters long');
$this->throwIfErrors('Login validation failed');
}
/**
* Validate registration data
*/
public function validateRegister(array $data): void
{
$this->data = $data;
$this->clearErrors();
// Batch validate all required fields first
$this->requiredFields([
'first_name', 'last_name', 'email', 'password'
]);
// Name validations (first_name)
$this->minLength('first_name', 2, 'First name must be at least 2 characters long')
->maxLength('first_name', 50, 'First name must not exceed 50 characters')
->regex('first_name', '/^[a-zA-Z\s\-\'\.]+$/', 'First name can only contain letters, spaces, hyphens, apostrophes, and periods');
// Name validations (last_name)
$this->minLength('last_name', 2, 'Last name must be at least 2 characters long')
->maxLength('last_name', 50, 'Last name must not exceed 50 characters')
->regex('last_name', '/^[a-zA-Z\s\-\'\.]+$/', 'Last name can only contain letters, spaces, hyphens, apostrophes, and periods');
// Email validations
$this->email('email', 'Please enter a valid email address')
->maxLength('email', 255, 'Email address must not exceed 255 characters');
// Password validation
$this->strongPassword('password');
// Optional but validated if present
if (isset($data['phone']) && !empty($data['phone'])) {
$this->phone('phone', 'Please enter a valid phone number (10-15 digits)');
}
// Company ID validation
if (isset($data['company_id'])) {
$this->integer('company_id', 'Company ID must be a valid integer')
->min('company_id', 1, 'Company ID must be a positive number');
}
// Role validation if provided
if (isset($data['role'])) {
$this->in('role', ['user', 'admin', 'manager', 'viewer'], 'Invalid role specified');
}
// Additional optional fields
if (isset($data['middle_name']) && !empty($data['middle_name'])) {
$this->maxLength('middle_name', 50, 'Middle name must not exceed 50 characters')
->regex('middle_name', '/^[a-zA-Z\s\-\'\.]+$/', 'Middle name can only contain letters, spaces, hyphens, apostrophes, and periods');
}
if (isset($data['date_of_birth']) && !empty($data['date_of_birth'])) {
$this->date('date_of_birth', 'Y-m-d', 'Date of birth must be in YYYY-MM-DD format');
}
if (isset($data['website']) && !empty($data['website'])) {
$this->url('website', 'Please enter a valid website URL');
}
// Collect ALL validation errors before throwing
$this->throwIfErrors('Registration validation failed');
}
/**
* Validate email format
*/
public function validateEmail(string $email): void
{
$this->data = ['email' => $email];
$this->clearErrors();
$this->required('email', 'Email is required')
->email('email', 'Invalid email format');
$this->throwIfErrors('Email validation failed');
}
/**
* Validate reset password data
*/
public function validateResetPassword(array $data): void
{
$this->data = $data;
$this->clearErrors();
$this->required('token', 'Reset token is required');
$this->required('password', 'New password is required')
->strongPassword('password');
$this->required('password_confirmation', 'Password confirmation is required')
->matches('password_confirmation', 'password', 'Password confirmation must match password');
$this->throwIfErrors('Password reset validation failed');
}
/**
* Validate change password data
*/
public function validateChangePassword(array $data): void
{
$this->data = $data;
$this->clearErrors();
$this->required('current_password', 'Current password is required');
$this->required('new_password', 'New password is required')
->strongPassword('new_password');
$this->required('new_password_confirmation', 'New password confirmation is required')
->matches('new_password_confirmation', 'new_password', 'New password confirmation must match new password');
// Ensure new password is different from current
if (isset($data['current_password']) && isset($data['new_password']) &&
$data['current_password'] === $data['new_password']) {
$this->addError('new_password', 'New password must be different from current password');
}
$this->throwIfErrors('Change password validation failed');
}
/**
* Validate forgot password data
*/
public function validateForgotPassword(array $data): void
{
$this->data = $data;
$this->clearErrors();
$this->required('email', 'Email address is required')
->email('email', 'Please enter a valid email address');
$this->throwIfErrors('Forgot password validation failed');
}
/**
* Validate two-factor authentication setup
*/
public function validateTwoFactorSetup(array $data): void
{
$this->data = $data;
$this->clearErrors();
$this->required('password', 'Current password is required for two-factor authentication setup');
if (isset($data['phone']) && !empty($data['phone'])) {
$this->phone('phone', 'Please enter a valid phone number for SMS authentication');
}
$this->throwIfErrors('Two-factor authentication setup validation failed');
}
/**
* Validate two-factor authentication code
*/
public function validateTwoFactorCode(array $data): void
{
$this->data = $data;
$this->clearErrors();
$this->required('code', 'Authentication code is required')
->regex('code', '/^[0-9]{6}$/', 'Authentication code must be exactly 6 digits');
$this->throwIfErrors('Two-factor authentication code validation failed');
}
/**
* Validate profile update data
*/
public function validateProfileUpdate(array $data): void
{
$this->data = $data;
$this->clearErrors();
// All fields are optional for profile update, but validate if present
if (isset($data['first_name']) && !empty($data['first_name'])) {
$this->minLength('first_name', 2, 'First name must be at least 2 characters long')
->maxLength('first_name', 50, 'First name must not exceed 50 characters')
->regex('first_name', '/^[a-zA-Z\s\-\'\.]+$/', 'First name can only contain letters, spaces, hyphens, apostrophes, and periods');
}
if (isset($data['last_name']) && !empty($data['last_name'])) {
$this->minLength('last_name', 2, 'Last name must be at least 2 characters long')
->maxLength('last_name', 50, 'Last name must not exceed 50 characters')
->regex('last_name', '/^[a-zA-Z\s\-\'\.]+$/', 'Last name can only contain letters, spaces, hyphens, apostrophes, and periods');
}
if (isset($data['email']) && !empty($data['email'])) {
$this->email('email', 'Please enter a valid email address')
->maxLength('email', 255, 'Email address must not exceed 255 characters');
}
if (isset($data['phone']) && !empty($data['phone'])) {
$this->phone('phone', 'Please enter a valid phone number');
}
if (isset($data['website']) && !empty($data['website'])) {
$this->url('website', 'Please enter a valid website URL');
}
if (isset($data['date_of_birth']) && !empty($data['date_of_birth'])) {
$this->date('date_of_birth', 'Y-m-d', 'Date of birth must be in YYYY-MM-DD format');
}
$this->throwIfErrors('Profile update validation failed');
}
}