Tale inflector bends strings into different naming styles. A common use-case would be the converting of class-names or property-names to table-names or titles to slugs for URLs.
It can also generate the plural or singular of a string or ordinalize numbers.
composer require talesoft/tale-inflector
use Tale\Inflector;
$inflector = new Inflector();
//Table generation
$inflector->inflect('ProductAttribute', ['tableize', 'pluralize']); //product_attributes
$inflector->inflect('someProperty', ['tableize']); //some_property
//Canonicalization / slugs
$inflector->inflect('Some title I inserted', ['canonicalize']); //some-title-i-inserted
$inflector->inflect('Was höre ich da?', ['canonicalize']); //was-hore-ich-da
//Or just use the static methods for quick access
Inflector::canonicalize('Some random title'); //some-random-title
Tale\Inflector\Strategy\CamelCaseStrategy
some Random string = SomeRandomString
Tale\Inflector\Strategy\DashRejoinStrategy
some Random string = some-Random-string
Tale\Inflector\Strategy\KebabCaseStrategy
some Random string = some-random-string
Tale\Inflector\Strategy\LowerCamelCaseStrategy
some Random string = someRandomString
Tale\Inflector\Strategy\MacroCaseStrategy
some Random string = SOME_RANDOM_STRING
Tale\Inflector\Strategy\SnakeCaseStrategy
some Random string = some_random_string
Tale\Inflector\Strategy\UnderscoreRejoinStrategy
some Random string = some_Random_string
Tale\Inflector\Strategy\UppercaseWordsStrategy
some Random string = Some Random String
Tale\Inflector\Strategy\NumberOrdinalStrategy
1 = 1st
12 = 12th
23 = 23rd
Tale\Inflector\Strategy\MacroCaseStrategy
rabbit = rabbits
car = cars
house = houses
Tale\Inflector\Strategy\MacroCaseStrategy
rabbits = rabbit
cars = car
houses = house
use Tale\Inflector\StrategyInterface;
class MyInflectionStrategy implements StrategyInterface
{
public function inflect(string $string): string
{
return "!! {$string} !!";
}
}
$inflector->inflect('Test', [MyInflectionStrategy::class]); //!! Test !!
You can register your own short names
use Tale\Inflector\StrategyInterface;
class MyInflectionStrategy implements StrategyInterface
{
public function inflect(string $string): string
{
return "!! {$string} !!";
}
}
$inflector->addNamedStrategy('exlamize', MyInflectionStrategy::class);
$inflector->inflect('House', ['pluralize', 'exclamize']); //!! Houses !!