Add functions for validating longitudes and latitutdes
This commit is contained in:
parent
c362aa1283
commit
f477401114
20
exceptions/MDCoordinateOutOfRange.php
Normal file
20
exceptions/MDCoordinateOutOfRange.php
Normal file
@ -0,0 +1,20 @@
|
||||
<?PHP
|
||||
declare(strict_types = 1);
|
||||
|
||||
/**
|
||||
* Custom exception class for coordinates (longitude, latitude) that are out
|
||||
* of range (-90 - 90 for latitudes, -180 to 180 for longitudes).
|
||||
*/
|
||||
final class MDCoordinateOutOfRange extends MDExpectedException {
|
||||
/**
|
||||
* Error message.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function errorMessage() {
|
||||
//error message
|
||||
$errorMsg = 'Parameter: <b>' . $this->getMessage() . '</b> is not a valid, numeric values.';
|
||||
return $errorMsg;
|
||||
|
||||
}
|
||||
}
|
@ -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.
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user