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

misatotremor/case-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation:

    composer require misatotremor/case-bundle
    

    Enable the bundle in config/bundles.php:

    Avro\CaseBundle\AvroCaseBundle::class => ['all' => true],
    
  2. 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"
    }
    
  3. 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);
    }
    

Implementation Patterns

Core Workflows

  1. Dependency Injection: Always inject CaseConverter via constructor (recommended) or autowire:

    #[AutowireService]
    private CaseConverter $converter;
    
  2. 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"]
    
  3. Twig Integration (if enabled): Use in templates without manual conversion:

    {{ "userName"|avro_case('kebab') }}  {# Output: user-name #}
    
  4. 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;
        }
    }
    
  5. 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...
    }
    

Gotchas and Tips

Pitfalls

  1. Twig Extension Overhead:

    • Disabling Twig (use_twig: false) in config/packages/avro_case.yaml may improve performance if you don’t use templates.
    • Debugging: If Twig filters fail silently, check use_twig config and clear cache:
      php bin/console cache:clear
      
  2. 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.
      
  3. Edge Cases in Arrays:

    • Non-string values in arrays passed to 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
      );
      
  4. Caching Converter Instances:

    • The CaseConverter is a stateless service. No need to cache instances manually; Symfony’s container handles it.

Tips

  1. 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.

  2. 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);
    
  3. Testing: Mock CaseConverter in tests to isolate logic (now includes kebab case methods):

    $this->mockBuilder()
         ->disableOriginalConstructor()
         ->getMock()
         ->method('toKebabCase')
         ->willReturn('mocked-result');
    
  4. 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
    
  5. 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.

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.
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope