2017 © Pedro PelĂĄez
 

library inflector

Multilingual inflector that transforms words from singular to plural, underscore to camel case, and more.

image

icanboogie/inflector

Multilingual inflector that transforms words from singular to plural, underscore to camel case, and more.

  • Friday, September 29, 2017
  • by olvlvl
  • Repository
  • 7 Watchers
  • 175 Stars
  • 323,135 Installations
  • PHP
  • 63 Dependents
  • 0 Suggesters
  • 16 Forks
  • 4 Open issues
  • 14 Versions
  • 8 % Grown

The README.md

Inflector

Release Code Coverage Downloads, (*1)

A multilingual inflector that transforms words from singular to plural, underscore to camel case, and formats strings in various ways. Inflections are localized, the default English inflections for pluralization, singularization, and uncountable words are kept in lib/Inflections/en.php., (*2)

Inflections are currently available for the following languages:, (*3)

  • English (en)
  • French (fr)
  • Norwegian Bokmal (nb)
  • Portuguese (pt)
  • Spanish (es)
  • Turkish (tr)

Installation

composer require icanboogie/inflector

Usage

These are some examples of the inflector with the en locale (default)., (*4)

<?php

use ICanBoogie\Inflector;

$inflector = Inflector::get(Inflector::DEFAULT_LOCALE);
# or
$inflector = Inflector::get('en');
# or
$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', Inflector::UPCASE_FIRST_LETTER);
# or
$inflector->camelize('active_model');
// 'ActiveModel'

$inflector->camelize('active_model', Inflector::DOWNCASE_FIRST_LETTER);
// 'activeModel'

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

$inflector->camelize('active_model/errors', Inflector::DOWNCASE_FIRST_LETTER);
// 'activeModel\Errors'

# underscore

$inflector->underscore('ActiveModel');               // 'active_model'
$inflector->underscore('ActiveModel\Errors');        // 'active_model/errors'
$inflector->underscore('Less Active Phrase');        // 'less_active_phrase'
$inflector->underscore('Number 1 Test');             // 'number_1_test'
$inflector->underscore('Johnny5 Still Alive');       // 'johnny5_still_alive'
$inflector->underscore('Lots   of   Spaces');        // 'lots_of_spaces'

# 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"

# uncountable

$inflector->is_uncountable("advice");                // true
$inflector->is_uncountable("weather");               // true
$inflector->is_uncountable("cat");                   // false

Static interfaces are also available:, (*5)

<?php

namespace ICanBoogie;

# Static inflector
echo StaticInflector::pluralize('child');            // "children"
echo StaticInflector::pluralize('genou', 'fr');      // "genoux"
echo StaticInflector::singularize('lĂŠrere', 'nb');   // "lĂŠrer"
echo StaticInflector::pluralize('ĂŒĂ§gen', 'tr');      // "ĂŒĂ§genler"

# Helper functions
echo pluralize('child');                             // "children"
echo pluralize('genou', 'fr');                       // "genoux"
echo singularize('lĂŠrere', 'nb');                    // "lĂŠrer"
echo pluralize('ĂŒĂ§gen', 'tr');                       // "ĂŒĂ§genler"

[!WARNING] Since v3.0 the file with the helper functions is no longer included in the autoload. You need to include the file vendor/icanboogie/inflector/lib/helpers.php in your composer.json if you want to continue using these functions., (*6)

About inflections

Inflections are localized, the configurators are kept in lib/Inflections/., (*7)

[!TIP] Since v2.1, these configurators are autoloaded classes, which means, in theory, you could add your own or overwrite those already defined by specifying another ICanBoogie\\Inflections\\ in your composer.json file., (*8)

Acknowledgements

Most of the code and documentation was adapted from Ruby On Rails's Inflector and David Celis' inflections., (*9)

Significant differences:, (*10)

  • Better support of accented characters.
  • The Ruby module separator :: as been replaced by the PHP namespace separator \.
  • The plural of "octopus" is "octopuses" (not "octopi"), the plural of "virus" is "viruses" (not viri), and the plural of "cow" is "cows" (not "kine").
  • The following methods have been removed: tableize, classify, demodulize, constantize, deconstantize and foreign_key. They can be easily implemented in specific inflectors.
  • Added the hyphenate method, which is a combination of underscore and dasherize.
  • One specifies true rather than false to camelize() to downcase the first letter of the camel-cased string.

