2022-12-01 18:24:08 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace com\fwdekker\deathnotifier\validator;
|
|
|
|
|
|
|
|
use com\fwdekker\deathnotifier\ValidationException;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Verifies that the input has the specified value.
|
|
|
|
*/
|
|
|
|
class IsEqualToRule extends Rule
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string the required value
|
|
|
|
*/
|
|
|
|
private readonly string $expected;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs an `IsEqualToRule` that checks for equality with the specified value.
|
|
|
|
*
|
|
|
|
* @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.
|
|
|
|
*
|
2022-12-01 20:32:12 +01:00
|
|
|
* @param array<int|string, mixed> $inputs the list of inputs in which the value at `key` should be checked
|
2022-12-01 18:24:08 +01:00
|
|
|
* @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
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|