<?php

namespace GriffinForms\Admin;

use \GriffinForms\Traits\Singleton;

class Units extends \GriffinForms\Admin\Format
{
    use Singleton;
    
    /**
     * Converts bytes to a human-readable format (Bytes, KB, MB, GB, TB).
     *
     * @param int|float $bytes The size in bytes.
     * @return string Human-readable size string.
     */
    public function formatBytes($bytes)
    {
        $units = array('Bytes', 'KB', 'MB', 'GB', 'TB');
        if (!is_numeric($bytes) || $bytes < 0) {
            return '0 Bytes';
        }
        if ($bytes == 0) {
            return '0 Bytes';
        }
        $power = intval(floor(($bytes > 0 ? log($bytes) : 0) / log(1024)));
        $power = min($power, count($units) - 1);
        $value = $bytes / pow(1024, $power);
        return round($value, 2) . ' ' . $units[$power];
    }
}