open-southeners/byte-unit-converter
PHP 8.1+ utility to convert byte sizes between multiple units with no dependencies. Inspired by macOS ByteCountFormatter, it helps format and convert storage values consistently for apps and libraries.
Architecture Fit
The package’s immutable design and arithmetic operations (add, sub, subtract) align seamlessly with Laravel’s functional paradigms (e.g., Collection immutability, service-layer purity). Its enum-based metric systems (MetricSystem, ByteUnit) integrate cleanly with Laravel’s typed APIs (PHP 8.1+). The BCMath dependency for long-number support is a non-issue in Laravel, as BCMath is commonly enabled for financial/precision use cases.
Integration Feasibility
asRound now accepts int|bool (default: 2 decimal places). Requires:
assertEquals(1.87, $converter->asRound()->toKiB()) → assertEquals(2, $converter->asRound(2)->toKiB())).app('byte-converter')), update bindings to resolve new class methods.ByteUnitConverter::new($bytes)->toGB()).Technical Risk
asRound(2) may truncate values like 1.9999 to 2 (rounding up), conflicting with business rules (e.g., "round half up").asRound(false) or implement a custom rounding strategy (e.g., asRound(RoundingMode::HALF_UP)).add/sub) create new objects, which could impact high-frequency loops (e.g., batch processing).BCMath direct calls) if needed.1e18 bytes).extension=bcmath in php.ini; fall back to GMP if required.Key Questions
asRound(4) for scientific data)?formatBytes() helpers) or augment it?asRound/to* methods? If not, prioritize edge cases (e.g., 0.999 → 1, 1.001 → 1).StorageService dependency)?Stack Fit
$this->app->bind(ByteUnitConverter::class, function () {
return ByteUnitConverter::new()->usingBytes();
});
ByteConverter facade for convenience (e.g., ByteConverter::toGB($bytes)).helpers.php macro for common conversions:
if (!function_exists('formatBytes')) {
function formatBytes($bytes, $precision = 2): string {
return (string) ByteUnitConverter::new($bytes)->asRound($precision)->nearestUnit();
}
}
int|bool) in asRound for type safety.MetricSystem, ByteUnit) in Laravel’s typed APIs (e.g., DTOs, request validation).Migration Path
Phase 1: Evaluation (1–2 days)
1.999 → 2, 1.001 → 1).add/sub in loops.Phase 2: Integration (3–5 days)
asRound calls to use int precision (e.g., asRound(2)).ByteUnitConverter::conversion() with instance methods (e.g., ByteUnitConverter::new($bytes)->toGB()).if (method_exists(ByteUnitConverter::class, 'new')) {
$converter = ByteUnitConverter::new($bytes);
} else {
$converter = ByteUnitConverter::from($bytes); // Legacy fallback
}
to* methods (e.g., toKiB, toTB).add, sub).0.999, 1.001).Phase 3: Rollout (1 week)
config('byte_converter.enabled')).asRound(int|bool)).Compatibility
symfony/options-resolver).Sequencing
add/sub) until immutability is fully adopted.Maintenance
asRound refactor requires ongoing maintenance for legacy code.RoundingMode::HALF_UP).Support
$converter->add($bytes)->sub($bytes)->toGB()).asRound calls with precision values to trace discrepancies.Scaling
php.ini for long-number support (e.g., memory_limit=2G if processing large files).Failure Modes
| Scenario | Impact | Mitigation |
|---|---|---|
| BCMath Disabled | Long-number conversions fail | Fall back to GMP or error handling. |
asRound Misuse |
Unexpected rounding (e.g., 1.999 → 2) |
Document defaults; use asRound(false) for custom logic. |
| Immutable Overhead | Performance lag in loops | Cache results or use mutable alternatives. |
| Breaking Change | Legacy code fails | Gradual rollout with deprecation warnings. |
How can I help you explore Laravel packages today?