| 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 UserValidator extends BaseValidator
{
/**
* Main validation method
*/
public function validate(array $data): void
{
$this->data = $data;
$this->clearErrors();
}
/**
* Validate user creation data
*/
public function validateCreate(array $data): void
{
$this->data = $data;
$this->clearErrors();
// Required fields
$this->required('first_name', 'First name is required')
->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');
$this->required('last_name', 'Last name is required')
->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');
$this->required('email', 'Email address is required')
->email('email', 'Please enter a valid email address')
->maxLength('email', 255, 'Email address must not exceed 255 characters');
$this->required('password', 'Password is required')
->strongPassword('password');
// 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 (isset($data['role'])) {
$this->in('role', ['user', 'admin', 'manager', 'viewer'], 'Invalid role specified');
}
// Optional fields with validation
if (isset($data['phone']) && !empty($data['phone'])) {
$this->phone('phone', 'Please enter a valid phone number');
}
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');
}
if (isset($data['is_active'])) {
$this->boolean('is_active', 'Active status must be true or false');
}
if (isset($data['timezone']) && !empty($data['timezone'])) {
$this->maxLength('timezone', 50, 'Timezone must not exceed 50 characters');
}
if (isset($data['language']) && !empty($data['language'])) {
$this->in('language', ['en', 'es', 'fr', 'de', 'it', 'pt', 'zh', 'ja', 'ko'], 'Invalid language code');
}
$this->throwIfErrors('User creation validation failed');
}
/**
* Validate user update data
*/
public function validateUpdate(array $data): void
{
$this->data = $data;
$this->clearErrors();
// All fields are optional for 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['company_id'])) {
$this->integer('company_id', 'Company ID must be a valid integer')
->min('company_id', 1, 'Company ID must be a positive number');
}
if (isset($data['role'])) {
$this->in('role', ['user', 'admin', 'manager', 'viewer'], 'Invalid role specified');
}
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');
}
if (isset($data['is_active'])) {
$this->boolean('is_active', 'Active status must be true or false');
}
if (isset($data['timezone']) && !empty($data['timezone'])) {
$this->maxLength('timezone', 50, 'Timezone must not exceed 50 characters');
}
if (isset($data['language']) && !empty($data['language'])) {
$this->in('language', ['en', 'es', 'fr', 'de', 'it', 'pt', 'zh', 'ja', 'ko'], 'Invalid language code');
}
$this->throwIfErrors('User update validation failed');
}
/**
* Validate password update data
*/
public function validatePasswordUpdate(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 does not match');
$this->throwIfErrors('Password update validation failed');
}
/**
* Validate profile update data
*/
public function validateProfileUpdate(array $data): void
{
$this->data = $data;
$this->clearErrors();
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['phone']) && !empty($data['phone'])) {
$this->phone('phone', 'Please enter a valid phone number');
}
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');
}
if (isset($data['timezone']) && !empty($data['timezone'])) {
$this->maxLength('timezone', 50, 'Timezone must not exceed 50 characters');
}
if (isset($data['language']) && !empty($data['language'])) {
$this->in('language', ['en', 'es', 'fr', 'de', 'it', 'pt', 'zh', 'ja', 'ko'], 'Invalid language code');
}
$this->throwIfErrors('Profile update validation failed');
}
/**
* Validate user search parameters
*/
public function validateSearch(array $data): void
{
$this->data = $data;
$this->clearErrors();
if (isset($data['q']) && !empty($data['q'])) {
$this->minLength('q', 2, 'Search query must be at least 2 characters long')
->maxLength('q', 100, 'Search query must not exceed 100 characters');
}
if (isset($data['role']) && !empty($data['role'])) {
$this->in('role', ['user', 'admin', 'manager', 'viewer'], 'Invalid role specified');
}
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');
}
if (isset($data['is_active'])) {
$this->boolean('is_active', 'Active status must be true or false');
}
if (isset($data['page'])) {
$this->integer('page', 'Page must be a valid integer')
->min('page', 1, 'Page must be at least 1');
}
if (isset($data['limit'])) {
$this->integer('limit', 'Limit must be a valid integer')
->min('limit', 1, 'Limit must be at least 1')
->max('limit', 100, 'Limit must not exceed 100');
}
$this->throwIfErrors('User search validation failed');
}
}