Add can_convert_to_integer and to_integer functions.
authorFlorent Bruneau <florent.bruneau@polytechnique.org>
Mon, 1 Nov 2010 10:50:33 +0000 (11:50 +0100)
committerFlorent Bruneau <florent.bruneau@polytechnique.org>
Mon, 1 Nov 2010 10:50:33 +0000 (11:50 +0100)
Signed-off-by: Florent Bruneau <florent.bruneau@polytechnique.org>
classes/env.php
classes/pldict.php
classes/s.php
include/misc.inc.php

index cecee84..7a2144f 100644 (file)
@@ -67,8 +67,8 @@ class Env
 
     public static function i($key, $default = 0)
     {
-        $i = Env::_get($key, $default);
-        return is_numeric($i) ? intval($i) : $default;
+        $i = to_integer(Env::_get($key, $default));
+        return $i === false ? $default : $i;
     }
 
     public static function l(array $keys)
@@ -125,8 +125,8 @@ class Post
 
     public static function i($key, $default = 0)
     {
-        $i = Post::_get($key, $default);
-        return is_numeric($i) ? intval($i) : $default;
+        $i = to_integer(Post::_get($key, $default));
+        return $i === false ? $default : $i;
     }
 
      public static function l(array $keys)
@@ -183,8 +183,8 @@ class Get
 
     public static function i($key, $default = 0)
     {
-        $i = Get::_get($key, $default);
-        return is_numeric($i) ? intval($i) : $default;
+        $i = to_integer(Get::_get($key, $default));
+        return $i === false ? $default : $i;
     }
 
     public static function l(array $keys)
@@ -258,8 +258,8 @@ class Cookie
 
     public static function i($key, $default = 0)
     {
-        $i = Cookie::_get($key, $default);
-        return is_numeric($i) ? intval($i) : $default;
+        $i = to_integer(Cookie::_get($key, $default));
+        return $i === false ? $default : $i;
     }
 
     public static function l(array $keys)
index b67095a..21a911b 100644 (file)
@@ -79,8 +79,8 @@ class PlDict
 
     public function i($key, $default = 0)
     {
-        $i = $this->_get($key, $default);
-        return (is_int($i) || ctype_digit($i)) ? intval($i) : $default;
+        $i = to_integer($this->_get($key, $default));
+        return $i === false ? $default : $i;
     }
 
     public function l(array $keys)
index d436bf1..456736f 100644 (file)
@@ -56,8 +56,8 @@ class S
 
     public static function i($key, $default = 0)
     {
-        $i = S::v($key, $default);
-        return is_numeric($i) ? intval($i) : $default;
+        $i = to_integer(S::v($key, $default));
+        return $i === false ? $default : $i;
     }
 
     public static function t($key, $default = '')
index e70e6d7..ad37a92 100644 (file)
@@ -380,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:
 ?>