death-notifier/src/main/php/com/fwdekker/deathnotifier/validation/IsNotBlankRule.php

34 lines
1.2 KiB
PHP

<?php
namespace com\fwdekker\deathnotifier\validation;
/**
* Validates that the input is not a blank string.
*/
class IsNotBlankRule extends Rule
{
/**
* Validates that the input is not a blank string.
*
* @param array<int|string, mixed> $inputs the list of inputs in which the value at {@see $key} should be checked
* @param string $key the key in {@see $inputs} of the input to check
* @return void if the checked input is not a blank string
* @throws InvalidTypeException if the checked input is not set or is not a string
* @throws InvalidValueException if the checked input is a blank string
*/
public function check(array $inputs, string $key): void
{
if (!isset($inputs[$key]))
throw new InvalidTypeException("Required input '$key' not set.");
if (!is_string($inputs[$key]))
throw new InvalidTypeException("Input '$key' should be string, but is " . gettype($inputs[$key]) . ".");
if ($inputs[$key] === "")
throw new InvalidValueException("Use at least one character.", $key);
if (trim($inputs[$key]) === "")
throw new InvalidValueException("Use at least one character other than a space.", $key);
}
}