Enabler PHP natives assertion in debug and use Platal::assert() as a
[platal.git] / include / misc.inc.php
index bfa5163..b60045d 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2010 Polytechnique.org                              *
+ *  Copyright (C) 2003-2011 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -204,7 +204,18 @@ function make_datetime($date)
         return new DateTime("@$date");
     } else {
         try {
+            $list = explode('/', $date);
+            if (count($list) == 3) {
+                $date = $list[1] . '/' . $list[0] . '/' . $list[2];
+            }
+            // FIXME: On PHP < 5.3, parsing error are reported using an error,
+            //        not an exception. Thus count the number of error to detect
+            //        errors.
+            $errors = @count($GLOBALS['pl_errors']);
             $d = new DateTime($date);
+            if (@count($GLOBALS['pl_errors']) > $errors) {
+                return null;
+            }
             return $d;
         } catch (Exception $e) {
             return null;
@@ -369,6 +380,23 @@ function ends_with($string, $suffix, $caseSensitive = true)
     }
 }
 
+/** Check if the input data can be seen as an integer.
+ */
+function can_convert_to_integer($data)
+{
+    return is_int($data) || ctype_digit($data);
+}
+
+/** Interpret the input data as an integer or return false.
+ */
+function to_integer($data)
+{
+    if (!can_convert_to_integer($data)) {
+        return false;
+    }
+    return intval($data);
+}
+
 
 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
 ?>