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

47 lines
1.3 KiB
PHP

<?php
namespace com\fwdekker\deathnotifier\validator;
/**
* Verifies that the input has the specified value.
*/
class IsEqualToRule extends Rule
{
/**
* @var string the required value
*/
private readonly string $expected;
/**
* Constructs a new `IsEqualToRule`.
*
* @param string $expected the value that checked values should be equal to
*/
public function __construct(string $expected, ?string $override_message = null)
{
parent::__construct($override_message);
$this->expected = $expected;
}
/**
* Verifies that the input has the specified value.
*
* @param array<int|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 void if `$inputs[$key]` equals `$expected`
* @throws ValidationException if `$inputs[$key]` is not set or does not equal `$expected`
*/
public function check(array $inputs, string $key): void
{
if (!isset($inputs[$key]) || $inputs[$key] !== $this->expected)
throw new ValidationException(
$this->override_message ?? "Inputs '$key' should equal '$this->expected'.",
$key
);
}
}