Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Case Bundle Laravel Package

avro/case-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Install via Composer:
    composer require avro/case-bundle
    
  2. Enable the Bundle in config/bundles.php (Symfony 4+) or AppKernel.php (Symfony 2/3):
    Avro\CaseBundle\AvroCaseBundle::class => ['all' => true],
    
  3. Configure (optional) in config/packages/avro_case.yaml:
    avro_case:
        use_twig: true  # Enable Twig filters (default: true)
    

First Use Case

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"
}

Implementation Patterns

Core Workflows

  1. Service-Based Conversion: Inject the converter service (avro_case.converter) into any class:

    public function __construct(private AvroCaseConverter $converter) {}
    

    Use methods like:

    • toCamelCase()someStringHere
    • toPascalCase()SomeStringHere
    • toUnderscoreCase()some_string_here
    • toTitleCase()Some String Here
  2. Batch Processing: Convert arrays of strings:

    $converter->toCamelCase(['foo_bar', 'baz_qux']); // ['fooBar', 'bazQux']
    
  3. Twig Integration: Use filters in templates (if use_twig: true):

    {{ 'hello_world' | camel }}  {# helloWorld #}
    {{ 'hello_world' | pascal }} {# HelloWorld #}
    

Integration Tips

  • Validation: Pair with Symfony’s 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')
    ]);
    
  • API Responses: Normalize case in serializers (e.g., 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 }}
    
  • Database Migrations: Use for column naming consistency:
    $schema->renameTable('old_table_name', $converter->toUnderscoreCase('NewTableName'));
    

Gotchas and Tips

Pitfalls

  1. Twig Filter Overhead:

    • Disabling Twig filters (use_twig: false) reduces memory usage if unused.
    • Filters are not lazy-loaded; they register globally at bundle startup.
  2. Edge Cases in Conversion:

    • Title Case: Handles apostrophes and hyphens poorly (e.g., "o'reilly""O'Reilly" but "user-id""User-Id").
    • Camel/Pascal: Assumes input is underscore/space-separated. Malformed input (e.g., "fooBar_Baz") may produce unexpected results ("fooBarBaz" instead of "fooBarBaz").
  3. Service Container Access:

    • In Symfony 4+, use dependency injection (DI) instead of $this->container->get():
      // ❌ Avoid
      $this->container->get('avro_case.converter');
      
      // ✅ Prefer
      public function __construct(private AvroCaseConverter $converter) {}
      

Debugging

  • Log Input/Output:
    $input = 'some input';
    $output = $converter->toCamelCase($input);
    $this->logger->debug('Case conversion', ['input' => $input, 'output' => $output]);
    
  • Test Edge Cases:
    $this->assertEquals('helloWorld', $converter->toCamelCase('hello_world'));
    $this->assertEquals('HelloWorld', $converter->toPascalCase('hello_world'));
    $this->assertEquals('Some String', $converter->toTitleCase('some_string'));
    

Extension Points

  1. 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']
    
  2. 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);
        });
    }
    
  3. Performance:

    • Cache converted strings if used repeatedly (e.g., in loops):
      $cache = new ArrayCache();
      $cache->get('camel_case_' . $input, function() use ($converter, $input) {
          return $converter->toCamelCase($input);
      });
      
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver