guzzle/inflection
guzzle/inflection is a tiny PHP inflection utility for converting words between singular and plural forms and applying basic naming transformations. Useful in frameworks and code generators where consistent human-readable naming is needed without heavy dependencies.
Installation Add the package via Composer:
composer require guzzle/inflection
No additional configuration is required—it’s a standalone utility library.
First Use Case Import the core class and use it for basic string transformations:
use Guzzle\Inflection\Inflector;
$inflector = new Inflector();
echo $inflector->camelize('user_profile'); // Output: "userProfile"
Where to Look First
Model/Controller Naming Dynamically generate class names from database tables:
$table = 'posts';
$modelName = (new Inflector())->classify($table); // "Post"
API Route Grouping
Use pluralize() to auto-generate route names:
Route::resource('users', UserController::class);
// Internally, `inflector->pluralize('user')` → "users"
Form/Request Validation Transform snake_case input to camelCase for validation rules:
$camelized = $inflector->camelize('user_email'); // "userEmail"
Database Query Builder Convert model names to table names:
$table = $inflector->tableize('UserProfile'); // "user_profiles"
Service Provider Binding
Bind the Inflector as a singleton in AppServiceProvider for global access:
$this->app->singleton(Inflector::class, function () {
return new Inflector();
});
Then inject it anywhere:
public function __construct(private Inflector $inflector) {}
Laravel Helpers
Extend Laravel’s Str facade by wrapping Inflector methods:
Str::macro('pluralize', function ($string) {
return app(Inflector::class)->pluralize($string);
});
Eloquent Model Booting
Auto-generate fillable fields from a fields array in the model:
protected $fillable = ['name', 'email'];
// Convert to snake_case in constructor:
$this->fillable = (new Inflector())->underscoreArray($this->fillable);
Guzzle 3 Legacy
Inflection component. If you’re using Laravel 8+, ensure compatibility (no breaking changes expected, but avoid mixing with Guzzle HTTP client).camelize() may behave differently than Laravel’s native Str::camel() (e.g., user_profile → userProfile vs. userProfile → user_profile).Edge Cases in Pluralization
child → children) work, but test custom rules:
$inflector->pluralize('ox'); // "oxen" (correct)
$inflector->pluralize('custom_thing'); // May not handle as expected.
Inflector::setRules().Performance
Inflector once (e.g., as a singleton) to avoid regex recompilation overhead.Locale-Specific Rules
camelCase), extend the class or use a dedicated library like voku/portable-ascii.Method Chaining Chain methods for complex transformations:
$inflector->classify('user_profile')->pluralize(); // "Profiles"
Custom Rules Add custom rules to handle domain-specific cases:
$inflector->pluralize('APIKey', 'APIKeys');
$inflector->singularize('APIKeys', 'APIKey');
Fallback for Missing Methods Check if a method exists before calling:
if (method_exists($inflector, 'humanize')) {
$inflector->humanize('user_id'); // "User ID"
}
Subclassing
Extend Inflector to add domain-specific methods:
class CustomInflector extends Inflector {
public function kebabize($string) {
return strtolower($this->underscore($string));
}
}
Rule Overrides Modify pluralization/singularization rules:
$inflector->pluralize('test', 'tests'); // Force override
Integration with Laravel Collectives
Use with Laravel Collective for form input transformations:
$inflector->camelizeArray(request()->all());
How can I help you explore Laravel packages today?