, (*1)
Tale Inflector
What is Tale Inflector?
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., (*2)
It can also generate the plural or singular of a string or ordinalize numbers., (*3)
Installation
composer require talesoft/tale-inflector
Usage
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
Available strategies/static methods
camelize
Tale\Inflector\Strategy\CamelCaseStrategy, (*4)
some Random string = SomeRandomString
dasherize
Tale\Inflector\Strategy\DashRejoinStrategy, (*5)
some Random string = some-Random-string
canonicalize
Tale\Inflector\Strategy\KebabCaseStrategy, (*6)
some Random string = some-random-string
variableize
Tale\Inflector\Strategy\LowerCamelCaseStrategy, (*7)
some Random string = someRandomString
constantize
Tale\Inflector\Strategy\MacroCaseStrategy, (*8)
some Random string = SOME_RANDOM_STRING
tableize
Tale\Inflector\Strategy\SnakeCaseStrategy, (*9)
some Random string = some_random_string
underscorize
Tale\Inflector\Strategy\UnderscoreRejoinStrategy, (*10)
some Random string = some_Random_string
humanize
Tale\Inflector\Strategy\UppercaseWordsStrategy, (*11)
some Random string = Some Random String
ordinalize
Tale\Inflector\Strategy\NumberOrdinalStrategy, (*12)
1 = 1st
12 = 12th
23 = 23rd
pluralize
Tale\Inflector\Strategy\MacroCaseStrategy, (*13)
rabbit = rabbits
car = cars
house = houses
singularize
Tale\Inflector\Strategy\MacroCaseStrategy, (*14)
rabbits = rabbit
cars = car
houses = house
Roll your own
```php
use Tale\Inflector\StrategyInterface;, (*15)
class MyInflectionStrategy implements StrategyInterface
{
public function inflect(string $string): string
{
return "!! {$string} !!";
}
}, (*16)
$inflector->inflect('Test', [MyInflectionStrategy::class]); //!! Test !!
```, (*17)
You can register your own short names, (*18)
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 !!