Browse Source

move fromReadableSize to parseFileSize

antograssiot 11 years ago
parent
commit
0c0a2aacd5

+ 0 - 38
src/I18n/Number.php

@@ -14,7 +14,6 @@
  */
 namespace Cake\I18n;
 
-use Cake\Core\Exception\Exception;
 use NumberFormatter;
 
 /**
@@ -74,43 +73,6 @@ class Number {
 				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.

+ 40 - 0
src/Utility/String.php

@@ -14,6 +14,8 @@
  */
 namespace Cake\Utility;
 
+use Cake\Core\Exception\Exception;
+
 /**
  * String handling methods.
  *
@@ -714,4 +716,42 @@ class String {
 		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.');
+	}
+
+
 }

+ 2 - 2
src/Validation/Validation.php

@@ -14,7 +14,7 @@
  */
 namespace Cake\Validation;
 
-use Cake\I18n\Number;
+use Cake\Utility\String;
 use RuntimeException;
 
 /**
@@ -928,7 +928,7 @@ class Validation {
 		}
 
 		if (is_string($size)) {
-			$size = Number::fromReadableSize($size);
+			$size = String::parseFileSize($size);
 		}
 		$filesize = filesize($check);
 

+ 0 - 48
tests/TestCase/I18n/NumberTest.php

@@ -494,52 +494,4 @@ class NumberTest extends TestCase {
 		$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 - 0
tests/TestCase/Utility/StringTest.php

@@ -1413,4 +1413,52 @@ podeís adquirirla.</span></p>
 		$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')
+		);
+	}
+
 }