respect-validation/library/Rules/Email.php
Henrique Moody 48405271c5
Replace placeholder "name" with "subject"
The `{{name}}` placeholder could represent different things depending on
the state of the Result, and referring to it as `{{name}}` seems
arbitrary. This commit changes it to `{{subject}}`, which is much more
generic and it describes well what that placeholder can mean.
2025-12-26 21:30:01 +01:00

55 lines
1.3 KiB
PHP

<?php
/*
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
namespace Respect\Validation\Rules;
use Attribute;
use Egulias\EmailValidator\EmailValidator;
use Egulias\EmailValidator\Validation\RFCValidation;
use Respect\Validation\Message\Template;
use Respect\Validation\Rules\Core\Simple;
use function class_exists;
use function filter_var;
use function func_num_args;
use function is_string;
use const FILTER_VALIDATE_EMAIL;
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
#[Template(
'{{subject}} must be a valid email address',
'{{subject}} must not be an email address',
)]
final class Email extends Simple
{
private readonly EmailValidator|null $validator;
public function __construct(EmailValidator|null $validator = null)
{
if ($validator === null && func_num_args() === 0 && class_exists(EmailValidator::class)) {
$validator = new EmailValidator();
}
$this->validator = $validator;
}
public function isValid(mixed $input): bool
{
if (!is_string($input)) {
return false;
}
if ($this->validator !== null) {
return $this->validator->isValid($input, new RFCValidation());
}
return (bool) filter_var($input, FILTER_VALIDATE_EMAIL);
}
}