Skip to content
Permalink
Browse files

move fromReadableSize to parseFileSize

  • Loading branch information...
antograssiot committed Sep 4, 2014
1 parent 978974e commit 0c0a2aacd53147d441b3c6cab2f3c90d678f7d45
@@ -14,7 +14,6 @@
*/
namespace Cake\I18n;
use Cake\Core\Exception\Exception;
use NumberFormatter;
/**
@@ -74,43 +73,6 @@ public static function toReadableSize($size) {
return __d('cake', '{0,number,#,###.##} TB', $size / 1024 / 1024 / 1024 / 1024);
}
}
/**
* Converts filesize from human readable string to bytes
*
* @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 mixed Number of bytes as integer on success, `$default` on failure if not false
* @throws \Cake\Core\Exception\Exception On invalid Unit type.
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::fromReadableSize
*/
public static function fromReadableSize($size, $default = false) {
if (ctype_digit($size)) {
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, $l);
return $size * pow(1024, $i + 1);
}
if (substr($size, -1) === 'B' && ctype_digit(substr($size, 0, -1))) {
$size = substr($size, 0, -1);
return (int)$size;
}
if ($default !== false) {
return $default;
}
throw new Exception('No unit type.');
}
/**
* Formats a number into a percentage string.
@@ -14,6 +14,8 @@
*/
namespace Cake\Utility;
use Cake\Core\Exception\Exception;
/**
* String handling methods.
*
@@ -714,4 +716,42 @@ public static function ascii(array $array) {
return $ascii;
}
/**
* Converts filesize from human readable string to bytes
*
* @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 mixed Number of bytes as integer on success, `$default` on failure if not false
* @throws \Cake\Core\Exception\Exception On invalid Unit type.
* @link http://book.cakephp.org/3.0/en/core-libraries/helpers/text.html
*/
public static function parseFileSize($size, $default = false) {
if (ctype_digit($size)) {
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, $l);
return $size * pow(1024, $i + 1);
}
if (substr($size, -1) === 'B' && ctype_digit(substr($size, 0, -1))) {
$size = substr($size, 0, -1);
return (int)$size;
}
if ($default !== false) {
return $default;
}
throw new Exception('No unit type.');
}
}
@@ -14,7 +14,7 @@
*/
namespace Cake\Validation;
use Cake\I18n\Number;
use Cake\Utility\String;
use RuntimeException;
/**
@@ -928,7 +928,7 @@ public static function fileSize($check, $operator = null, $size = null) {
}
if (is_string($size)) {
$size = Number::fromReadableSize($size);
$size = String::parseFileSize($size);
}
$filesize = filesize($check);
@@ -494,52 +494,4 @@ public function testReadableSizeLocalized() {
$this->assertEquals('512,05 GB', $result);
}
/**
* testFromReadableSize
*
* @dataProvider filesizes
* @return void
*/
public function testFromReadableSize($params, $expected) {
$result = $this->Number->fromReadableSize($params['size'], $params['default']);
$this->assertEquals($expected, $result);
}
/**
* testFromReadableSize
*
* @expectedException \Cake\Core\Exception\Exception
* @return void
*/
public function testFromReadableSizeException() {
$this->Number->fromReadableSize('bogus', false);
}
/**
* filesizes dataprovider
*
* @return array
*/
public function filesizes() {
return array(
array(array('size' => '512B', 'default' => false), 512),
array(array('size' => '1KB', 'default' => false), 1024),
array(array('size' => '1.5KB', 'default' => false), 1536),
array(array('size' => '1MB', 'default' => false), 1048576),
array(array('size' => '1mb', 'default' => false), 1048576),
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')
);
}
}
@@ -1413,4 +1413,52 @@ public function testAscii() {
$this->assertEquals($expected, $result);
}
/**
* testparseFileSize
*
* @dataProvider filesizes
* @return void
*/
public function testparseFileSize($params, $expected) {
$result = String::parseFileSize($params['size'], $params['default']);
$this->assertEquals($expected, $result);
}
/**
* testFromReadableSize
*
* @expectedException \Cake\Core\Exception\Exception
* @return void
*/
public function testparseFileSizeException() {
String::parseFileSize('bogus', false);
}
/**
* filesizes dataprovider
*
* @return array
*/
public function filesizes() {
return array(
array(array('size' => '512B', 'default' => false), 512),
array(array('size' => '1KB', 'default' => false), 1024),
array(array('size' => '1.5KB', 'default' => false), 1536),
array(array('size' => '1MB', 'default' => false), 1048576),
array(array('size' => '1mb', 'default' => false), 1048576),
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')
);
}
}

0 comments on commit 0c0a2aa

Please sign in to comment.
You can’t perform that action at this time.