adding a clamp function

This commit is contained in:
Brian 2023-03-27 10:19:42 -06:00
parent 22946bc031
commit 4dcb6b6ebe
Signed by: brian
GPG Key ID: DE1A5390A3B84CD8

View File

@ -15,6 +15,47 @@ require_once "functions/temperatures.php";
|
*/
if (! function_exists('clamp')) {
/**
* Ensure a numerical value is between two bounds.
*
* @since 1.0.0
*
* @param int|float|string $number The value to be clamped between two other values.
* @param int|float|string $minNumber The miminum value for clamping bounds.
* @param int|float|string $maxNumber The maximum value for clamping bounds.
*
* @throws \Exception
*
* @return int|float
*/
function clamp($number, $minNumber, $maxNumber)
{
if (! is_numeric($number)) {
throw new Exception('Clamp number must be numeric in value.');
}
if (! is_numeric($minNumber)) {
throw new Exception('Clamped minimum number must be numeric in value.');
}
if (! is_numeric($maxNumber)) {
throw new Exception('Clamped maximum number must be numeric in value.');
}
$returnValue = $number;
if ($minNumber >= $number) {
$returnValue = $minNumber;
}
if ($maxNumber <= $number) {
$returnValue = $maxNumber;
}
return $returnValue;
}
}
if (! function_exists('humanBytes')) {
/**
* Convert bytes to a human-friendly format.
@ -195,7 +236,7 @@ if (! function_exists('maybe_unserialize')) {
*
* @param mixed $data
*
* @return mixed
* @return array|string|bool
*/
function maybe_unserialize($data)
{