From 087b4a128ed3b60dbec0f6008896c0ccefc3b942 Mon Sep 17 00:00:00 2001 From: Joshua Ramon Enslin Date: Wed, 30 Sep 2020 00:58:58 +0200 Subject: [PATCH] Add validation function for ISBN --- MD_STD_IN.php | 52 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 44 insertions(+), 8 deletions(-) diff --git a/MD_STD_IN.php b/MD_STD_IN.php index 6dd6b20..b3dc315 100644 --- a/MD_STD_IN.php +++ b/MD_STD_IN.php @@ -18,7 +18,7 @@ final class MD_STD_IN { * * @return integer */ - final public static function sanitize_id($input):int { + public static function sanitize_id($input):int { $input = \filter_var($input, \FILTER_VALIDATE_INT, [ 'options' => [ @@ -43,7 +43,7 @@ final class MD_STD_IN { * * @return integer */ - final public static function sanitize_id_or_zero($input):int { + public static function sanitize_id_or_zero($input):int { if ($input === "") return 0; @@ -71,7 +71,7 @@ final class MD_STD_IN { * * @return string */ - final public static function sanitize_text($input):string { + public static function sanitize_text($input):string { $output = \filter_var($input, FILTER_SANITIZE_STRING, @@ -96,7 +96,7 @@ final class MD_STD_IN { * * @return string */ - final public static function get_http_input_text(string $var_name, string $default = "", array $allowed = []):string { + public static function get_http_input_text(string $var_name, string $default = "", array $allowed = []):string { if (isset($_GET[$var_name])) { $output = self::sanitize_text($_GET[$var_name]); @@ -124,7 +124,7 @@ final class MD_STD_IN { * * @return string */ - final public static function get_http_post_text(string $var_name, string $default = "", array $allowed = []):string { + public static function get_http_post_text(string $var_name, string $default = "", array $allowed = []):string { if (isset($_POST[$var_name])) { $output = self::sanitize_text($_POST[$var_name]); @@ -146,7 +146,7 @@ final class MD_STD_IN { * * @return string */ - final public static function sanitize_url($input):string { + public static function sanitize_url($input):string { if ($input === "") return ""; @@ -166,7 +166,7 @@ final class MD_STD_IN { * * @return string */ - final public static function sanitize_email($input):string { + public static function sanitize_email($input):string { if ($input === "") return ""; @@ -186,7 +186,7 @@ final class MD_STD_IN { * * @return float */ - final public static function sanitize_float(string $input):float { + public static function sanitize_float(string $input):float { $output = \str_replace(",", ".", $input); if (($output = \filter_var($output, FILTER_VALIDATE_FLOAT)) === false) { @@ -196,4 +196,40 @@ final class MD_STD_IN { } + /** + * Validates ISBNs. Empty strings are accepted as well. + * + * @param string $input Input string. + * + * @return string + */ + public static function validate_isbn(string $input):string { + + if ($input === "") return ""; + + // Remove hyphens + $input = trim(strtr($input, ["-" => "", "–" => ""])); + + // ISBN 10 + if (\mb_strlen($input) === 10) { + + if (\preg_match('/\d{9}[0-9xX]/i', $input)) { + return $input; + } + + } + + // ISBN 13 + if (\mb_strlen($input) === 10) { + + if (\preg_match('/\d{13}/i', $input)) { + return $input; + } + + } + + throw new MDgenericInvalidInputsException("ISBNs must be either 10 or 13 characters long."); + + } + }