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

Camel Laravel Package

mattketmo/camel

Laravel-friendly utilities for converting strings, keys, and arrays between camelCase, snake_case, StudlyCase and more. Handy for normalizing request/response payloads, config keys, and API data with simple helpers and minimal setup.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require mattketmo/camel
    

    No additional configuration is required—just autoload the package.

  2. First Use Case: Convert a string to camelCase:

    use Mattketmo\Camel\Camel;
    
    $snakeCase = 'hello_world';
    $camelCase = Camel::snakeToCamel($snakeCase);
    // Output: 'helloWorld'
    
  3. Where to Look First:

    • Class Documentation (if available; check for README or PHPDoc).
    • Core methods: snakeToCamel(), camelToSnake(), kebabToCamel(), pascalCase().

Implementation Patterns

Common Workflows

  1. Form Request Sanitization:

    use Mattketmo\Camel\Camel;
    
    $requestData = $request->all();
    $camelized = array_map([Camel::class, 'snakeToCamel'], $requestData);
    // Use in API responses or model assignments.
    
  2. Model Attribute Mapping:

    protected $casts = [
        'user_name' => 'string',
    ];
    
    public function setUserNameAttribute($value) {
        $this->attributes['user_name'] = Camel::camelToSnake($value);
    }
    
  3. API Response Transformation:

    $response = collect($data)->mapWithKeys(function ($item) {
        return [Camel::snakeToCamel(key($item)) => $item];
    });
    
  4. Configuration Files:

    // config/app.php
    'camel_case' => [
        'enabled' => true,
        'fields' => ['user.*', 'settings.*'],
    ];
    
    // Apply via middleware or service provider.
    

Integration Tips

  • Laravel Service Provider: Bind the Camel facade for global access:

    $this->app->bind('camel', function () {
        return new \Mattketmo\Camel\Camel();
    });
    
  • Dynamic Method Calls: Use reflection or method_exists() to check for supported transformations before calling.

  • Testing: Mock the Camel class in unit tests to isolate case-conversion logic:

    $this->partialMock(Camel::class, ['snakeToCamel'])
         ->shouldReceive('snakeToCamel')
         ->once()
         ->andReturn('mockedResult');
    

Gotchas and Tips

Pitfalls

  1. Locale Sensitivity:

    • Non-ASCII characters (e.g., é, ü) may not transform predictably. Test with multilingual strings.
    • Example:
      Camel::snakeToCamel('café_au_lait'); // May return 'caféAuLait' or 'cafeAuLait'.
      
  2. Edge Cases:

    • Leading/trailing underscores or mixed separators (hello-world_foo) may cause unexpected results.
    • Fix: Pre-process strings with trim() or preg_replace().
  3. Performance:

    • Avoid chaining transformations (e.g., snakeToCamel(camelToSnake($str))). Cache results if reused.
  4. False Positives:

    • Acronyms (e.g., HTTPRequest) may not split correctly. Use Camel::pascalCase() for consistency.

Debugging

  • Verify Input/Output:
    dd(Camel::snakeToCamel('test_string')); // Debug unexpected results.
    
  • Check for Updates: The package is lightweight; ensure you’re not missing newer features (e.g., studlyCase() support).

Extension Points

  1. Custom Separators: Override the default underscore/kebab handling by extending the class:

    class CustomCamel extends Camel {
        public static function customToCamel($str) {
            return parent::snakeToCamel(str_replace('-', '_', $str));
        }
    }
    
  2. Laravel Helpers: Create a macro for Str::camel():

    Str::macro('camel', function ($str) {
        return Camel::snakeToCamel($str);
    });
    
  3. Configuration: Add a config file (config/camel.php) to centralize rules (e.g., reserved words to exclude from transformation):

    return [
        'reserved' => ['id', 'created_at'],
    ];
    

Pro Tips

  • Use in API Resources:
    public function toArray($request) {
        return collect(parent::toArray($request))
            ->mapWithKeys(fn ($value, $key) => [Camel::snakeToCamel($key) => $value]);
    }
    
  • Laravel Nova: Override field names in resolveAttribute():
    public function resolveAttribute($resource, $attribute) {
        return Camel::camelToSnake($attribute);
    }
    
  • Artisan Commands: Transform CLI arguments:
    $input = Camel::snakeToCamel($this->argument('input'));
    
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.
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
christhompsontldr/laravel-inky