Skip to content

Commit

Permalink
Merge pull request #939 from resurtm/fix-938
Browse files Browse the repository at this point in the history
Fix #938, solution №2.
  • Loading branch information
qiangxue committed Jul 8, 2012
2 parents 834471b + cc2faf5 commit b296020
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Expand Up @@ -72,6 +72,7 @@ Version 1.1.11 work in progress
- Enh #766: Added 'userId' to $params in CDbAuthManager::checkAccess() and CPhpAuthManager::checkAccess() (cebe)
- Enh #666: Added property $off to CValidator, a list of scenarios that the validator should not be applied to (resurtm)
- Enh #839: CListView::renderItems now resolves view file only once (nizsheanez)
- Enh #938: CFileValidator::sizeToBytes() is now public and available for using in the whole application (resurtm)
- Enh: Fixed romanian translation to use the better-supported cedilla characters (tudorilisoi)
- Enh: Added default value to CConsoleCommand::confirm (musterknabe)
- Enh: Allowed returning integer values as application exit code in CConsoleCommand actions (cebe)
Expand Down
24 changes: 15 additions & 9 deletions framework/validators/CFileValidator.php
Expand Up @@ -233,19 +233,25 @@ protected function getSizeLimit()
}

/**
* Converts php.ini style size to bytes
* Converts php.ini style size to bytes. Examples of size strings are: 150, 1g, 500k, 5M (size suffix
* is case insensitive). If you pass here the number with a fractional part, then everything after
* the decimal point will be ignored (php.ini values common behavior). For example 1.5G value would be
* treated as 1G and 1073741824 number will be returned as a result. This method is public
* (was private before) since 1.1.11.
*
* @param string $sizeStr $sizeStr
* @return int
* @param string $sizeStr the size string to convert.
* @return int the byte count in the given size string.
* @since 1.1.11
*/
private function sizeToBytes($sizeStr)
public function sizeToBytes($sizeStr)
{
switch (substr($sizeStr, -1))
// get the latest character
switch (strtolower(substr($sizeStr, -1)))
{
case 'M': case 'm': return (int)$sizeStr * 1048576;
case 'K': case 'k': return (int)$sizeStr * 1024;
case 'G': case 'g': return (int)$sizeStr * 1073741824;
default: return (int)$sizeStr;
case 'm': return (int)$sizeStr * 1048576; // 1024 * 1024
case 'k': return (int)$sizeStr * 1024; // 1024
case 'g': return (int)$sizeStr * 1073741824; // 1024 * 1024 * 1024
default: return (int)$sizeStr; // do nothing
}
}
}
38 changes: 38 additions & 0 deletions tests/framework/validators/CFileValidatorTest.php
@@ -0,0 +1,38 @@
<?php

class CFileValidatorTest extends CTestCase
{
public function providerSizeToBytes()
{
return array(
array('100M', 100*1024*1024),
array('100,5M', 100*1024*1024),
array('150m', 150*1024*1024),
array('150.5m', 150*1024*1024),
array('500K', 500*1024),
array('540.5K', 540*1024),
array('70k', 70*1024),
array('70,2k', 70*1024),
array('1G', 1*1024*1024*1024),
array('1.5g', 1*1024*1024*1024),
array('2g', 2*1024*1024*1024),
array('1.2G', 1*1024*1024*1024),
array('100500', 100500),
array('9000', 9000),
array(null, null),
array('', null),
);
}

/**
* @dataProvider providerSizeToBytes
*
* @param string $sizeString
* @param integer $assertion
*/
public function testSizeToBytes($sizeString, $assertion)
{
$fileValidator=new CFileValidator();
$this->assertEquals($assertion, $fileValidator->sizeToBytes($sizeString));
}
}

0 comments on commit b296020

Please sign in to comment.