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.
Installation:
composer require mattketmo/camel
No additional configuration is required—just autoload the package.
First Use Case: Convert a string to camelCase:
use Mattketmo\Camel\Camel;
$snakeCase = 'hello_world';
$camelCase = Camel::snakeToCamel($snakeCase);
// Output: 'helloWorld'
Where to Look First:
snakeToCamel(), camelToSnake(), kebabToCamel(), pascalCase().Form Request Sanitization:
use Mattketmo\Camel\Camel;
$requestData = $request->all();
$camelized = array_map([Camel::class, 'snakeToCamel'], $requestData);
// Use in API responses or model assignments.
Model Attribute Mapping:
protected $casts = [
'user_name' => 'string',
];
public function setUserNameAttribute($value) {
$this->attributes['user_name'] = Camel::camelToSnake($value);
}
API Response Transformation:
$response = collect($data)->mapWithKeys(function ($item) {
return [Camel::snakeToCamel(key($item)) => $item];
});
Configuration Files:
// config/app.php
'camel_case' => [
'enabled' => true,
'fields' => ['user.*', 'settings.*'],
];
// Apply via middleware or service provider.
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');
Locale Sensitivity:
é, ü) may not transform predictably. Test with multilingual strings.Camel::snakeToCamel('café_au_lait'); // May return 'caféAuLait' or 'cafeAuLait'.
Edge Cases:
hello-world_foo) may cause unexpected results.trim() or preg_replace().Performance:
snakeToCamel(camelToSnake($str))). Cache results if reused.False Positives:
HTTPRequest) may not split correctly. Use Camel::pascalCase() for consistency.dd(Camel::snakeToCamel('test_string')); // Debug unexpected results.
studlyCase() support).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));
}
}
Laravel Helpers:
Create a macro for Str::camel():
Str::macro('camel', function ($str) {
return Camel::snakeToCamel($str);
});
Configuration:
Add a config file (config/camel.php) to centralize rules (e.g., reserved words to exclude from transformation):
return [
'reserved' => ['id', 'created_at'],
];
public function toArray($request) {
return collect(parent::toArray($request))
->mapWithKeys(fn ($value, $key) => [Camel::snakeToCamel($key) => $value]);
}
resolveAttribute():
public function resolveAttribute($resource, $attribute) {
return Camel::camelToSnake($attribute);
}
$input = Camel::snakeToCamel($this->argument('input'));
How can I help you explore Laravel packages today?