Skip to content

Commit

Permalink
opmitisation of fromReadableSize
Browse files Browse the repository at this point in the history
  • Loading branch information
ceeram committed Sep 18, 2012
1 parent 07c5102 commit 90c32ad
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions lib/Cake/Utility/CakeNumber.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -112,15 +112,16 @@ public static function fromReadableSize($size, $default = false) {
if (ctype_digit($size)) { if (ctype_digit($size)) {
return $size * 1; return $size * 1;
} }
$units = array('KB', 'MB', 'GB', 'TB', 'PB');
foreach ($units as $i => $unit) { $i = array_search(substr($size, -2), array('KB', 'MB', 'GB', 'TB', 'PB'));
if ($unit == substr($size, -2)) { if ($i !== false) {
return $size * pow(1024, $i + 1); return $size * pow(1024, $i + 1);
}
} }

if (substr($size, -1) == 'B' && ctype_digit(substr($size, 0, strlen($size) - 1))) { if (substr($size, -1) == 'B' && ctype_digit(substr($size, 0, strlen($size) - 1))) {
return $size * 1; return $size * 1;
} }

if ($default !== false) { if ($default !== false) {
return $default; return $default;
} }
Expand Down

0 comments on commit 90c32ad

Please sign in to comment.