Skip to content
This repository
Browse code

add default return value as parameter to use when size can not be det…

…ermined
  • Loading branch information...
commit 9530e68ae62dc9cb49b00e61814165c46009f56e 1 parent 6ec0afc
ceeram authored September 17, 2012
23  lib/Cake/Test/Case/Utility/CakeNumberTest.php
@@ -529,8 +529,8 @@ public function testToPercentage() {
529 529
  * @dataProvider filesizes
530 530
  * @return void
531 531
  */
532  
-	public function testFromReadableSize($size, $expected) {
533  
-		$result = $this->Number->fromReadableSize($size);
  532
+	public function testFromReadableSize($params, $expected) {
  533
+		$result = $this->Number->fromReadableSize($params['size'], $params['default']);
534 534
 		$this->assertEquals($expected, $result);
535 535
 	}
536 536
 
@@ -541,7 +541,7 @@ public function testFromReadableSize($size, $expected) {
541 541
  * @return void
542 542
  */
543 543
 	public function testFromReadableSizeException() {
544  
-		$result = $this->Number->fromReadableSize('bogus');
  544
+		$result = $this->Number->fromReadableSize('bogus', false);
545 545
 	}
546 546
 
547 547
 /**
@@ -551,14 +551,15 @@ public function testFromReadableSizeException() {
551 551
  */
552 552
 	public function filesizes() {
553 553
 		return array(
554  
-			array('512B', 512),
555  
-			array('1KB', 1024),
556  
-			array('1.5KB', 1536),
557  
-			array('1MB', 1048576),
558  
-			array('1.5MB', 1572864),
559  
-			array('1GB', 1073741824),
560  
-			array('1.5GB', 1610612736),
561  
-			array('512', 512),
  554
+			array(array('size' => '512B', 'default' => false), 512),
  555
+			array(array('size' => '1KB', 'default' => false), 1024),
  556
+			array(array('size' => '1.5KB', 'default' => false), 1536),
  557
+			array(array('size' => '1MB', 'default' => false), 1048576),
  558
+			array(array('size' => '1.5MB', 'default' => false), 1572864),
  559
+			array(array('size' => '1GB', 'default' => false), 1073741824),
  560
+			array(array('size' => '1.5GB', 'default' => false), 1610612736),
  561
+			array(array('size' => '512', 'default' => 'Unknown type'), 512),
  562
+			array(array('size' => '2VB', 'default' => 'Unknown type'), 'Unknown type')
562 563
 		);
563 564
 	}
564 565
 
6  lib/Cake/Utility/CakeNumber.php
@@ -105,9 +105,10 @@ public static function toReadableSize($size) {
105 105
  * Converts filesize from human readable string to bytes
106 106
  *
107 107
  * @param string $size Size in human readable string like '5MB'
  108
+ * @param mixed $default Value to be returned when invalid size was used, for example 'Unknown type'
108 109
  * @return integer Bytes
109 110
  */
110  
-	public static function fromReadableSize($size) {
  111
+	public static function fromReadableSize($size, $default = false) {
111 112
 		if (ctype_digit($size)) {
112 113
 			return $size * 1;
113 114
 		}
@@ -120,6 +121,9 @@ public static function fromReadableSize($size) {
120 121
 		if (substr($size, -1) == 'B' && ctype_digit(substr($size, 0, strlen($size) - 1))) {
121 122
 			return $size * 1;
122 123
 		}
  124
+		if ($default !== false) {
  125
+			return $default;
  126
+		}
123 127
 		throw new CakeException(__d('cake_dev', 'No unit type.'));
124 128
 	}
125 129
 

0 notes on commit 9530e68

Please sign in to comment.
Something went wrong with that request. Please try again.