Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Inflector Laravel Package

doctrine/inflector

Doctrine Inflector is a lightweight PHP library for common string transformations: convert words between singular and plural forms, and handle case changes. Useful for naming conventions in frameworks and tools where consistent word inflection is needed.

View on GitHub
Deep Wiki
Context7

Getting Started

Start by installing the package via Composer:

composer require doctrine/inflector

Then, instantiate the inflector for your target language (e.g., English):

use Doctrine\Inflector\InflectorFactory;

$inflector = InflectorFactory::create()->build();

Begin with basic pluralization/singularization and casing transformations:

echo $inflector->pluralize('person'); // 'people'
echo $inflector->singularize('vertices'); // 'vertex'
echo $inflector->camelize('user_name'); // 'userName'
echo $inflector->snakeCase('UserName'); // 'user_name'

The README and /docs directory in the repo provide concise API examples—start there for quick validation.

Implementation Patterns

  • Code Generation Workflows: Integrate the inflector in scaffolding tools, ORM layer integrations (e.g., generating entity class names or DB table names), or CLI tools. Example:
    $tableName = $inflector->tableize('UserProfile'); // 'user_profiles'
    $className = $inflector->classify('product_categories'); // 'ProductCategory'
    
  • Framework-Agnostic Utility: Use it in custom libraries or legacy apps where Laravel’s Str::plural() is unavailable. Since it’s PSR- compliant and dependency-light, drop it into any project.
  • Language-Specific Configurations: Switch inflection rules by locale:
    $inflector = InflectorFactory::createForLocale('es')->build(); // Spanish rules
    
  • Custom Rule Extension: Register domain-specific irregular forms (e.g., for SaaS business terms):
    $inflector->getPluralRuleRuleset()->addIrregular('octopus', 'octopodes');
    
  • Internal Casing Consistency: Normalize identifiers before serialization, caching keys, or API payloads—ensuring stable casing across integrations.

Gotchas and Tips

  • API Break in v2.x: Avoid the legacy API (Inflector::pluralize()) entirely—use the new Inflector instance methods only. If upgrading, update composer constraints to "doctrine/inflector": "^2.0" and audit usages.
  • Language Mismatch Pitfalls: English rules dominate by default. Always specify createForLocale() for non-English text; otherwise, words like criteriumcriteriums (incorrect vs criteria) will slip through.
  • Irregular Word Overlap: The Uninflected list is extensive but not exhaustive. If a word fails inflection (e.g., data staying plural), extend uninflected words:
    $inflector->getUninflected()->add('/^(data|equipment)$/i');
    
  • Casing Methods Are Not Invertible: camelize('foo-bar')'fooBar', but humanize() (not part of Inflector) isn’t included. Use snakeCase() + titleCase() from other libraries for roundtrips.
  • Performance: For high-throughput systems (e.g., rendering 10k+ table names), instantiate once as a singleton or service—do not rebuild InflectorFactory on every request.
  • Tests Are Your friend: Many edge cases are covered in test/InflectorTest.php. Check them when debugging unexpected output (e.g., why nurserynurseries but browniebrownies).
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport
twbs/bootstrap4