Add validation function for ISBN

This commit is contained in:
Joshua Ramon Enslin 2020-09-30 00:58:58 +02:00 committed by Stefan Rohde-Enslin
parent 0bfd6c3765
commit 087b4a128e

View File

@ -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.");
}
}