jawira/case-converter
Convert strings between common case styles (camelCase, snake_case, kebab-case, PascalCase, etc.) with a simple PHP API. Handy for slugs, variable naming, and text normalization, supporting robust Unicode-aware conversions and formatting options.
Besides \Jawira\CaseConverter\Convert you also have at your disposal:
\Jawira\CaseConverter\CaseConverter\Jawira\CaseConverter\CaseConverterInterfaceInstead of using new Convert(); you can use the convenience method from
CaseConverter class.
In concrete, you have to call \Jawira\CaseConverter\CaseConverter::convert to
create Convert objects.
Here an example:
<?php
namespace App;
use Jawira\CaseConverter\CaseConverterInterface;
class MySuperNameCreator
{
protected $cc;
public function __construct(CaseConverterInterface $cc)
{
$this->cc = $cc;
}
public function variableName(string $slug): string
{
// `->convert()` returns a `Convert` object.
$myConvert = $this->cc->convert($slug);
return $myConvert->toCamel();
}
public function constantName(string $slug): string
{
// Of course you can also chain everything.
return $this->cc->convert($slug)->fromKebab()->toMacro();
}
}
Please note that an interface -CaseConverterInterface- is also provided. If
you are using Symfony you can use this interface with Symfony autowiring
to automatically instantiate CaseConverter, otherwise if you are working in a
standalone project you should try php-di project.
Using \Jawira\CaseConverter\CaseConverter::convert is preferred because:
new operator is considered harmful.How can I help you explore Laravel packages today?