oodle/inflect
Lightweight PHP inflector for converting English words between singular and plural forms. Install via Composer and use static methods like Inflect::singularize('tests') and Inflect::pluralize('test') for quick string inflection.
Installation
Add to composer.json:
"require": {
"oodle/inflect": "^1.0"
}
Run composer update or composer install.
First Usage
Import and use the Inflect facade in your Laravel app:
use Inflect\Inflect;
// Basic inflection
$singular = Inflect::singularize('photos'); // "photo"
$plural = Inflect::pluralize('child'); // "children"
First Use Case Dynamically generate model names or table names in controllers/views:
$modelName = Inflect::singularize(request()->route()->getName());
$tableName = Inflect::tableize($modelName); // e.g., "posts" → "post" → "posts"
Model/Table Name Generation
Use Inflect to auto-generate Eloquent model/table names from routes or user input:
$routeName = 'admin/users/posts';
$modelName = Inflect::singularize($routeName); // "post"
$tableName = Inflect::tableize($modelName); // "posts"
Dynamic Form Labels Convert snake_case or pluralized fields to human-readable labels:
$field = 'user_posts_count';
$label = Inflect::humanize($field); // "User posts count"
Memoization for Performance Leverage memoization for repeated inflections (e.g., in loops or API responses):
// First call caches the result
Inflect::pluralize('crisis'); // "crises" (cached)
Integration with Laravel Helpers
Extend Laravel’s Str helper by wrapping Inflect in a trait:
use Inflect\Inflect;
trait InflectHelper {
public static function pluralize($string) {
return Inflect::pluralize($string);
}
}
Custom Rules Extend the inflector with domain-specific rules (e.g., for acronyms):
Inflect::pluralize('NASA'); // "NASAs" (default)
Inflect::addRule('NASA', 'NASAs', 'NASAs'); // Custom rule
Localization Override inflections per language (e.g., Spanish plurals):
Inflect::setLocale('es');
Inflect::pluralize('hombre'); // "hombres" (Spanish)
API Response Normalization Standardize JSON responses by inflecting keys:
$data = ['items' => collect($posts)->toArray()];
$data['posts'] = $data['items']; // Rename key dynamically
unset($data['items']);
Memoization Overhead
Inflect::clearMemoization('special_case');
Locale Conflicts
// config/app.php (bootstrapServices)
Inflect::setLocale(config('app.locale'));
Edge Cases in Rules
addRule('foo', 'bar') overriding Inflect::pluralize('foo')).Thread Safety
Inspect Rules Dump all registered rules to debug unexpected behavior:
print_r(Inflect::getRules());
Disable Memoization Temporarily disable caching to test rule accuracy:
Inflect::disableMemoization();
Inflect::pluralize('edge_case'); // Uncached result
Fallback to Defaults Reset to default rules if customizations break:
Inflect::reset();
Add Custom Inflections
Extend the package by adding methods to the Inflect class:
// In a service provider
Inflect::addMethod('acronymize', function($string) {
return preg_replace('/([a-z])([A-Z])/', '$1_$2', $string);
});
Override Core Methods Replace entire methods (e.g., for locale-specific logic):
Inflect::overrideMethod('pluralize', function($word) {
// Custom logic
return $word . 's';
});
Integrate with Laravel Events
Trigger inflection events (e.g., Inflecting) for observability:
event(new Inflecting('pluralize', ['word' => 'child']));
How can I help you explore Laravel packages today?