| 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 CompanyValidator extends BaseValidator
{
private $validIndustries = [
'technology', 'healthcare', 'finance', 'education', 'retail',
'manufacturing', 'construction', 'transportation', 'energy',
'entertainment', 'consulting', 'real_estate', 'agriculture', 'other'
];
private $validCompanySizes = ['1-10', '11-50', '51-200', '201-500', '501-1000', '1000+'];
/**
* Main validation method
*/
public function validate(array $data): void
{
$this->data = $data;
$this->clearErrors();
}
/**
* Validate company creation data
*/
public function validateCreate(array $data): void
{
$this->data = $data;
$this->clearErrors();
// Required fields
$this->required('name', 'Company name is required')
->minLength('name', 2, 'Company name must be at least 2 characters long')
->maxLength('name', 100, 'Company name must not exceed 100 characters')
->regex('name', '/^[a-zA-Z0-9\s\-\.\&\(\)]+$/', 'Company name contains invalid characters');
$this->required('email', 'Company email is required')
->email('email', 'Please enter a valid email address')
->maxLength('email', 255, 'Email address must not exceed 255 characters');
$this->required('address', 'Company address is required')
->minLength('address', 5, 'Address must be at least 5 characters long')
->maxLength('address', 255, 'Address must not exceed 255 characters');
$this->required('city', 'City is required')
->minLength('city', 2, 'City must be at least 2 characters long')
->maxLength('city', 100, 'City must not exceed 100 characters')
->regex('city', '/^[a-zA-Z\s\-\'\.]+$/', 'City can only contain letters, spaces, hyphens, apostrophes, and periods');
$this->required('country', 'Country is required')
->minLength('country', 2, 'Country must be at least 2 characters long')
->maxLength('country', 100, 'Country must not exceed 100 characters')
->regex('country', '/^[a-zA-Z\s\-\'\.]+$/', 'Country can only contain letters, spaces, hyphens, apostrophes, and periods');
// Optional fields with validation
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['industry']) && !empty($data['industry'])) {
$this->in('industry', $this->validIndustries, 'Invalid industry type');
}
if (isset($data['company_size']) && !empty($data['company_size'])) {
$this->in('company_size', $this->validCompanySizes, 'Invalid company size');
}
if (isset($data['tax_id']) && !empty($data['tax_id'])) {
$this->minLength('tax_id', 5, 'Tax ID must be at least 5 characters long')
->maxLength('tax_id', 20, 'Tax ID must not exceed 20 characters')
->regex('tax_id', '/^[a-zA-Z0-9\-]+$/', 'Tax ID can only contain letters, numbers, and hyphens');
}
if (isset($data['postal_code']) && !empty($data['postal_code'])) {
$this->maxLength('postal_code', 10, 'Postal code must not exceed 10 characters')
->regex('postal_code', '/^[a-zA-Z0-9\s\-]+$/', 'Invalid postal code format');
}
if (isset($data['state']) && !empty($data['state'])) {
$this->maxLength('state', 100, 'State must not exceed 100 characters')
->regex('state', '/^[a-zA-Z\s\-\'\.]+$/', 'State can only contain letters, spaces, hyphens, apostrophes, and periods');
}
if (isset($data['status'])) {
$this->in('status', ['active', 'inactive', 'suspended'], 'Invalid status. Must be active, inactive, or suspended');
}
if (isset($data['founded_year'])) {
$currentYear = (int)date('Y');
$this->integer('founded_year', 'Founded year must be a valid year')
->min('founded_year', 1800, 'Founded year must be after 1800')
->max('founded_year', $currentYear, 'Founded year cannot be in the future');
}
if (isset($data['employee_count'])) {
$this->integer('employee_count', 'Employee count must be a valid number')
->min('employee_count', 1, 'Employee count must be at least 1');
}
if (isset($data['is_public'])) {
$this->boolean('is_public', 'Public status must be true or false');
}
$this->throwIfErrors('Company creation validation failed');
}
/**
* Validate company 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['name']) && !empty($data['name'])) {
$this->minLength('name', 2, 'Company name must be at least 2 characters long')
->maxLength('name', 100, 'Company name must not exceed 100 characters')
->regex('name', '/^[a-zA-Z0-9\s\-\.\&\(\)]+$/', 'Company name contains invalid characters');
}
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['address']) && !empty($data['address'])) {
$this->minLength('address', 5, 'Address must be at least 5 characters long')
->maxLength('address', 255, 'Address must not exceed 255 characters');
}
if (isset($data['city']) && !empty($data['city'])) {
$this->minLength('city', 2, 'City must be at least 2 characters long')
->maxLength('city', 100, 'City must not exceed 100 characters')
->regex('city', '/^[a-zA-Z\s\-\'\.]+$/', 'City can only contain letters, spaces, hyphens, apostrophes, and periods');
}
if (isset($data['country']) && !empty($data['country'])) {
$this->minLength('country', 2, 'Country must be at least 2 characters long')
->maxLength('country', 100, 'Country must not exceed 100 characters')
->regex('country', '/^[a-zA-Z\s\-\'\.]+$/', 'Country can only contain letters, spaces, hyphens, apostrophes, and periods');
}
if (isset($data['industry']) && !empty($data['industry'])) {
$this->in('industry', $this->validIndustries, 'Invalid industry type');
}
if (isset($data['company_size']) && !empty($data['company_size'])) {
$this->in('company_size', $this->validCompanySizes, 'Invalid company size');
}
if (isset($data['tax_id']) && !empty($data['tax_id'])) {
$this->minLength('tax_id', 5, 'Tax ID must be at least 5 characters long')
->maxLength('tax_id', 20, 'Tax ID must not exceed 20 characters')
->regex('tax_id', '/^[a-zA-Z0-9\-]+$/', 'Tax ID can only contain letters, numbers, and hyphens');
}
if (isset($data['postal_code']) && !empty($data['postal_code'])) {
$this->maxLength('postal_code', 10, 'Postal code must not exceed 10 characters')
->regex('postal_code', '/^[a-zA-Z0-9\s\-]+$/', 'Invalid postal code format');
}
if (isset($data['state']) && !empty($data['state'])) {
$this->maxLength('state', 100, 'State must not exceed 100 characters')
->regex('state', '/^[a-zA-Z\s\-\'\.]+$/', 'State can only contain letters, spaces, hyphens, apostrophes, and periods');
}
if (isset($data['status'])) {
$this->in('status', ['active', 'inactive', 'suspended'], 'Invalid status. Must be active, inactive, or suspended');
}
if (isset($data['founded_year'])) {
$currentYear = (int)date('Y');
$this->integer('founded_year', 'Founded year must be a valid year')
->min('founded_year', 1800, 'Founded year must be after 1800')
->max('founded_year', $currentYear, 'Founded year cannot be in the future');
}
if (isset($data['employee_count'])) {
$this->integer('employee_count', 'Employee count must be a valid number')
->min('employee_count', 1, 'Employee count must be at least 1');
}
if (isset($data['is_public'])) {
$this->boolean('is_public', 'Public status must be true or false');
}
$this->throwIfErrors('Company update validation failed');
}
/**
* Validate company 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['industry']) && !empty($data['industry'])) {
$this->in('industry', $this->validIndustries, 'Invalid industry type');
}
if (isset($data['company_size']) && !empty($data['company_size'])) {
$this->in('company_size', $this->validCompanySizes, 'Invalid company size');
}
if (isset($data['status'])) {
$this->in('status', ['active', 'inactive', 'suspended'], 'Invalid status');
}
if (isset($data['country']) && !empty($data['country'])) {
$this->maxLength('country', 100, 'Country must not exceed 100 characters');
}
if (isset($data['city']) && !empty($data['city'])) {
$this->maxLength('city', 100, 'City must not exceed 100 characters');
}
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('Company search validation failed');
}
/**
* Validate company filter parameters
*/
public function validateFilter(array $data): void
{
$this->data = $data;
$this->clearErrors();
if (isset($data['industry']) && !empty($data['industry'])) {
$this->in('industry', $this->validIndustries, 'Invalid industry type');
}
if (isset($data['company_size']) && !empty($data['company_size'])) {
$this->in('company_size', $this->validCompanySizes, 'Invalid company size');
}
if (isset($data['status'])) {
$this->in('status', ['active', 'inactive', 'suspended'], 'Invalid status');
}
if (isset($data['country']) && !empty($data['country'])) {
$this->maxLength('country', 100, 'Country must not exceed 100 characters');
}
if (isset($data['city']) && !empty($data['city'])) {
$this->maxLength('city', 100, 'City must not exceed 100 characters');
}
if (isset($data['created_from']) && !empty($data['created_from'])) {
$this->date('created_from', 'Y-m-d', 'Created from date must be in YYYY-MM-DD format');
}
if (isset($data['created_to']) && !empty($data['created_to'])) {
$this->date('created_to', 'Y-m-d', 'Created to date must be in YYYY-MM-DD format');
}
// Validate date range
if (isset($data['created_from']) && isset($data['created_to']) &&
!empty($data['created_from']) && !empty($data['created_to'])) {
$fromDate = \DateTime::createFromFormat('Y-m-d', $data['created_from']);
$toDate = \DateTime::createFromFormat('Y-m-d', $data['created_to']);
if ($fromDate && $toDate && $fromDate > $toDate) {
$this->addError('created_from', 'Created from date must be before created to date');
}
}
if (isset($data['sort_by']) && !empty($data['sort_by'])) {
$this->in('sort_by', ['company_id', 'name', 'email', 'industry', 'created_at', 'updated_at'], 'Invalid sort field');
}
if (isset($data['sort_order']) && !empty($data['sort_order'])) {
$this->in('sort_order', ['asc', 'desc'], 'Sort order must be asc or desc');
}
$this->throwIfErrors('Company filter validation failed');
}
}