composer require misatotremor/case-bundle
CaseConverter:
// app/Providers/AppServiceProvider.php
use Avro\CaseBundle\Util\CaseConverter;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->singleton(CaseConverter::class, function ($app) {
return new CaseConverter();
});
}
}
use Avro\CaseBundle\Util\CaseConverter;
class StringNormalizer
{
public function __construct(private CaseConverter $converter) {}
public function normalize(string $input, string $targetCase): string
{
return match ($targetCase) {
'camel' => $this->converter->toCamelCase($input),
'pascal' => $this->converter->toPascalCase($input),
'kebab' => $this->converter->toKebabCase($input),
'title' => $this->converter->toTitleCase($input),
'underscore' => $this->converter->toUnderscoreCase($input),
default => $input,
};
}
}
Single String Conversion:
$converter->toCamelCase('user_first_name'); // "userFirstName"
$converter->toPascalCase('user_first_name'); // "UserFirstName"
Array Batch Processing:
$converter->toCamelCase(['user_first_name', 'user_last_name']);
// ["userFirstName", "userLastName"]
Dynamic Case Handling in Controllers:
public function transform(Request $request, CaseConverter $converter)
{
$case = $request->input('case', 'camel');
$data = $converter->{"to{$case}Case"}($request->input('input'));
return response()->json($data);
}
Eloquent Model Attribute Casting:
use Avro\CaseBundle\Util\CaseConverter;
class User extends Model
{
protected $casts = [
'name' => 'string',
];
public function getNameAttribute($value)
{
return app(CaseConverter::class)->toTitleCase($value);
}
public function setNameAttribute($value)
{
$this->attributes['name'] = app(CaseConverter::class)->toUnderscoreCase($value);
}
}
API Response Normalization:
public function show(User $user, CaseConverter $converter)
{
return response()->json([
'user' => [
'firstName' => $converter->toCamelCase($user->first_name),
'lastName' => $converter->toCamelCase($user->last_name),
],
]);
}
Laravel Facade (Optional): Create a facade for cleaner syntax:
// app/Facades/CaseConverterFacade.php
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class CaseConverterFacade extends Facade
{
protected static function getFacadeAccessor()
{
return \Avro\CaseBundle\Util\CaseConverter::class;
}
}
Then use:
use App\Facades\CaseConverterFacade as Case;
Case::toPascalCase('user_first_name'); // "UserFirstName"
Service Container Aliases:
Add an alias in config/app.php:
'aliases' => [
// ...
'Case' => App\Facades\CaseConverterFacade::class,
],
Request Filtering: Use middleware to auto-convert request data:
public function handle(Request $request, Closure $next)
{
$request->merge([
'camel_case_data' => app(CaseConverter::class)->toCamelCase($request->input('snake_case_data')),
]);
return $next($request);
}
Blade Directives (Alternative to Twig): Register a Blade directive for template usage:
Blade::directive('camel', function ($expression) {
return "<?php echo app(\\Avro\\CaseBundle\\Util\\CaseConverter::class)->toCamelCase({$expression}); ?>";
});
Usage:
@camel($variable)
Symfony Dependency Injection:
CaseConverter to avoid errors.Twig Extension Conflicts:
# config/packages/avro_case.yaml (if manually configured)
avro_case:
use_twig: false
Edge Case Handling:
Title Case for üBER).$converter->toTitleCase('über'); // May return "Über" or "Über" depending on implementation.
Array Depth Limitations:
array_map with the converter:
array_map([$converter, 'toCamelCase'], $nestedArray);
Performance for Large Datasets:
$chunkSize = 1000;
$results = [];
foreach (array_chunk($largeArray, $chunkSize) as $chunk) {
$results = array_merge($results, array_map([$converter, 'toCamelCase'], $chunk));
}
Verify Converter Injection:
null, ensure the service provider is registered and the binding is correct.dd(app(CaseConverter::class)); // Should return an instance of Avro\CaseBundle\Util\CaseConverter
Check for Method Existence:
$methods = get_class_methods(CaseConverter::class);
// ['toCamelCase', 'toPascalCase', 'toKebabCase', 'toTitleCase', 'toUnderscoreCase']
Handle Null/Empty Inputs:
null or empty strings. Add guards:
$input = $request->input('data') ?? '';
$converted = empty($input) ? $input : $converter->toCamelCase($input);
Locale-Specific Title Case:
toTitleCase may not respect locale rules (e.g., "iPhone" vs "iPad"). For advanced use, combine with Symfony\Component\String\UnicodeString:
use Symfony\Component\String\UnicodeString;
$titleCase = UnicodeString::from($str)->title();
Custom Case Formats:
CaseConverter class to add new case types:
class ExtendedCaseConverter extends CaseConverter
{
public function toSpaceCase(string $str): string
{
return str_replace('_', ' ', $str);
}
}
$this->app->singleton(ExtendedCaseConverter::class);
Laravel-Specific Features:
Str helper for seamless integration:
Str::macro('avroCase', function ($str, $case) {
$converter = app(CaseConverter::class);
return match ($case) {
'camel' => $converter->toCamelCase($str),
'pascal' => $converter->toPascalCase($str),
// ...
default => $str,
};
});
Str
How can I help you explore Laravel packages today?