diff --git a/core/includes/common.inc b/core/includes/common.inc index 31492ed..805cdea 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -249,23 +249,14 @@ function check_url($uri) { * A translated string representation of the size. */ function format_size($size, $langcode = NULL) { - if ($size < Bytes::KILOBYTE) { - return \Drupal::translation()->formatPlural($size, '1 byte', '@count bytes', array(), array('langcode' => $langcode)); + $val = Bytes::toUnitAndSize($size); + $options = ['langcode' => $langcode]; + if ($val['unit'] == 'B') { + return \Drupal::translation()->formatPlural($val['size'], '1 byte', '@count bytes', [], $options); } else { - $size = $size / Bytes::KILOBYTE; // Convert bytes to kilobytes. - $units = ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; - foreach ($units as $unit) { - if (round($size, 2) >= Bytes::KILOBYTE) { - $size = $size / Bytes::KILOBYTE; - } - else { - break; - } - } - $args = ['@size' => round($size, 2)]; - $options = ['langcode' => $langcode]; - switch ($unit) { + $args = ['@size' => $val['size']]; + switch ($val['unit']) { case 'KB': return new TranslatableMarkup('@size KB', $args, $options); case 'MB': diff --git a/core/lib/Drupal/Component/Utility/Bytes.php b/core/lib/Drupal/Component/Utility/Bytes.php index 4232635..f64d610 100644 --- a/core/lib/Drupal/Component/Utility/Bytes.php +++ b/core/lib/Drupal/Component/Utility/Bytes.php @@ -45,32 +45,54 @@ public static function toInt($size) { } /** - * Returns a string representing a byte size. + * Returns unit of measurement and size of a byte quantity. * - * @param int $size - * An integer representation of the size in bytes. + * @param float $size + * A quantity of bytes. * - * @return string - * A string size expressed as a number of bytes or multiples. + * @return array + * An assocative array with the following keys: + * - 'unit': unit of measurement (string) + * - 'size': size (float, rounded to 2 decimals). */ - public static function toString($size) { - if ($size < Bytes::KILOBYTE) { - return $size . ' B'; + public static function toUnitAndSize($size) { + if ($size < self::KILOBYTE) { + return ['unit' => 'B', 'size' => $size]; } else { - $size = $size / Bytes::KILOBYTE; - $units = ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; - foreach ($units as $unit) { - if (round($size, 2) >= Bytes::KILOBYTE) { - $size = $size / Bytes::KILOBYTE; + $size = $size / self::KILOBYTE; + foreach (['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] as $unit) { + if (round($size, 2) >= self::KILOBYTE) { + $size = $size / self::KILOBYTE; } else { $size = round($size, 2); break; } } - return $size . ' ' . $unit; } + return ['unit' => $unit, 'size' => $size]; + } + + /** + * Returns a string representing a byte quantity. + * + * Note that this function returns a plain string. If you need to get a + * translation aware object representing a size in bytes, use format_size() + * instead. + * + * @param float $size + * A quantity of bytes. + * + * @return string + * A string expressed as a number of bytes or multiples, suffixed with the + * unit of measurement. + * + * @see format_size + */ + public static function toString($size) { + $val = self::toUnitAndSize($size); + return $val['size'] . ' ' . $val['unit']; } } diff --git a/core/lib/Drupal/Component/Utility/Environment.php b/core/lib/Drupal/Component/Utility/Environment.php index a2183cc..d95b433 100644 --- a/core/lib/Drupal/Component/Utility/Environment.php +++ b/core/lib/Drupal/Component/Utility/Environment.php @@ -16,9 +16,9 @@ class Environment { * Compares the memory required for an operation to the total memory. * * @param string $required - * The memory required for the operation, expressed as a number of bytes with - * optional SI or IEC binary unit prefix (e.g. 2, 3K, 5MB, 10G, 6GiB, 8bytes, - * 9mbytes). + * The memory required for the operation, expressed as a number of bytes + * with optional SI or IEC binary unit prefix (e.g. 2, 3K, 5MB, 10G, 6GiB, + * 8bytes, 9mbytes). * @param $memory_limit * (optional) The memory limit for the operation, expressed as a number of * bytes with optional SI or IEC binary unit prefix (e.g. 2, 3K, 5MB, 10G, @@ -46,9 +46,9 @@ public static function checkMemoryLimit($required, $memory_limit = NULL) { * Compares the memory required for an operation to the remaining memory. * * @param string $required - * The memory required for the operation, expressed as a number of bytes with - * optional SI or IEC binary unit prefix (e.g. 2, 3K, 5MB, 10G, 6GiB, 8bytes, - * 9mbytes). + * The memory required for the operation, expressed as a number of bytes + * with optional SI or IEC binary unit prefix (e.g. 2, 3K, 5MB, 10G, 6GiB, + * 8bytes, 9mbytes). * @param string $memory_limit * (optional) The memory limit for the operation, expressed as a number of * bytes with optional SI or IEC binary unit prefix (e.g. 2, 3K, 5MB, 10G, diff --git a/core/tests/Drupal/Tests/Component/Utility/BytesTest.php b/core/tests/Drupal/Tests/Component/Utility/BytesTest.php index ecda509..554c3a0 100644 --- a/core/tests/Drupal/Tests/Component/Utility/BytesTest.php +++ b/core/tests/Drupal/Tests/Component/Utility/BytesTest.php @@ -37,23 +37,6 @@ public function testToInt($size, $expected_int) { } /** - * Tests \Drupal\Component\Utility\Bytes::toString(). - * - * @param int $size - * The value for the size argument for - * \Drupal\Component\Utility\Bytes::toString(). - * @param string $expected_string - * The expected return value from - * \Drupal\Component\Utility\Bytes::toString(). - * - * @dataProvider providerTestToString - * @covers ::toString - */ - public function testToString($size, $expected_string) { - $this->assertEquals($expected_string, Bytes::toString($size)); - } - - /** * Provides data for testToInt. * * @return array @@ -80,28 +63,69 @@ public function providerTestToInt() { } /** - * Provides data for testToString. + * Tests \Drupal\Component\Utility\Bytes::toUnitAndSize(). + * + * @param int $size + * The value for the size argument for + * \Drupal\Component\Utility\Bytes::toUnitAndSize(). + * @param array $expected_unit_and_size + * The expected return value from + * \Drupal\Component\Utility\Bytes::toUnitAndSize(). + * @param string $expected_string + * The expected return value from + * \Drupal\Component\Utility\Bytes::toString(). + * + * @dataProvider providerTestBytes + * @covers ::toUnitAndSize + */ + public function testToUnitAndSize($size, array $expected_unit_and_size, $expected_string) { + $this->assertEquals($expected_unit_and_size, Bytes::toUnitAndSize($size)); + } + + /** + * Tests \Drupal\Component\Utility\Bytes::toString(). + * + * @param int $size + * The value for the size argument for + * \Drupal\Component\Utility\Bytes::toString(). + * @param array $expected_unit_and_size + * The expected return value from + * \Drupal\Component\Utility\Bytes::toUnitAndSize(). + * @param string $expected_string + * The expected return value from + * \Drupal\Component\Utility\Bytes::toString(). + * + * @dataProvider providerTestBytes + * @covers ::toString + */ + public function testToString($size, array $expected_unit_and_size, $expected_string) { + $this->assertEquals($expected_string, Bytes::toString($size)); + } + + /** + * Provides data for ::testToUnitAndSize and ::testToString. * * @return array[] * An array of arrays, each containing the argument for - * \Drupal\Component\Utility\Bytes::toString(): size, and the expected return - * value. + * \Drupal\Component\Utility\Bytes::toUnitAndSize() and + * \Drupal\Component\Utility\Bytes::toString(), and the expected return + * value from each method. */ - public function providerTestToString() { + public function providerTestBytes() { return [ - [1, '1 B'], - [768, '768 B'], - [Bytes::KILOBYTE, '1 KB'], - [pow(Bytes::KILOBYTE, 2), '1 MB'], - [pow(Bytes::KILOBYTE, 3), '1 GB'], - [pow(Bytes::KILOBYTE, 4), '1 TB'], - [pow(Bytes::KILOBYTE, 5), '1 PB'], - [pow(Bytes::KILOBYTE, 6), '1 EB'], - [pow(Bytes::KILOBYTE, 7), '1 ZB'], - [pow(Bytes::KILOBYTE, 8), '1 YB'], - [23476892, '22.39 MB'], - [79691776, '76 MB'], - [81862076662, '76.24 GB'], + [1, ['unit' => 'B', 'size' => 1], '1 B'], + [768, ['unit' => 'B', 'size' => 768], '768 B'], + [Bytes::KILOBYTE, ['unit' => 'KB', 'size' => 1], '1 KB'], + [pow(Bytes::KILOBYTE, 2), ['unit' => 'MB', 'size' => 1], '1 MB'], + [pow(Bytes::KILOBYTE, 3), ['unit' => 'GB', 'size' => 1], '1 GB'], + [pow(Bytes::KILOBYTE, 4), ['unit' => 'TB', 'size' => 1], '1 TB'], + [pow(Bytes::KILOBYTE, 5), ['unit' => 'PB', 'size' => 1], '1 PB'], + [pow(Bytes::KILOBYTE, 6), ['unit' => 'EB', 'size' => 1], '1 EB'], + [pow(Bytes::KILOBYTE, 7), ['unit' => 'ZB', 'size' => 1], '1 ZB'], + [pow(Bytes::KILOBYTE, 8), ['unit' => 'YB', 'size' => 1], '1 YB'], + [23476892, ['unit' => 'MB', 'size' => 22.39], '22.39 MB'], + [79691776, ['unit' => 'MB', 'size' => 76], '76 MB'], + [81862076662, ['unit' => 'GB', 'size' => 76.24], '76.24 GB'], ]; } } diff --git a/core/tests/Drupal/Tests/Component/Utility/EnvironmentTest.php b/core/tests/Drupal/Tests/Component/Utility/EnvironmentTest.php index 16a5f87..46e2d1f 100644 --- a/core/tests/Drupal/Tests/Component/Utility/EnvironmentTest.php +++ b/core/tests/Drupal/Tests/Component/Utility/EnvironmentTest.php @@ -7,7 +7,6 @@ namespace Drupal\Tests\Component\Utility; -use Drupal\Component\Utility\Bytes; use Drupal\Component\Utility\Environment; use Drupal\Tests\UnitTestCase;