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

Inflection Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation Add the package via Composer:

    composer require guzzle/inflection
    

    No additional configuration is required—it’s a standalone utility library.

  2. 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"
    
  3. Where to Look First

    • Core Methods: Check Inflector for available methods like camelize(), underscore(), pluralize(), etc.
    • Tests: Review tests for usage examples and edge cases.

Implementation Patterns

Common Workflows

  1. Model/Controller Naming Dynamically generate class names from database tables:

    $table = 'posts';
    $modelName = (new Inflector())->classify($table); // "Post"
    
  2. API Route Grouping Use pluralize() to auto-generate route names:

    Route::resource('users', UserController::class);
    // Internally, `inflector->pluralize('user')` → "users"
    
  3. Form/Request Validation Transform snake_case input to camelCase for validation rules:

    $camelized = $inflector->camelize('user_email'); // "userEmail"
    
  4. Database Query Builder Convert model names to table names:

    $table = $inflector->tableize('UserProfile'); // "user_profiles"
    

Integration Tips

  • 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);
    

Gotchas and Tips

Pitfalls

  1. Guzzle 3 Legacy

    • This is a read-only subtree split of Guzzle 3’s Inflection component. If you’re using Laravel 8+, ensure compatibility (no breaking changes expected, but avoid mixing with Guzzle HTTP client).
    • No Guzzle 7+ Support: Methods like camelize() may behave differently than Laravel’s native Str::camel() (e.g., user_profileuserProfile vs. userProfileuser_profile).
  2. Edge Cases in Pluralization

    • Irregular plurals (e.g., childchildren) work, but test custom rules:
      $inflector->pluralize('ox'); // "oxen" (correct)
      $inflector->pluralize('custom_thing'); // May not handle as expected.
      
    • Override defaults via Inflector::setRules().
  3. Performance

    • Instantiate Inflector once (e.g., as a singleton) to avoid regex recompilation overhead.
  4. Locale-Specific Rules

    • The package assumes English rules. For non-English (e.g., Spanish camelCase), extend the class or use a dedicated library like voku/portable-ascii.

Debugging Tips

  • 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"
    }
    

Extension Points

  1. Subclassing Extend Inflector to add domain-specific methods:

    class CustomInflector extends Inflector {
        public function kebabize($string) {
            return strtolower($this->underscore($string));
        }
    }
    
  2. Rule Overrides Modify pluralization/singularization rules:

    $inflector->pluralize('test', 'tests'); // Force override
    
  3. Integration with Laravel Collectives Use with Laravel Collective for form input transformations:

    $inflector->camelizeArray(request()->all());
    
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.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky