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

Caser Laravel Package

rmiller/caser

PHP case manipulation utility for converting and formatting strings between common naming styles (e.g., camelCase, snake_case, kebab-case, StudlyCase). Lightweight helper for normalizing identifiers and labels in applications and libraries.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require rmiller/caser
    

    No configuration is required—just autoload the package.

  2. First Use Case: Transform a string to a specific case in a Laravel controller or helper:

    use RichardMiller\Caser\Facades\Caser;
    
    $text = "hello world";
    $upperCamel = Caser::toUpperCamel($text); // "HelloWorld"
    $snakeCase = Caser::toSnakeCase($text);   // "hello_world"
    
  3. Where to Look First:


Implementation Patterns

Common Workflows

  1. String Normalization: Use Caser::toSnakeCase() or Caser::toKebabCase() for database column names or API routes:

    $route = route('user.profile');
    $kebabRoute = Caser::toKebabCase($route); // "user_profile"
    
  2. Model/Attribute Handling: Dynamically transform attribute names in Eloquent models:

    public function getAttribute($key)
    {
        $snakeKey = Caser::toSnakeCase($key);
        return $this->attributes[$snakeKey] ?? null;
    }
    
  3. Form Request Validation: Normalize input keys before validation:

    public function rules()
    {
        $rules = [];
        foreach ($this->all() as $key => $value) {
            $normalizedKey = Caser::toSnakeCase($key);
            $rules[$normalizedKey] = 'required|string';
        }
        return $rules;
    }
    
  4. API Response Transformation: Convert camelCase API responses to snake_case for consistency:

    $response = Caser::toSnakeCase(json_encode($data));
    

Integration Tips

  • Service Providers: Register the facade in config/app.php if not auto-discovered.
  • Custom Cases: Extend the package by creating a new Case class (see source).
  • Laravel Helpers: Add a helper function in app/Helpers/caser.php:
    if (!function_exists('snake_case')) {
        function snake_case($string) {
            return Caser::toSnakeCase($string);
        }
    }
    

Gotchas and Tips

Pitfalls

  1. Non-Alphanumeric Characters: Some case transformations (e.g., toSnakeCase) may not handle symbols like @, #, or spaces predictably. Pre-sanitize strings if needed:

    $cleanText = preg_replace('/[^a-zA-Z0-9]/', ' ', $text);
    
  2. Locale Sensitivity: Accented characters (e.g., é, ü) may not transform as expected. Use strtolower() or mb_strtolower() first:

    $text = mb_strtolower($text, 'UTF-8');
    $snake = Caser::toSnakeCase($text);
    
  3. Facade vs. Class: Directly instantiate \RichardMiller\Caser\Case for non-facade use (e.g., in tests):

    $caser = new \RichardMiller\Caser\Case();
    $result = $caser->toUpperCamel($text);
    

Debugging

  • Unexpected Output: Check the source to verify logic. Override methods if needed.
  • Performance: For bulk operations, cache transformed strings:
    $cacheKey = 'caser_'.$text;
    $cached = Cache::remember($cacheKey, now()->addHours(1), function() use ($text) {
        return Caser::toSnakeCase($text);
    });
    

Extension Points

  1. Add Custom Cases: Create a new class extending \RichardMiller\Caser\Case:

    namespace App\Services;
    
    use RichardMiller\Caser\Case as BaseCase;
    
    class CustomCase extends BaseCase
    {
        public function toTitleCase($string)
        {
            return ucwords(strtolower($string));
        }
    }
    
  2. Override Facade: Bind your custom case to the Caser facade in a service provider:

    $this->app->bind('caser', function() {
        return new \App\Services\CustomCase();
    });
    
  3. Testing: Mock the facade in PHPUnit:

    $this->mock(\RichardMiller\Caser\Facades\Caser::class)
         ->shouldReceive('toSnakeCase')
         ->andReturn('mocked_result');
    
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.
besmartand-pro/php-quality-config
sentix/ai-chatbot
terminal42/code-quality-tools
codifyo/ts-generator-bundle
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