for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
declare(strict_types=1);
namespace Longman\LaravelLodash\SelfDiagnosis;
use function floor;
use function pow;
use function sprintf;
use function strlen;
use function strtolower;
use function trim;
trait ParsesNumValues
{
private function toBytes(string $value): int
$value = trim($value);
$last = strtolower($value[strlen($value) - 1]);
$value = (int) $value;
switch ($last) {
case 'g':
$value *= 1024;
// no break
case 'm':
case 'k':
}
return $value;
private function fromBytes(int $bytes, int $decimals = 2): string
$size = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
$factor = (int) floor((strlen((string) $bytes) - 1) / 3);
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . $size[$factor];