Browse Source

Make Hash::numeric() accept more numeric things.

Negative numbers and other stringy forms of numbers should be accepted.
The name Hash::numeric implies is_numeric which it now uses.

Fixes #2478
mark_story 12 years ago
parent
commit
79701af501
2 changed files with 4 additions and 3 deletions
  1. 3 0
      lib/Cake/Test/Case/Utility/HashTest.php
  2. 1 3
      lib/Cake/Utility/Hash.php

+ 3 - 0
lib/Cake/Test/Case/Utility/HashTest.php

@@ -665,6 +665,9 @@ class HashTest extends CakeTestCase {
 
 		$data = array('one', 2 => 'two', 3 => 'three', 4 => 'four', 'a' => 'five');
 		$this->assertFalse(Hash::numeric(array_keys($data)));
+
+		$data = array(2.4, 1, 0, -1, -2);
+		$this->assertTrue(Hash::numeric($data));
 	}
 
 /**

+ 1 - 3
lib/Cake/Utility/Hash.php

@@ -626,9 +626,7 @@ class Hash {
 		if (empty($data)) {
 			return false;
 		}
-		$values = array_values($data);
-		$str = implode('', $values);
-		return (bool)ctype_digit($str);
+		return $data === array_filter($data, 'is_numeric');
 	}
 
 /**