BytesFormatter.php 682 B

123456789101112131415161718192021222324252627
  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\Migrations\Tools;
  4. use function floor;
  5. use function log;
  6. use function pow;
  7. use function round;
  8. /**
  9. * The BytesFormatter class is responsible for converting a bytes integer to a more human readable string.
  10. * This class is used to format the memory used for display purposes when executing migrations.
  11. *
  12. * @internal
  13. */
  14. final class BytesFormatter
  15. {
  16. public static function formatBytes(float $size, int $precision = 2): string
  17. {
  18. $base = log($size, 1024);
  19. $suffixes = ['', 'K', 'M', 'G', 'T'];
  20. return round(pow(1024, $base - floor($base)), $precision) . $suffixes[(int) floor($base)];
  21. }
  22. }