How can PHP validation classes be structured to handle multiple validation rules and provide clear feedback on validation results?

When structuring PHP validation classes to handle multiple validation rules and provide clear feedback, it is important to use a modular approach where each validation rule is a separate method within the class. This allows for easy addition, removal, or modification of rules. Additionally, the class should return specific error messages or codes to indicate which validation rules failed, providing clear feedback to the user.

class Validator {
    public function validate($data) {
        $errors = [];

        if (!$this->validateRequired($data['name'])) {
            $errors['name'] = 'Name is required';
        }

        if (!$this->validateEmail($data['email'])) {
            $errors['email'] = 'Invalid email format';
        }

        return $errors;
    }

    private function validateRequired($value) {
        return !empty($value);
    }

    private function validateEmail($value) {
        return filter_var($value, FILTER_VALIDATE_EMAIL);
    }
}

// Example usage
$validator = new Validator();
$data = [
    'name' => 'John Doe',
    'email' => 'john.doe@example.com'
];
$errors = $validator->validate($data);

if (!empty($errors)) {
    foreach ($errors as $field => $error) {
        echo $field . ': ' . $error . PHP_EOL;
    }
}