<?php
class Validator
{
    public static function validate($data, $rules)
    {
        $errors = [];

        foreach ($rules as $field => $ruleString) {
            $rulesArray = explode('|', $ruleString);

            foreach ($rulesArray as $rule) {
                if ($rule === 'required' && empty($data[$field])) {
                    $errors[$field][] = "$field is required.";
                }

                if (strpos($rule, 'min:') === 0) {
                    $min = (int) str_replace('min:', '', $rule);
                    if (isset($data[$field]) && strlen($data[$field]) < $min) {
                        $errors[$field][] = "$field must be at least $min characters.";
                    }
                }

                if (strpos($rule, 'max:') === 0) {
                    $max = (int) str_replace('max:', '', $rule);
                    if (isset($data[$field]) && strlen($data[$field]) > $max) {
                        $errors[$field][] = "$field must be at most $max characters.";
                    }
                }

                if ($rule === 'email' && !empty($data[$field])) {
                    if (!filter_var($data[$field], FILTER_VALIDATE_EMAIL)) {
                        $errors[$field][] = "$field must be a valid email address.";
                    }
                }
            }
        }

        return $errors;
    }
}
?>