<?php

namespace WPUltimateSecurity\UltimateSecurity\Support\Validation\Rules;

/**
 * Email Rule
 *
 * Validates that a value is a valid email address.
 *
 * @package WPUltimateSecurity\UltimateSecurity\Support\Validation\Rules
 * @since 2.0.0
 */
class EmailRule extends AbstractRule
{
    /**
     * {@inheritDoc}
     */
    public function validate(mixed $value, string $field, array $data = []): bool
    {
        if ($value === null || $value === '') {
            return true;
        }

        if (!is_string($value)) {
            return false;
        }

        return is_email($value) !== false;
    }

    /**
     * {@inheritDoc}
     */
    public function getName(): string
    {
        return 'email';
    }

    /**
     * {@inheritDoc}
     */
    protected function getDefaultMessage(string $field): string
    {
        return sprintf(
            /* translators: %s: field name */
            __('The %s must be a valid email address.', 'ultimate-security'),
            $field
        );
    }
}
