diff --git a/dblayer.php b/dblayer.php index 6c27471..d08f81a 100644 --- a/dblayer.php +++ b/dblayer.php @@ -206,15 +206,60 @@ class dblayer extends PDO if (in_array ("not null", $params) && !array_key_exists ($key, $datas)) throw new Exception (sprintf (_("Mandatory field '%s' not provided"), $key), 405); + if (in_array ("not null", $params) && $datas[$key] === "") + throw new Exception (sprintf (_("Mandatory field '%s' is empty"), + $key), 405); if (!array_key_exists ($key, $datas)) continue; // Verify the fields, if $verify is defined, before doing insertion $verify = $this->verifyOne ($key, $datas[$key]); if (is_array ($verify) && count ($verify)) throw new Exception ($verify[0]." ".$verify[1]." in ".$key); - // TODO : Check for type inconsistencies before create $datasOK - if (array_key_exists ($key, $datas)) - $datasOK[$key] = $datas[$key]; + // Check for type inconsistencies if the value is provided + if ($datas[$key] !== "" && $params[0] === "integer") + { + if (strspn ($datas[$key], "0123456789") !== strlen ($datas[$key])) + throw new Exception (sprintf ( + _("Errors in consistency : '%s' is not an integer"), + $key), 405); + } + elseif ($datas[$key] !== "" && $params[0] === "varchar") + { + if (! isset ($params[1])) + throw new Exception (sprintf ( + _("The length of varchar field '%s' is not provided"), + $key), 500); + if (strlen ($datas[$key]) > $params[1]) + throw new Exception (sprintf ( + _("Errors in consistency : '%s' data is too long"), + $key), 405); + } + elseif ($datas[$key] !== "" && $params[0] === "datetime") + { + // The date format must be in ANSI SQL : YYYY-MM-DD HH:MM:SS + $d = DateTime::createFromFormat("Y-m-d H:i:s", $datas[$key]); + if (!$d || $d->format("Y-m-d H:i:s") !== $datas[$key]) + throw new Exception (sprintf ( + _("Incorrect datetime provided for field '%s'"), + $key), 500); + } + elseif ($datas[$key] !== "" && $params[0] === "date") + { + // The date format must be in ANSI SQL : YYYY-MM-DD + $d = DateTime::createFromFormat("Y-m-d", $datas[$key]); + if (!$d || $d->format("Y-m-d") !== $datas[$key]) + throw new Exception (sprintf ( + _("Incorrect date provided for field '%s'"), + $key), 500); + } + elseif ($datas[$key] !== "") + throw new Exception (sprintf (_("Unknown field type for '%s'"), $key), + 500); + else + { + // Nothing to do if the value is empty : just save it + } + $datasOK[$key] = $datas[$key]; } // Check for inconsistency