From f4774011142c1585c226280273a8d00ff0163cea Mon Sep 17 00:00:00 2001 From: Joshua Ramon Enslin Date: Thu, 28 Jul 2022 11:01:30 +0200 Subject: [PATCH] Add functions for validating longitudes and latitutdes --- exceptions/MDCoordinateOutOfRange.php | 20 ++++++++++++++ src/MD_STD_IN.php | 40 +++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 exceptions/MDCoordinateOutOfRange.php diff --git a/exceptions/MDCoordinateOutOfRange.php b/exceptions/MDCoordinateOutOfRange.php new file mode 100644 index 0000000..b59ea97 --- /dev/null +++ b/exceptions/MDCoordinateOutOfRange.php @@ -0,0 +1,20 @@ +' . $this->getMessage() . ' is not a valid, numeric values.'; + return $errorMsg; + + } +} diff --git a/src/MD_STD_IN.php b/src/MD_STD_IN.php index f530c70..264e41e 100644 --- a/src/MD_STD_IN.php +++ b/src/MD_STD_IN.php @@ -295,6 +295,46 @@ final class MD_STD_IN { } + /** + * Validates a coordinate. + * + * @param string|integer $input Input string. + * + * @return float + */ + public static function validate_longitude(string|int $input):float { + + if (is_string($input)) $output = self::sanitize_float($input); + else $output = $input; + + if ($output < -180 || $output > 180) { + throw new MDCoordinateOutOfRange("Longitude out of range"); + } + + return $output; + + } + + /** + * Validates a coordinate. + * + * @param string|integer $input Input string. + * + * @return float + */ + public static function validate_latitude(string|int $input):float { + + if (is_string($input)) $output = self::sanitize_float($input); + else $output = $input; + + if ($output < -90 || $output > 90) { + throw new MDCoordinateOutOfRange("Latitude out of range"); + } + + return $output; + + } + /** * Validates ISBNs. Empty strings are accepted as well. *