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.
Installation:
composer require rmiller/caser
No configuration is required—just autoload the package.
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"
Where to Look First:
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"
Model/Attribute Handling: Dynamically transform attribute names in Eloquent models:
public function getAttribute($key)
{
$snakeKey = Caser::toSnakeCase($key);
return $this->attributes[$snakeKey] ?? null;
}
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;
}
API Response Transformation: Convert camelCase API responses to snake_case for consistency:
$response = Caser::toSnakeCase(json_encode($data));
config/app.php if not auto-discovered.Case class (see source).app/Helpers/caser.php:
if (!function_exists('snake_case')) {
function snake_case($string) {
return Caser::toSnakeCase($string);
}
}
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);
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);
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);
$cacheKey = 'caser_'.$text;
$cached = Cache::remember($cacheKey, now()->addHours(1), function() use ($text) {
return Caser::toSnakeCase($text);
});
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));
}
}
Override Facade:
Bind your custom case to the Caser facade in a service provider:
$this->app->bind('caser', function() {
return new \App\Services\CustomCase();
});
Testing: Mock the facade in PHPUnit:
$this->mock(\RichardMiller\Caser\Facades\Caser::class)
->shouldReceive('toSnakeCase')
->andReturn('mocked_result');
How can I help you explore Laravel packages today?