public static function byteFormat(
        $value,
        $magnitude = -1,
        $iec_units = false,
        $significant_digits = 3
    ) {
        if ($iec_units) {
            $units = array(
                60 => 'EiB',
                50 => 'PiB',
                40 => 'TiB',
                30 => 'GiB',
                20 => 'MiB',
                10 => 'KiB',
                0 => 'bytes'
            );
        } else {
            $units = array(
                60 => 'EB',
                50 => 'PB',
                40 => 'TB',
                30 => 'GB',
                20 => 'MB',
                10 => 'KB',
                0 => 'bytes'
            );
        }

        $unit_magnitude = null;

        if ($magnitude >= 0) {
            $magnitude = intval(round($magnitude / 10) * 10);
            if (array_key_exists($magnitude, $units)) {
                $unit_magnitude = $magnitude;
            } else {
                $unit_magnitude = reset($units); // default magnitude
            }
        } else {
            if ($value == 0) {
                $unit_magnitude = 0;
            } else {
                $log = floor(log10($value) / log10(2)); // get log2()

                $unit_magnitude = reset($units); // default magnitude
                foreach ($units as $magnitude => $title) {
                    if ($log >= $magnitude) {
                        $unit_magnitude = $magnitude;
                        break;
                    }
                }
            }
        }

        $value = $value / pow(2, $unit_magnitude);

        if ($unit_magnitude == 0) {
            // 'bytes' are always formatted as integers
            $formatted_value = self::numberFormat($value, 0);
        } else {
            if ($significant_digits !== null) {
                // round to number of significant digits
                $integer_digits = floor(log10($value)) + 1;

                $fractional_digits = max(
                    $significant_digits - $integer_digits,
                    0
                );

                $formatted_value = self::numberFormat(
                    $value,
                    $fractional_digits
                );
            } else {
                // just round to one fractional digit
                $formatted_value = self::numberFormat($value, 1);
            }
        }

        return $formatted_value . ' ' . $units[$unit_magnitude];
    }