<?php
/**
 * Simple Data Validator
 */

declare(strict_types=1);

namespace App\Core;

final class Validator
{
    public static function validate(array $data, array $rules): array
    {
        $errors = [];
        foreach ($rules as $field => $rule) {
            if (str_contains($rule, 'required') && (empty($data[$field]) && $data[$field] !== '0')) {
                $errors[$field] = "The {$field} field is required.";
            }
            if (str_contains($rule, 'email') && !empty($data[$field]) && !filter_var($data[$field], FILTER_VALIDATE_EMAIL)) {
                $errors[$field] = "The {$field} must be a valid email address.";
            }
        }
        return $errors;
    }
}
