Singuliers, pluriels, acronymes, underscore... l'inflexion en PHPavec ICanBoogie/Inflector

Je viens d'adapter le nouvel inflecteur trop sexy de Ruby on Rails à PHP. Il permet de transformer les mots du singulier au pluriel, de l'underscore au camel case, de transformer des mots en titres… en respectant les mots irréguliers et les acronymes. Les inflexions peuvent être étendues, surchargées et localisées. Des inflexions localisées sont actuellement disponibles pour l'Anglais, l'Espagnol, le Français et le Norvégien Bokmål. Il peut être installé avec Composer.

Jetez un œil au dépôt ICanBoogie/Inflector sur GitHub !

Quelques exemples

<?php

use ICanBoogie\Inflector;

$inflector = Inflector::get();

# pluralize

$inflector->pluralize('post');                       // "posts"
$inflector->pluralize('child');                      // "children"
$inflector->pluralize('sheep');                      // "sheep"
$inflector->pluralize('words');                      // "words"
$inflector->pluralize('CamelChild');                 // "CamelChildren"

# singularize

$inflector->singularize('posts');                    // "post"
$inflector->singularize('children');                 // "child"
$inflector->singularize('sheep');                    // "sheep"
$inflector->singularize('word');                     // "word"
$inflector->singularize('CamelChildren');            // "CamelChild"

# camelize

$inflector->camelize('active_model');                // 'ActiveModel'
$inflector->camelize('active_model'true);          // 'activeModel'
$inflector->camelize('active_model/errors');         // 'ActiveModel\Errors'
$inflector->camelize('active_model/errors'true);   // 'activeModel\Errors'

# underscore

$inflector->underscore('ActiveModel');               // 'active_model'
$inflector->underscore('ActiveModel\Errors');        // 'active_model/errors'

# humanize

$inflector->humanize('employee_salary');             // "Employee salary"
$inflector->humanize('author_id');                   // "Author"

# titleize

$inflector->titleize('man from the boondocks');      // "Man From The Boondocks"
$inflector->titleize('x-men: the last stand');       // "X Men: The Last Stand"
$inflector->titleize('TheManWithoutAPast');          // "The Man Without A Past"
$inflector->titleize('raiders_of_the_lost_ark');     // "Raiders Of The Lost Ark"

# ordinal

$inflector->ordinal(1);                              // "st"
$inflector->ordinal(2);                              // "nd"
$inflector->ordinal(1002);                           // "nd"
$inflector->ordinal(1003);                           // "rd"
$inflector->ordinal(-11);                            // "th"
$inflector->ordinal(-1021);                          // "st"

# ordinalize

$inflector->ordinalize(1);                           // "1st"
$inflector->ordinalize(2);                           // "2nd"
$inflector->ordinalize(1002);                        // "1002nd"
$inflector->ordinalize(1003);                        // "1003rd"
$inflector->ordinalize(-11);                         // "-11th"
$inflector->ordinalize(-1021);                       // "-1021st"

Des helpeurs permettent d'utiliser très simplement les inflexions localisées :

<?php

namespace ICanBoogie;

echo pluralize('child');                             // "children"
echo pluralize('genou''fr');                       // "genoux"
echo singularize('lærere''nb');                    // "lærer"

Laisser un commentaire

Pas de commentaire