Added support for php.ini style values

This commit is contained in:
ADmad 2012-12-22 01:09:02 +05:30
parent 5f9a371d58
commit a340c19c08
2 changed files with 19 additions and 7 deletions

View file

@ -642,6 +642,13 @@ class CakeNumberTest extends CakeTestCase {
array(array('size' => '1.5MB', 'default' => false), 1572864), array(array('size' => '1.5MB', 'default' => false), 1572864),
array(array('size' => '1GB', 'default' => false), 1073741824), array(array('size' => '1GB', 'default' => false), 1073741824),
array(array('size' => '1.5GB', 'default' => false), 1610612736), 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' => '512', 'default' => 'Unknown type'), 512),
array(array('size' => '2VB', 'default' => 'Unknown type'), 'Unknown type') array(array('size' => '2VB', 'default' => 'Unknown type'), 'Unknown type')
); );

View file

@ -111,26 +111,31 @@ class CakeNumber {
/** /**
* Converts filesize from human readable string to bytes * 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' * @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. * @throws CakeException On invalid Unit type.
*/ */
public static function fromReadableSize($size, $default = false) { public static function fromReadableSize($size, $default = false) {
if (ctype_digit($size)) { if (ctype_digit($size)) {
return $size * 1; return (int)$size;
} }
$size = strtoupper($size); $size = strtoupper($size);
$l = -2;
$i = array_search(substr($size, -2), array('KB', 'MB', 'GB', 'TB', 'PB')); $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) { if ($i !== false) {
$size = substr($size, 0, strlen($size) - 2); $size = substr($size, 0, $l);
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, -1))) {
$size = substr($size, 0, strlen($size) - 1); $size = substr($size, 0, -1);
return $size * 1; return (int)$size;
} }
if ($default !== false) { if ($default !== false) {