Improved datatypes

This commit is contained in:
Filip Znachor 2023-09-23 13:02:00 +02:00
parent 609ad26d24
commit 38a5a1336b

10
api.php
View file

@ -15,7 +15,7 @@ class API {
return $input; return $input;
}, },
"int" => function($input) { "int" => function($input) {
if($input === null) return null; if($input === null || $input === "") return null;
return intval($input); return intval($input);
}, },
"float" => function($input) { "float" => function($input) {
@ -23,7 +23,8 @@ class API {
return floatval($input); return floatval($input);
}, },
"bool" => function($input) { "bool" => function($input) {
return !in_array($input, ["0", "false", null]); if($input == null) return null;
return !in_array($input, ["0", "false"]);
} }
]; ];
$this->data = array_merge($_GET, $_POST); $this->data = array_merge($_GET, $_POST);
@ -50,8 +51,7 @@ class API {
$datatype = trim(array_shift($split2)); $datatype = trim(array_shift($split2));
$default = count($split2) ? trim(implode("=", $split2)) : null; $default = count($split2) ? trim(implode("=", $split2)) : null;
$result[$name] = $data[$name]; $result[$name] = isset($data[$name]) ? $data[$name] : null;
if(!isset($result[$name])) $result[$name] = null;
if($result[$name] === null && isset($default) && $default !== "") $result[$name] = $default; if($result[$name] === null && isset($default) && $default !== "") $result[$name] = $default;
$result[$name] = $this->datatypes[$datatype]($result[$name]); $result[$name] = $this->datatypes[$datatype]($result[$name]);
@ -108,4 +108,4 @@ class API {
die(json_encode($response)); die(json_encode($response));
} }
} }