From 9530e68ae62dc9cb49b00e61814165c46009f56e Mon Sep 17 00:00:00 2001 From: Ceeram Date: Mon, 17 Sep 2012 11:29:13 +0200 Subject: [PATCH] add default return value as parameter to use when size can not be determined --- lib/Cake/Test/Case/Utility/CakeNumberTest.php | 23 ++++++++++--------- lib/Cake/Utility/CakeNumber.php | 6 ++++- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/lib/Cake/Test/Case/Utility/CakeNumberTest.php b/lib/Cake/Test/Case/Utility/CakeNumberTest.php index 25737679f73..c1bf0188c51 100644 --- a/lib/Cake/Test/Case/Utility/CakeNumberTest.php +++ b/lib/Cake/Test/Case/Utility/CakeNumberTest.php @@ -529,8 +529,8 @@ public function testToPercentage() { * @dataProvider filesizes * @return void */ - public function testFromReadableSize($size, $expected) { - $result = $this->Number->fromReadableSize($size); + public function testFromReadableSize($params, $expected) { + $result = $this->Number->fromReadableSize($params['size'], $params['default']); $this->assertEquals($expected, $result); } @@ -541,7 +541,7 @@ public function testFromReadableSize($size, $expected) { * @return void */ public function testFromReadableSizeException() { - $result = $this->Number->fromReadableSize('bogus'); + $result = $this->Number->fromReadableSize('bogus', false); } /** @@ -551,14 +551,15 @@ public function testFromReadableSizeException() { */ public function filesizes() { return array( - array('512B', 512), - array('1KB', 1024), - array('1.5KB', 1536), - array('1MB', 1048576), - array('1.5MB', 1572864), - array('1GB', 1073741824), - array('1.5GB', 1610612736), - array('512', 512), + 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' => '1.5MB', 'default' => false), 1572864), + array(array('size' => '1GB', 'default' => false), 1073741824), + array(array('size' => '1.5GB', 'default' => false), 1610612736), + array(array('size' => '512', 'default' => 'Unknown type'), 512), + array(array('size' => '2VB', 'default' => 'Unknown type'), 'Unknown type') ); } diff --git a/lib/Cake/Utility/CakeNumber.php b/lib/Cake/Utility/CakeNumber.php index d12b4f90a23..db3f51a7006 100644 --- a/lib/Cake/Utility/CakeNumber.php +++ b/lib/Cake/Utility/CakeNumber.php @@ -105,9 +105,10 @@ public static function toReadableSize($size) { * Converts filesize from human readable string to bytes * * @param string $size Size in human readable string like '5MB' + * @param mixed $default Value to be returned when invalid size was used, for example 'Unknown type' * @return integer Bytes */ - public static function fromReadableSize($size) { + public static function fromReadableSize($size, $default = false) { if (ctype_digit($size)) { return $size * 1; } @@ -120,6 +121,9 @@ public static function fromReadableSize($size) { if (substr($size, -1) == 'B' && ctype_digit(substr($size, 0, strlen($size) - 1))) { return $size * 1; } + if ($default !== false) { + return $default; + } throw new CakeException(__d('cake_dev', 'No unit type.')); }