diff options
author | Bharat Mediratta <bharat@menalto.com> | 2011-04-23 10:06:17 -0700 |
---|---|---|
committer | Bharat Mediratta <bharat@menalto.com> | 2011-04-23 10:06:17 -0700 |
commit | ce2c6c3ada2854777891a7a8edec66b7b5be72ad (patch) | |
tree | 93ac43ffc99cc29b115a88cf3af6906c0a418108 /modules/gallery | |
parent | 993e49adda302c89ae84f8667fd2169f71b635d4 (diff) |
Add convert_to_human_readable and tests for both. Resolves #1693.
Diffstat (limited to 'modules/gallery')
-rw-r--r-- | modules/gallery/helpers/MY_num.php | 14 | ||||
-rw-r--r-- | modules/gallery/tests/Num_Helper_Test.php | 32 |
2 files changed, 46 insertions, 0 deletions
diff --git a/modules/gallery/helpers/MY_num.php b/modules/gallery/helpers/MY_num.php index 9787044c..842a2ee3 100644 --- a/modules/gallery/helpers/MY_num.php +++ b/modules/gallery/helpers/MY_num.php @@ -37,4 +37,18 @@ class num extends num_Core { return $val; } + + /** + * Convert a size value as accepted by PHP's shorthand to bytes. + * ref: http://us2.php.net/manual/en/function.ini-get.php + * ref: http://us2.php.net/manual/en/faq.using.php#faq.using.shorthandbytes + */ + static function convert_to_human_readable($num) { + foreach (array("G" => 1e9, "M" => 1e6, "K" => 1e3) as $k => $v) { + if ($num > $v) { + $num = round($num / $v) . $k; + } + } + return $num; + } } diff --git a/modules/gallery/tests/Num_Helper_Test.php b/modules/gallery/tests/Num_Helper_Test.php new file mode 100644 index 00000000..a22f9359 --- /dev/null +++ b/modules/gallery/tests/Num_Helper_Test.php @@ -0,0 +1,32 @@ +<?php defined("SYSPATH") or die("No direct script access."); +/** + * Gallery - a web based photo album viewer and editor + * Copyright (C) 2000-2011 Bharat Mediratta + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. + */ +class Num_Helper_Test extends Gallery_Unit_Test_Case { + public function convert_to_bytes_test() { + $this->assert_equal(5 * 1024, num::convert_to_bytes("5K")); + $this->assert_equal(3 * 1024*1024, num::convert_to_bytes("3M")); + $this->assert_equal(4 * 1024*1024*1024, num::convert_to_bytes("4G")); + } + + public function convert_to_human_readable_test() { + $this->assert_equal("6K", num::convert_to_human_readable(5615)); + $this->assert_equal("1M", num::convert_to_human_readable(1205615)); + $this->assert_equal("3G", num::convert_to_human_readable(3091205615)); + } +}
\ No newline at end of file |