Skip to content

Commit

Permalink
readableMemory to formatMemory
Browse files Browse the repository at this point in the history
  • Loading branch information
stepanenko3 committed Aug 27, 2022
1 parent 37e7954 commit 541faa5
Showing 1 changed file with 16 additions and 20 deletions.
36 changes: 16 additions & 20 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,23 @@ function rglob($pattern)
}
}

if (!function_exists('readableMemory')) {
/**
* readableMemory
*
* @param mixed $memory
* @param bool $startFromBytes
* @param bool $withUnit
* @return mixed
*/
function readableMemory($memory, $startFromBytes = false, $withUnit = true)
if (!function_exists('formatMemory')) {
function formatMemory(float $size, int $level = 0, int $precision = 2, int $base = 1024, $asArray = false): string
{
$i = floor(log($memory) / log(1024));
$sizes = ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
if ($startFromBytes) array_unshift($sizes, 'B');

if ($withUnit)
return sprintf('%.02F', $memory / pow(1024, $i)) * 1 . ' ' . $sizes[$i];
else return [
'memory' => sprintf('%.02F', $memory / pow(1024, $i)) * 1,
'unit' => $sizes[$i],
];
$unit = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
$times = floor(log($size, $base));

$memory = sprintf('%.' . $precision . 'f', $size / pow($base, ($times + $level)));
$unit = $unit[$times + $level];

if ($asArray) {
return [
'memory' => $memory,
'unit' => $unit,
];
}

return $memory . ' ' . $unit;
}
}

Expand Down

0 comments on commit 541faa5

Please sign in to comment.