Inflector Class

The Inflector class allows you to transforms words from singular to plural, class names to table names, modularized class names to ones without, and class names to foreign keys.

Inflector rulesets can be defined as language files, to be able to localize them. The framework comes with a default set of rules for the English language, which can act as a template for localisation.

ascii($str, $allow_non_ascii = false)

The ascii method allows you to translate a string to a 7-bit ASCII string. This method only works with UTF-8.

Static Yes
Parameters
Param Default Description
$str Required The string to translate.
$allow_non_ascii
false
Whether to allow non ASCII chars.
Returns string
Example
echo Inflector::ascii('Inglés'); // returns Ingles

camelize($underscored_word)

The camelize method allows you to convert a string with words separated by underscores into a CamelCased string.

Static Yes
Parameters
Param Default Description
$underscored_word Required The underscored word.
Returns string
Example
echo Inflector::camelize('apples_and_oranges'); // returns ApplesAndOranges

words_to_upper($class)

The words_to_upper method uppercases the first letter of each word in a classname with the words separated by underscores.

Static Yes
Parameters
Param Default Description
$class Required The classname.
$sep _ The separator
Returns string
Example
echo Inflector::words_to_upper('fuel_users'); // returns Fuel_Users
echo Inflector::words_to_upper('module::method', '::'); // returns Module::Method

classify($table_name)

The classify method allows you to convert a table name to a class name.

Static Yes
Parameters
Param Default Description
$table_name Required The table name.
$singularize
true
Whether the $tablename is singularized before its words are uppercased.
Returns string
Example
echo Inflector::classify('fuel_users'); // returns Fuel_User

demodulize($class_name_in_module)

The demodulize method allows you to take the class name out of a modulized string.

Static Yes
Parameters
Param Default Description
$class_name_in_module Required The modulized class.
Returns string
Example
echo Inflector::demodulize('Uri::main()'); // returns main()

denamespace($class_name)

The denamespace method allows you to take the namespace off the given class name.

Static Yes
Parameters
Param Default Description
$class_name required The class name.
Returns string
Example
echo Inflector::denamespace('Fuel\\Core\\Config'); // returns Config

get_namespace($class_name)

The get_namespace method returns the namespace of the given class name..

Static Yes
Parameters
Param Default Description
$class_name required The class name.
Returns string, the namespace
Example
echo Inflector::get_namespace('Fuel\\Core\\Config'); // returns Fuel\Core\

foreign_key($class_name, $use_underscore = true)

The foreign_key method allows you to get the foreign key for a given class.

Static Yes
Parameters
Param Default Description
$class_name Required The class name.
$use_underscore
true
Whether to use an underscore or not.
Returns string
Example
echo Inflector::foreign_key('Inflector'); // returns inflector_id
echo Inflector::foreign_key('Inflector', false); // returns inflectorid

friendly_title($str, $sep = '-', $lowercase = false, $allow_non_ascii = false)

The friendly_title method allows you to convert your text to a URL-friendly title so that it can be used in the URL. It only works with UTF-8 input and outputs 7 bit ASCII characters.

Static Yes
Parameters
Param Default Description
$str Required The text to convert.
$sep
-
The separator (either - or _)
$lowercase
false
Whether to use lowercase or not.
$allow_non_ascii
false
Whether to allow non ASCII chars.
Returns string
Example
echo Inflector::friendly_title('Fuel is a community driven PHP 5 web framework.', '-', true);
// returns fuel-is-a-community-driven-php-5-web-framework

humanize($lower_case_and_underscored_word)

The humanize method allows you to turn an underscore separated word and turns it into a human looking string.

Static Yes
Parameters
Param Default Description
$lower_case_and_underscored_word Required The word to convert.
Returns string
Example
echo Inflector::humanize('apples_and_oranges'); // returns Apples and oranges

is_countable($word)

The is_countable method allows you check if the given word has a plural version.

Static Yes
Parameters
Param Default Description
$word Required The word to check.
Returns boolean
Example
echo Inflector::is_countable('fish'); // returns false
echo Inflector::is_countable('apple'); // returns true

pluralize($word, $count)

The pluralize method allows you get the plural version of the given word.

Static Yes
Parameters
Param Default Description
$word Required The word you want to pluralize.
$count 0 An instance, which if equal 1 will return the string as a singular.
Returns string
Example
echo Inflector::pluralize('apple'); // returns apples
echo Inflector::pluralize('apple', 1); // returns apple

ordinalize($number)

The ordinalize method allows you to add english language order number suffixes.

Static Yes
Parameters
Param Default Description
$number Required The number you want to ordinalize.
Returns string
Example
echo Inflector::ordinalize(2); // returns 2nd

singularize($word)

The singularize method allows you get the singular version of the given word.

Static Yes
Parameters
Param Default Description
$word Required The word you want to singularize.
Returns string
Example
echo Inflector::singularize('apples'); // returns apple

tableize($class_name)

The tableize method allows you to convert a class name to a table name.

Static Yes
Parameters
Param Default Description
$class_name Required The class name.
Returns string
Example
echo Inflector::tableize('FuelUser'); // returns fuel_users

underscore($camel_cased_word)

The underscore method allows you to convert a CamelCased string into an underscore separated string.

Static Yes
Parameters
Param Default Description
$camel_cased_word Required The CamelCased word.
Returns string
Example
echo Inflector::underscore('ApplesAndOranges'); // returns apples_and_oranges

load_rules()

The load_rules method loads the inflector rules based on the current language configuration. If no rulesset can be found (i.e. no inflector lang file exists), the class will default to a ruleset for the English language. If you have loaded a ruleset, you change your language configuration, reload again, but the new configuration doesn't have a ruleset defined, Inflector will continue to use the current one.

Upon first use the Inflector class will autoload the the ruleset based on the language configuration. Normally you do not have to use this method.

Static Yes
Parameters None
Example
Inflector::load_rules(); // (re)loads the Inflector ruleset