coduo/php-humanizer
Humanize and format values for people: turn field names into readable labels, truncate plain text or HTML safely to word boundaries, and handle common string transformations. Lightweight PHP utility with simple static APIs.
Laravel Compatibility: The package is highly compatible with Laravel due to:
Use Cases:
user_id → "User ID") for admin panels or APIs.1048576 → "1 MB"), dates (e.g., "2 days ago"), or collections (e.g., "Alice, Bob, and 3 others").size → "500 KB" instead of 500000).Anti-Patterns:
composer require coduo/php-humanizer).app()->make(StringHumanizer::class)).translation component (already in Laravel via symfony/translation).| Risk Area | Assessment | Mitigation Strategy |
|---|---|---|
| Backward Compatibility | Low. Follows semantic versioning (v5.x is stable). | Test in a staging environment before production rollout. |
| Performance | Negligible for typical use (e.g., UI rendering). | Benchmark in high-traffic endpoints if used in loops. |
| Locale Support | High. 20+ locales, but custom translations require manual setup. | Pre-configure supported locales in Laravel’s config/app.php or use a fallback. |
| Type Safety | PHP 8.4+ nullable types may cause warnings if not using strict mode. | Enable strict_types=1 in Laravel’s bootstrap/app.php. |
| Testing | Comprehensive test suite (CI/CD integrated). | Run composer test in CI pipeline. |
humanize() for UI labels vs. toRoman() for niche use)?en) or dynamic (user-preference-based)?DateTimeHumanizer::difference())?Str::title(), Str::limit()). Overlap may reduce need for this package.@php echo StringHumanizer::humanize('user_id') @endphp).return response()->json(['size' => NumberHumanizer::binarySuffix($size)])).public function getHumanizedNameAttribute() { return StringHumanizer::humanize($this->name); }).StringHumanizer::removeShortcodes($request->input('description'))).Str::title() with StringHumanizer::humanize() for consistency.config/app.php:
'humanizer' => [
'default_locale' => 'en',
'supported_locales' => ['en', 'es', 'fr'], // Add as needed
],
public function boot()
{
app()->singleton(StringHumanizer::class, function () {
return new StringHumanizer(app('translator'));
});
}
| Component | Compatibility Notes |
|---|---|
| Laravel 10/11 | Fully compatible (PHP 8.4+). |
| Symfony 6/7/8 | Works out-of-the-box (used in Laravel). |
| PHP 8.1–8.3 | Not supported (requires PHP 8.4+). |
| Translations | Uses Symfony’s translation component (already in Laravel). |
| Custom Logic | Extendable via traits or decorators (e.g., override StringHumanizer methods). |
composer require coduo/php-humanizer
humanize(''), binarySuffix(0)).NumberHumanizer::binarySuffix() for file sizes").coduo/php-humanizer for existing solutions.// Benchmark humanize() in a loop
$start = microtime(true);
for ($i = 0; $i < 10000; $i++) {
StringHumanizer::humanize('test_field_' . $i);
}
echo microtime(true) - $start; // Should be < 100ms for 10K ops
| Scenario | Impact |
How can I help you explore Laravel packages today?