Merge pull request #1042 from ADmad/2.3-cakenumber

Added support for php.ini style values
This commit is contained in:
Mark Story 2012-12-22 13:15:38 -08:00
commit f61b052a51
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' => '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')
);

View file

@ -111,26 +111,31 @@ class CakeNumber {
/**
* 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) {