Permalink
Browse files

Added support for php.ini style values

  • Loading branch information...
1 parent 5f9a371 commit a340c19c0830d6ff3675abfa5d5596a87318688f @ADmad ADmad committed Dec 21, 2012
Showing with 19 additions and 7 deletions.
  1. +7 −0 lib/Cake/Test/Case/Utility/CakeNumberTest.php
  2. +12 −7 lib/Cake/Utility/CakeNumber.php
@@ -642,6 +642,13 @@ public function filesizes() {
array(array('size' => '1.5MB', 'default' => false), 1572864),
array(array('size' => '1GB', 'default' => false), 1073741824),
array(array('size' => '1.5GB', 'default' => false), 1610612736),
+ array(array('size' => '1K', 'default' => false), 1024),
+ array(array('size' => '1.5K', 'default' => false), 1536),
+ array(array('size' => '1M', 'default' => false), 1048576),
+ array(array('size' => '1m', 'default' => false), 1048576),
+ array(array('size' => '1.5M', 'default' => false), 1572864),
+ array(array('size' => '1G', 'default' => false), 1073741824),
+ array(array('size' => '1.5G', 'default' => false), 1610612736),
array(array('size' => '512', 'default' => 'Unknown type'), 512),
array(array('size' => '2VB', 'default' => 'Unknown type'), 'Unknown type')
);
@@ -111,26 +111,31 @@ public static function toReadableSize($size) {
/**
* Converts filesize from human readable string to bytes
*
- * @param string $size Size in human readable string like '5MB'
+ * @param string $size Size in human readable string like '5MB', '5M', '500B', '50kb' etc.
* @param mixed $default Value to be returned when invalid size was used, for example 'Unknown type'
- * @return integer Bytes
+ * @return mixed Number of bytes as integer on success, `$default` on failure if not false
* @throws CakeException On invalid Unit type.
*/
public static function fromReadableSize($size, $default = false) {
if (ctype_digit($size)) {
- return $size * 1;
+ return (int)$size;
}
$size = strtoupper($size);
+ $l = -2;
$i = array_search(substr($size, -2), array('KB', 'MB', 'GB', 'TB', 'PB'));
+ if ($i === false) {
+ $l = -1;
+ $i = array_search(substr($size, -1), array('K', 'M', 'G', 'T', 'P'));
+ }
if ($i !== false) {
- $size = substr($size, 0, strlen($size) - 2);
+ $size = substr($size, 0, $l);
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 (substr($size, -1) == 'B' && ctype_digit(substr($size, 0, -1))) {
+ $size = substr($size, 0, -1);
+ return (int)$size;
}
if ($default !== false) {

0 comments on commit a340c19

Please sign in to comment.