Getting started

Inflector expects to work in UTF-8, which is the default encoding character set starting PHP 5.6, if your configuration is different, use mb_internal_encoding() as follows:, (*11)

<?php

namespace ICanBoogie;

// 


mb_internal_encoding('UTF-8');

titleize("Ă©tĂ©_aux_Ăąmes_inouĂŻes"); // ÉtĂ© Aux Âmes InouĂŻes

Continuous Integration

The project is continuously tested by GitHub actions., (*12)

Tests Static Analysis Code Style, (*13)

Code of Conduct

This project adheres to a Contributor Code of Conduct. By participating in this project and its community, you're expected to uphold this code., (*14)

Contributing

See CONTRIBUTING for details., (*15)

The Versions

29/09 2017

dev-master

9999999-dev http://icanboogie.org/

Multilingual inflector that transforms words from singular to plural, underscore to camel case, and more.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.4
  • ext-mbstring *

 

multilingual inflect underscore singularize pluralize hyphenate camelize

29/09 2017

dev-travis-php53

dev-travis-php53 http://icanboogie.org/

Multilingual inflector that transforms words from singular to plural, underscore to camel case, and more.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.4
  • ext-mbstring *

 

multilingual inflect underscore singularize pluralize hyphenate camelize

30/06 2017

v1.4.4

1.4.4.0 http://icanboogie.org/

Multilingual inflector that transforms words from singular to plural, underscore to camel case, and more.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.4
  • ext-mbstring *

 

multilingual inflect underscore singularize pluralize hyphenate camelize

24/04 2017

v1.4.3

1.4.3.0 http://icanboogie.org/

Multilingual inflector that transforms words from singular to plural, underscore to camel case, and more.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.4
  • ext-mbstring *

 

multilingual inflect underscore singularize pluralize hyphenate camelize

15/12 2016

v1.4.2

1.4.2.0 http://icanboogie.org/

Multilingual inflector that transforms words from singular to plural, underscore to camel case, and more.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.4
  • ext-mbstring *

 

multilingual inflect underscore singularize pluralize hyphenate camelize

02/07 2016

v1.4.1

1.4.1.0 http://icanboogie.org/

Multilingual inflector that transforms words from singular to plural, underscore to camel case, and more.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.4
  • ext-mbstring *

 

multilingual inflect underscore singularize pluralize hyphenate camelize

03/11 2015

v1.4.0

1.4.0.0 http://icanboogie.org/

Multilingual inflector that transforms words from singular to plural, underscore to camel case, and more.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.4
  • ext-mbstring *

 

multilingual inflect underscore singularize pluralize hyphenate camelize

03/11 2015

dev-spanish_update

dev-spanish_update http://icanboogie.org/

Multilingual inflector that transforms words from singular to plural, underscore to camel case, and more.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.4
  • ext-mbstring *

 

multilingual inflect underscore singularize pluralize hyphenate camelize

18/08 2015

dev-filtering_titleize

dev-filtering_titleize http://icanboogie.org/

Multilingual inflector that transforms words from singular to plural, underscore to camel case, and more.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.4
  • ext-mbstring *

 

multilingual inflect underscore singularize pluralize hyphenate camelize

18/08 2015

v1.3.3

1.3.3.0 http://icanboogie.org/

Multilingual inflector that transforms words from singular to plural, underscore to camel case, and more.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.4
  • ext-mbstring *

 

multilingual inflect underscore singularize pluralize hyphenate camelize

16/09 2014

v1.3.2

1.3.2.0 http://icanboogie.org/

The Inflector transforms words from singular to plural, underscore to camel case, titelize words and more.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

inflect underscore singularize pluralize hyphenate camelize

11/09 2014

v1.3.0

1.3.0.0 http://icanboogie.org/

The Inflector transforms words from singular to plural, underscore to camel case, titelize words and more.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

inflect underscore singularize pluralize hyphenate camelize

03/01 2014

v1.2.0

1.2.0.0 http://icanboogie.org/

The Inflector transforms words from singular to plural, underscore to camel case, titelize words and more.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

inflect underscore singularize pluralize hyphenate camelize

14/03 2013

v1.0.0

1.0.0.0 http://icanboogie.org/

The Inflector transforms words from singular to plural, underscore to camel case, titelize words and more.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

inflect underscore singularize pluralize hyphenate camelize