composer require avro/case-bundle
config/bundles.php (Symfony 4+) or AppKernel.php (Symfony 2/3):
Avro\CaseBundle\AvroCaseBundle::class => ['all' => true],
config/packages/avro_case.yaml:
avro_case:
use_twig: true # Enable Twig filters (default: true)
Convert a string to camelCase in a controller:
use Symfony\Component\HttpFoundation\Response;
public function convertAction(): Response
{
$converter = $this->get('avro_case.converter');
$result = $converter->toCamelCase('some_string_here');
return new Response($result); // Output: "someStringHere"
}
Service-Based Conversion:
Inject the converter service (avro_case.converter) into any class:
public function __construct(private AvroCaseConverter $converter) {}
Use methods like:
toCamelCase() → someStringHeretoPascalCase() → SomeStringHeretoUnderscoreCase() → some_string_heretoTitleCase() → Some String HereBatch Processing: Convert arrays of strings:
$converter->toCamelCase(['foo_bar', 'baz_qux']); // ['fooBar', 'bazQux']
Twig Integration:
Use filters in templates (if use_twig: true):
{{ 'hello_world' | camel }} {# helloWorld #}
{{ 'hello_world' | pascal }} {# HelloWorld #}
Validator to enforce case standards in DTOs:
$validator = $this->get('validator');
$errors = $validator->validate($dto, [
new Assert\Expression('value matches /^[a-z][a-zA-Z0-9]*$/', 'Invalid camelCase')
]);
JMSSerializer):
# config/serializer/Entity.MyEntity.yaml
properties:
fieldName:
serialized_name: field_name
accessor:
getter: getFieldName
setter: setFieldName
type: string
groups: [api]
twig: {{ object.fieldName | underscore }}
$schema->renameTable('old_table_name', $converter->toUnderscoreCase('NewTableName'));
Twig Filter Overhead:
use_twig: false) reduces memory usage if unused.Edge Cases in Conversion:
"o'reilly" → "O'Reilly" but "user-id" → "User-Id")."fooBar_Baz") may produce unexpected results ("fooBarBaz" instead of "fooBarBaz").Service Container Access:
$this->container->get():
// ❌ Avoid
$this->container->get('avro_case.converter');
// ✅ Prefer
public function __construct(private AvroCaseConverter $converter) {}
$input = 'some input';
$output = $converter->toCamelCase($input);
$this->logger->debug('Case conversion', ['input' => $input, 'output' => $output]);
$this->assertEquals('helloWorld', $converter->toCamelCase('hello_world'));
$this->assertEquals('HelloWorld', $converter->toPascalCase('hello_world'));
$this->assertEquals('Some String', $converter->toTitleCase('some_string'));
Custom Case Formats: Extend the converter by creating a subclass:
class CustomCaseConverter extends AvroCaseConverter
{
public function toSnakeCase($str): string
{
return $this->toUnderscoreCase($str);
}
}
Register it as a service:
services:
app.custom_case.converter:
class: App\Service\CustomCaseConverter
tags: ['avro_case.converter']
Override Twig Filters: Replace the default Twig extension:
// src/Kernel.php
public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load(function (ContainerBuilder $container) {
$container->setAlias('avro_case.twig.extension', MyCustomTwigExtension::class);
});
}
Performance:
$cache = new ArrayCache();
$cache->get('camel_case_' . $input, function() use ($converter, $input) {
return $converter->toCamelCase($input);
});
How can I help you explore Laravel packages today?