class Inflector (View source)

Doctrine inflector has static methods for inflecting text.

The methods in these classes are from several different sources collected across several different php projects and several different authors. The original author names and emails are not known.

Pluralize & Singularize implementation are borrowed from CakePHP with some modifications.

Methods

static 
tableize(string $word)

Converts a word into the format for a Doctrine table name. Converts 'ModelName' to 'model_name'.

static 
classify(string $word)

Converts a word into the format for a Doctrine class name. Converts 'table_name' to 'TableName'.

static 
camelize(string $word)

Camelizes a word. This uses the classify() method and turns the first character to lowercase.

static string
ucwords(string $string, string $delimiters = " \n\t\r\0\v-")

Uppercases words with configurable delimiters between words.

static 
reset()

Clears Inflectors inflected value caches, and resets the inflection rules to the initial values.

static 
rules(string $type, iterable $rules, bool $reset = false)

Adds custom inflection $rules, of either 'plural' or 'singular' $type.

static string
pluralize(string $word)

Returns a word in plural form.

static string
singularize(string $word)

Returns a word in singular form.

Details

static tableize(string $word)

Converts a word into the format for a Doctrine table name. Converts 'ModelName' to 'model_name'.

Parameters

string $word

static classify(string $word)

Converts a word into the format for a Doctrine class name. Converts 'table_name' to 'TableName'.

Parameters

string $word

static camelize(string $word)

Camelizes a word. This uses the classify() method and turns the first character to lowercase.

Parameters

string $word

static string ucwords(string $string, string $delimiters = " \n\t\r\0\v-")

Uppercases words with configurable delimiters between words.

Takes a string and capitalizes all of the words, like PHP's built-in ucwords function. This extends that behavior, however, by allowing the word delimiters to be configured, rather than only separating on whitespace.

Here is an example:

Parameters

string $string The string to operate on.
string $delimiters A list of word separators.

Return Value

string The string with all delimiter-separated words capitalized.

static reset()

Clears Inflectors inflected value caches, and resets the inflection rules to the initial values.

static rules(string $type, iterable $rules, bool $reset = false)

Adds custom inflection $rules, of either 'plural' or 'singular' $type.

Usage:

{{{ Inflector::rules('plural', ['/^(inflect)or$/i' => '\1ables']); Inflector::rules('plural', [ 'rules' => ['/^(inflect)ors$/i' => '\1ables'], 'uninflected' => ['dontinflectme'], 'irregular' => ['red' => 'redlings'] ]); }}}

Parameters

string $type The type of inflection, either 'plural' or 'singular'
iterable $rules An array of rules to be added. new rules that are being defined in $rules.
bool $reset If true, will unset default inflections for all new rules that are being defined in $rules.

static string pluralize(string $word)

Returns a word in plural form.

Parameters

string $word The word in singular form.

Return Value

string The word in plural form.

static string singularize(string $word)

Returns a word in singular form.

Parameters

string $word The word in plural form.

Return Value

string The word in singular form.