Installation:
composer require misatotremor/case-bundle
Enable the bundle in config/bundles.php:
Avro\CaseBundle\AvroCaseBundle::class => ['all' => true],
First Use Case:
Inject CaseConverter into a service or controller:
use Avro\CaseBundle\Util\CaseConverter;
public function __construct(private CaseConverter $converter) {}
public function convertString(string $input): string
{
return $this->converter->toKebabCase($input); // New: e.g., "userName" → "user-name"
}
Quick Test:
Use the CaseConverter directly in a route or command:
use Symfony\Component\HttpFoundation\Response;
public function testCaseConversion(CaseConverter $converter): Response
{
$result = $converter->toKebabCase("userName"); // New: "user-name"
return new Response($result);
}
Dependency Injection:
Always inject CaseConverter via constructor (recommended) or autowire:
#[AutowireService]
private CaseConverter $converter;
Batch Processing: Convert arrays of strings efficiently (now includes kebab case):
$strings = ["userName", "firstName", "lastName"];
$converted = $this->converter->toKebabCaseArray($strings);
// ["user-name", "first-name", "last-name"]
Twig Integration (if enabled): Use in templates without manual conversion:
{{ "userName"|avro_case('kebab') }} {# Output: user-name #}
Command-Line Usage: Create a console command for bulk conversions (now supports kebab case):
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class ConvertCasesCommand extends Command
{
protected static $defaultName = 'app:convert-cases';
public function __construct(private CaseConverter $converter) {}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$output->writeln($this->converter->toKebabCase("camelCaseString"));
return Command::SUCCESS;
}
}
Form Data Handling: Normalize form input before processing (now supports kebab case):
public function submitForm(Request $request, CaseConverter $converter)
{
$data = $request->request->all();
$normalized = array_map([$converter, 'toKebabCase'], $data);
// Proceed with normalized data...
}
Twig Extension Overhead:
use_twig: false) in config/packages/avro_case.yaml may improve performance if you don’t use templates.use_twig config and clear cache:
php bin/console cache:clear
Locale-Sensitive Conversions:
toTitleCase() respects locale settings (e.g., é in French). Ensure your app’s locale is configured in config/packages/framework.yaml:
framework:
default_locale: en_US # or fr_FR, etc.
Edge Cases in Arrays:
to*Array() methods will be skipped. Handle explicitly if needed:
$mixedArray = ["userName", 123, null];
$converted = array_map(
fn($item) => is_string($item) ? $this->converter->toKebabCase($item) : $item,
$mixedArray
);
Caching Converter Instances:
CaseConverter is a stateless service. No need to cache instances manually; Symfony’s container handles it.Custom Case Formats: Extend the bundle by creating a custom converter (now includes kebab case as a reference):
use Avro\CaseBundle\Util\CaseConverterInterface;
class CustomCaseConverter implements CaseConverterInterface
{
public function convert(string $str): string
{
return strtoupper($str); // Example: "hello" → "HELLO"
}
}
Register it as a service and replace CaseConverter where needed.
Performance: For large datasets, reuse the converter instance (already handled by DI) and avoid redundant conversions:
// Bad: Creates a new instance each time
$converter = new \Avro\CaseBundle\Util\CaseConverter();
// Good: Uses the injected service
$this->converter->toKebabCase($str);
Testing:
Mock CaseConverter in tests to isolate logic (now includes kebab case methods):
$this->mockBuilder()
->disableOriginalConstructor()
->getMock()
->method('toKebabCase')
->willReturn('mocked-result');
Configuration Overrides:
Override the default use_twig setting globally or per-environment:
# config/packages/dev/avro_case.yaml
avro_case:
use_twig: true # Enable only in dev
Debugging Conversions: Add a debug route to test conversions interactively (now includes kebab case):
use Symfony\Component\Routing\Annotation\Route;
#[Route('/debug/case-converter', name: 'debug_case_converter')]
public function debugConverter(CaseConverter $converter, Request $request): Response
{
$input = $request->query->get('input', 'testString');
$result = $converter->toKebabCase($input);
return new Response("Input: {$input} → Output: {$result}");
}
Access via /debug/case-converter?input=userName.
How can I help you explore Laravel packages today?