adding a clamp function
This commit is contained in:
		| @@ -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) | ||||
|     { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user