diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 460fb54da9..28d7d76de5 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -37,6 +37,7 @@ Yii Framework 2 Change Log - Bug #19655: Fix `LinkPager::getPageRange` when `maxButtons` is 2 (max-s-lab) - Enh #20539: Update minimum PHP version requirement from `7.3` to `7.4` (terabytesoftw) - Bug #20541: Remove deprecated caching components: `XCache` and `ZendDataCache`, and update related tests and documentation (terabytesoftw) +- Enh #19526: Add the `convertIniSizeToBytes` method to `BaseStringHelper` (max-s-lab) 2.0.53 June 27, 2025 diff --git a/framework/helpers/BaseStringHelper.php b/framework/helpers/BaseStringHelper.php index 5854e29766..a1ed907e98 100644 --- a/framework/helpers/BaseStringHelper.php +++ b/framework/helpers/BaseStringHelper.php @@ -52,6 +52,30 @@ class BaseStringHelper return mb_substr((string)$string, $start, $length, '8bit'); } + /** + * Converts php.ini style size to bytes. + * + * @param string $string php.ini style size. Examples: `512M`, `1024K`, `1G`, `256`. + * @return int the number of bytes equivalent to the specified string. + * @since 2.0.54 + */ + public static function convertIniSizeToBytes($string) + { + switch (substr($string, -1)) { + case 'M': + case 'm': + return (int) $string * 1048576; + case 'K': + case 'k': + return (int) $string * 1024; + case 'G': + case 'g': + return (int) $string * 1073741824; + default: + return (int) $string; + } + } + /** * Returns the trailing name component of a path. * This method is similar to the php function `basename()` except that it will diff --git a/framework/validators/FileValidator.php b/framework/validators/FileValidator.php index 28a2df0fa3..4d5a5dfd26 100644 --- a/framework/validators/FileValidator.php +++ b/framework/validators/FileValidator.php @@ -325,8 +325,8 @@ class FileValidator extends Validator public function getSizeLimit() { // Get the lowest between post_max_size and upload_max_filesize, log a warning if the first is < than the latter - $limit = $this->sizeToBytes(ini_get('upload_max_filesize')); - $postLimit = $this->sizeToBytes(ini_get('post_max_size')); + $limit = StringHelper::convertIniSizeToBytes(ini_get('upload_max_filesize')); + $postLimit = StringHelper::convertIniSizeToBytes(ini_get('post_max_size')); if ($postLimit > 0 && $postLimit < $limit) { Yii::warning('PHP.ini\'s \'post_max_size\' is less than \'upload_max_filesize\'.', __METHOD__); $limit = $postLimit; @@ -351,29 +351,6 @@ class FileValidator extends Validator return !($value instanceof UploadedFile) || $value->error == UPLOAD_ERR_NO_FILE; } - /** - * Converts php.ini style size to bytes. - * - * @param string $sizeStr $sizeStr - * @return int - */ - private function sizeToBytes($sizeStr) - { - switch (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; - } - } - /** * Checks if given uploaded file have correct type (extension) according current validator settings. * @param UploadedFile $file diff --git a/tests/framework/helpers/StringHelperTest.php b/tests/framework/helpers/StringHelperTest.php index 5f222ec4e7..8c28a4d561 100644 --- a/tests/framework/helpers/StringHelperTest.php +++ b/tests/framework/helpers/StringHelperTest.php @@ -536,4 +536,25 @@ class StringHelperTest extends TestCase ['من صالح هاشمی هستم', 'من ', ' هستم', 'صالح هاشمی'], // other languages ]; } + + /** + * @dataProvider provideConvertIniSizeToBytesData + */ + public function testConvertIniSizeToBytes(string $string, int $expectedResult): void + { + $this->assertEquals($expectedResult, StringHelper::convertIniSizeToBytes($string)); + } + + public static function provideConvertIniSizeToBytesData(): array + { + return [ + ['1024', 1024], + ['512K', 524288], + ['512k', 524288], + ['128M', 134217728], + ['128m', 134217728], + ['2G', 2147483648], + ['2g', 2147483648], + ]; + } } diff --git a/tests/framework/validators/FileValidatorTest.php b/tests/framework/validators/FileValidatorTest.php index 9e05edf4d2..0009db6dbf 100644 --- a/tests/framework/validators/FileValidatorTest.php +++ b/tests/framework/validators/FileValidatorTest.php @@ -9,6 +9,7 @@ namespace yiiunit\framework\validators; use Yii; use yii\helpers\FileHelper; +use yii\helpers\StringHelper; use yii\validators\FileValidator; use yii\web\UploadedFile; use yiiunit\data\validators\models\FakedValidationModel; @@ -78,7 +79,11 @@ class FileValidatorTest extends TestCase public function testGetSizeLimit() { - $size = min($this->sizeToBytes(ini_get('upload_max_filesize')), $this->sizeToBytes(ini_get('post_max_size'))); + $size = min( + StringHelper::convertIniSizeToBytes(ini_get('upload_max_filesize')), + StringHelper::convertIniSizeToBytes(ini_get('post_max_size')) + ); + $val = new FileValidator(); $this->assertEquals($size, $val->getSizeLimit()); $val->maxSize = $size + 1; // set and test if value is overridden @@ -91,23 +96,6 @@ class FileValidatorTest extends TestCase $this->assertSame($_POST['MAX_FILE_SIZE'], $val->getSizeLimit()); } - protected function sizeToBytes($sizeStr) - { - switch (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; - } - } - public function testValidateAttributeMultiple() { $val = new FileValidator([ @@ -370,7 +358,7 @@ class FileValidatorTest extends TestCase } else { $size = isset($param['size']) ? $param['size'] : random_int( 1, - $this->sizeToBytes(ini_get('upload_max_filesize')) + StringHelper::convertIniSizeToBytes(ini_get('upload_max_filesize')) ); } $type = isset($param['type']) ? $param['type'] : 'text/plain';