death-notifier/src/main/php/com/fwdekker/deathnotifier/validator/IsEmailRule.php

27 lines
853 B
PHP

<?php
namespace com\fwdekker\deathnotifier\validator;
use com\fwdekker\deathnotifier\Response;
/**
* Requires the input to be a valid email address.
*/
class IsEmailRule extends Rule
{
/**
* Checks whether the input is a valid email address.
*
* @param array<string, mixed> $inputs the list of inputs in which the value at `key` should be checked
* @param string $key the key in `inputs` of the input to check
* @return Response|null `null` if `$inputs[$key]` is an email address, or an unsatisfied `Response` otherwise
*/
public function check(array $inputs, string $key): ?Response
{
return !isset($inputs[$key]) || !filter_var($inputs[$key], FILTER_VALIDATE_EMAIL)
? Response::unsatisfied($this->override_message ?? "Enter a valid email address.", $key)
: null;
}
}