String
String transformers collection., (*1)
Library uses transformer concept. You may write new transformer implementing \Zelenin\Ddd\String\Domain\Service\Transformer
interface.
Built-in transformers:
- RawTransformer
uses a string as is
- IntlNormalizeTransformer
normalizes a string (uses php-intl)
- IntlTransliterator
transliterates a string (uses php-intl)
- UrlifyTransformer
cleans a string for friendly url (slugifier)
- TemplateTransformer
transforms a string according a template, (*2)
Installation
Composer
The preferred way to install this extension is through Composer., (*3)
Either run, (*4)
php composer.phar require zelenin/string "~0.0.0"
or add, (*5)
"zelenin/string": "~0.0.0"
to the require section of your composer.json
, (*6)
Usage
$string = 'Jeanne Françoise Julie Adélaïde Récamier';
$transformers = [
new IntlNormalizeTransformer(),
new IntlTransliterateTransformer(),
new UrlifyTransformer(),
new TemplateTransformer('{id}-{slug}', ['{id}' => $model->id])
];
$transformer = new Transformer(new TransformerCollection($transformers));
$slug = $transformer->transform($string); // '12-jeanne-francoise-julie-adelaide-recamier'
You may set some options:, (*7)
$string = 'Jeanne Françoise Julie Adélaïde Récamier';
$transformers = [
new IntlNormalizeTransformer(Normalizer::FORM_C),
new IntlTransliterateTransformer('Russian-Latin/BGN; Any-Latin; Latin-ASCII; NFD; [:Nonspacing Mark:] Remove; NFKC;'),
new UrlifyTransformer('_', false),
new TemplateTransformer('{id}-{slug}', ['{id}' => $model->id])
];
$transformer = new Transformer(new TransformerCollection($transformers));
$slug = $transformer->transform($string);
Author
Aleksandr Zelenin, e-mail: aleksandr@zelenin.me, (*8)