Permalink
Browse files

Merge pull request #856 from ceeram/2.3-cakenumber

Simplify and improve fromReadableSize().
  • Loading branch information...
markstory committed Sep 18, 2012
2 parents 144761c + ff676b5 commit 5d4a6fd6b625cd22b6185c23d909a0509b4656bf
Showing with 10 additions and 5 deletions.
  1. +1 −0 lib/Cake/Test/Case/Utility/CakeNumberTest.php
  2. +9 −5 lib/Cake/Utility/CakeNumber.php
@@ -555,6 +555,7 @@ public function filesizes() {
array(array('size' => '1KB', 'default' => false), 1024),
array(array('size' => '1.5KB', 'default' => false), 1536),
array(array('size' => '1MB', 'default' => false), 1048576),
+ array(array('size' => '1mb', 'default' => false), 1048576),
array(array('size' => '1.5MB', 'default' => false), 1572864),
array(array('size' => '1GB', 'default' => false), 1073741824),
array(array('size' => '1.5GB', 'default' => false), 1610612736),
@@ -112,15 +112,19 @@ public static function fromReadableSize($size, $default = false) {
if (ctype_digit($size)) {
return $size * 1;
}
- $units = array('KB', 'MB', 'GB', 'TB', 'PB');
- foreach ($units as $i => $unit) {
- if ($unit == substr($size, -2)) {
- return $size * pow(1024, $i + 1);
- }
+ $size = strtoupper($size);
+
+ $i = array_search(substr($size, -2), array('KB', 'MB', 'GB', 'TB', 'PB'));
+ if ($i !== false) {
+ $size = substr($size, 0, strlen($size) -2);
+ return $size * pow(1024, $i + 1);
}
+
if (substr($size, -1) == 'B' && ctype_digit(substr($size, 0, strlen($size) - 1))) {
+ $size = substr($size, 0, strlen($size) - 1);
return $size * 1;
}
+
if ($default !== false) {
return $default;
}

0 comments on commit 5d4a6fd

Please sign in to comment.