Skip to content

Commit

Permalink
move fromReadableSize to parseFileSize
Browse files Browse the repository at this point in the history
  • Loading branch information
antograssiot committed Sep 4, 2014
1 parent 978974e commit 0c0a2aa
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 88 deletions.
38 changes: 0 additions & 38 deletions src/I18n/Number.php
Expand Up @@ -14,7 +14,6 @@
*/
namespace Cake\I18n;

use Cake\Core\Exception\Exception;
use NumberFormatter;

/**
Expand Down Expand Up @@ -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.
Expand Down
40 changes: 40 additions & 0 deletions src/Utility/String.php
Expand Up @@ -14,6 +14,8 @@
*/
namespace Cake\Utility;

use Cake\Core\Exception\Exception;

/**
* String handling methods.
*
Expand Down Expand Up @@ -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.');
}


}
4 changes: 2 additions & 2 deletions src/Validation/Validation.php
Expand Up @@ -14,7 +14,7 @@
*/
namespace Cake\Validation;

use Cake\I18n\Number;
use Cake\Utility\String;
use RuntimeException;

/**
Expand Down Expand Up @@ -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);

Expand Down
48 changes: 0 additions & 48 deletions tests/TestCase/I18n/NumberTest.php
Expand Up @@ -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')
);
}

}
48 changes: 48 additions & 0 deletions tests/TestCase/Utility/StringTest.php
Expand Up @@ -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.