Installation:
composer require nayjest/str-case-converter
No additional configuration is required—use the package directly via its facade or class.
First Use Case: Convert a snake_case string to camelCase in a Laravel controller:
use Nayjest\StrCaseConverter\Str;
$camelCase = Str::toCamelCase('user_first_name'); // 'UserFirstName'
Or convert camelCase to snake_case:
$snakeCase = Str::toSnakeCase('userFirstName'); // 'user_first_name'
Where to Look First:
Nayjest\StrCaseConverter\Facades\Str (if published).Nayjest\StrCaseConverter\Str (direct usage).tests/ directory for edge cases (e.g., mixed delimiters, empty strings).Model Attribute Mapping: Automatically convert database column names (snake_case) to Eloquent model attributes (camelCase):
public function getTable()
{
return 'user_profiles'; // snake_case
}
// Convert to camelCase for API responses
$tableName = Str::toCamelCase($this->getTable()); // 'UserProfiles'
Request Validation:
Normalize input case in FormRequest classes:
public function rules()
{
return [
'user_first_name' => 'required|string',
// Convert to camelCase for model binding
Str::toCamelCase('user_first_name') => 'sometimes|string',
];
}
API Response Transformation:
Use in AppServiceProvider to standardize JSON responses:
public function boot()
{
Response::macro('camelCase', function ($response) {
$data = $response->getData();
array_walk_recursive($data, function (&$value, $key) {
$value = Str::toCamelCase($key);
});
return $response->setData($data);
});
}
Service Container Binding: Bind the converter as a singleton for dependency injection:
// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->singleton('caseConverter', function () {
return new \Nayjest\StrCaseConverter\Str();
});
}
Usage:
$converter = app('caseConverter');
$camelCase = $converter->toCamelCase('snake_string');
Custom Facade:
Extend Laravel’s Str facade or create a dedicated facade:
// app/Facades/CaseConverter.php
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class CaseConverter extends Facade
{
protected static function getFacadeAccessor() { return 'caseConverter'; }
}
Usage:
use App\Facades\CaseConverter;
$snakeCase = CaseConverter::toSnakeCase('camelCaseString');
Helper Functions:
Add global helper functions in app/Helpers.php:
if (!function_exists('to_camel_case')) {
function to_camel_case($string) {
return \Nayjest\StrCaseConverter\Str::toCamelCase($string);
}
}
Usage:
$camel = to_camel_case('snake_case_string');
Eloquent Events: Use in model events to enforce case consistency:
protected static function boot()
{
static::creating(function ($model) {
$model->attributes = array_map(function ($value, $key) {
return [$key => Str::toCamelCase($key) => $value];
}, $model->attributes, array_keys($model->attributes));
});
}
API Route Naming: Convert route names to snake_case for consistency:
Route::get('user/{user}', function ($user) {
$snakeRoute = Str::toSnakeCase('userShow'); // 'user_show'
return response()->json(['route' => $snakeRoute]);
});
No PHP 8.x Support:
strict_types and may throw deprecation warnings in PHP 8.x.declare(strict_types=1);
to the Str.php file.Edge Cases with Delimiters:
user__name → userName instead of userName).preg_replace('/_{2,}/', '_', $string).Reserved Words:
user_class → userClass if class is a reserved word).ctype_alnum() or a whitelist.Unicode Characters:
café → cafe).Str::of() for Unicode support:
Str::of('café')->camel(); // 'cafe'
No Custom Separators in toCamelCase:
-, _, and space for toCamelCase.$string = preg_replace('/[_\-\s]/', '_', $string);
$camel = Str::toCamelCase($string);
Input Validation:
if (preg_match('/[^a-zA-Z0-9_\-\s]/', $string)) {
Log::warning("Invalid characters in case conversion input: {$string}");
}
Test Edge Cases:
Str::toCamelCase('') → ''.Str::toCamelCase('user_123') → user123.Str::toCamelCase('User_Name') → userName.Performance:
$time = microtime(true);
for ($i = 0; $i < 10000; $i++) {
Str::toCamelCase('snake_case_string');
}
$duration = microtime(true) - $time;
preg_replace_callback:
function to_camel_case($str) {
return preg_replace_callback('/_([a-z])/', function ($matches) {
return strtoupper($matches[1]);
}, $str);
}
Facade Publishing:
config/app.php:
'aliases' => [
'Str' => Nayjest\StrCaseConverter\Facades\Str::class,
],
Service Provider:
StrCaseConverterServiceProvider), but it’s optional. If using the facade, ensure it’s registered.Custom Delimiters:
toSnakeCase supports custom delimiters:
Str::toSnakeCase('camelCase', '-'); // 'camel-case'
toCamelCase with custom delimiters:
function to_camel_case_custom($string, $delimiter = '_') {
return Str::toCamelCase(str_replace($delimiter, ' ', $string));
}
Add New Case Types:
Str class to support PascalCase or kebab-case:
namespace Nayjest\StrCaseConverter;
class Str {
public static function toPascalCase($string) {
return ucfirst(self::toCamelCase($string));
}
}
Locale-Aware Conversion:
public static function toCamelCase($string) {
$string = iconv('UTF-8', 'ASCII//TRANSLIT', $string);
return parent::toCamelCase($string);
}
Integration with Laravel Helpers:
Str helper for unified API:
if (!method_exists(\Str::class, 'toCamelCase')) {
\Str::macro('toCamelCase', function
How can I help you explore Laravel packages today?