Inflector
in package
Horde Inflector class.
Tags
Table of Contents
- $_cache : array<string|int, mixed>
- Inflection cache
- $_pluralizationRules : array<string|int, mixed>
- Rules for pluralizing English nouns.
- $_singularizationRules : array<string|int, mixed>
- Rules for singularizing English nouns.
- $_uncountables : array<string|int, mixed>
- An array of words with the same singular and plural spellings.
- $_uncountables_keys : array<string|int, mixed>
- Flipped map of uncountables
- __construct() : mixed
- Constructor.
- camelize() : string
- Camel-cases a word.
- classify() : mixed
- Creates a class name from a table name like Rails does for table names to models.
- clearCache() : mixed
- Clears the inflection cache.
- dasherize() : mixed
- Replaces underscores with dashes in the string.
- demodulize() : mixed
- Removes the module part from the expression in the string.
- foreignKey() : mixed
- Creates a foreign key name from a class name.
- getCache() : string
- Retuns a cached inflection.
- humanize() : mixed
- Capitalizes the first word and turns underscores into spaces and strips _id.
- ordinalize() : mixed
- Turns a number into an ordinal string used to denote the position in an ordered sequence such as 1st, 2nd, 3rd, 4th.
- pluralize() : string
- Singular English word to pluralize.
- setCache() : string
- Caches an inflection.
- singularize() : string
- Plural English word to singularize.
- tableize() : mixed
- Creates the name of a table like Rails does for models to table names.
- titleize() : mixed
- Capitalizes all the words and replaces some characters in the string to create a nicer looking title.
- uncountable() : mixed
- Adds an uncountable word.
- underscore() : mixed
- The reverse of camelize().
Properties
$_cache
Inflection cache
protected
array<string|int, mixed>
$_cache
= []
$_pluralizationRules
Rules for pluralizing English nouns.
protected
array<string|int, mixed>
$_pluralizationRules
= ['/move$/i' => 'moves', '/sex$/i' => 'sexes', '/child$/i' => 'children', '/man$/i' => 'men', '/foot$/i' => 'feet', '/person$/i' => 'people', '/(quiz)$/i' => '$1zes', '/^(ox)$/i' => '$1en', '/(m|l)ouse$/i' => '$1ice', '/(matr|vert|ind)ix|ex$/i' => '$1ices', '/(x|ch|ss|sh)$/i' => '$1es', '/([^aeiouy]|qu)ies$/i' => '$1y', '/([^aeiouy]|qu)y$/i' => '$1ies', '/(?:([^f])fe|([lr])f)$/i' => '$1$2ves', '/sis$/i' => 'ses', '/([ti])um$/i' => '$1a', '/(buffal|tomat)o$/i' => '$1oes', '/(bu)s$/i' => '$1ses', '/(alias|status)$/i' => '$1es', '/(octop|vir)us$/i' => '$1i', '/(ax|test)is$/i' => '$1es', '/s$/i' => 's', '/$/' => 's']
$_singularizationRules
Rules for singularizing English nouns.
protected
array<string|int, mixed>
$_singularizationRules
= ['/cookies$/i' => 'cookie', '/moves$/i' => 'move', '/sexes$/i' => 'sex', '/children$/i' => 'child', '/men$/i' => 'man', '/feet$/i' => 'foot', '/people$/i' => 'person', '/databases$/i' => 'database', '/(quiz)zes$/i' => '\\1', '/(matr)ices$/i' => '\\1ix', '/(vert|ind)ices$/i' => '\\1ex', '/^(ox)en/i' => '\\1', '/(alias|status)es$/i' => '\\1', '/([octop|vir])i$/i' => '\\1us', '/(cris|ax|test)es$/i' => '\\1is', '/(shoe)s$/i' => '\\1', '/(o)es$/i' => '\\1', '/(bus)es$/i' => '\\1', '/([m|l])ice$/i' => '\\1ouse', '/(x|ch|ss|sh)es$/i' => '\\1', '/(m)ovies$/i' => '\\1ovie', '/(s)eries$/i' => '\\1eries', '/([^aeiouy]|qu)ies$/i' => '\\1y', '/([lr])ves$/i' => '\\1f', '/(tive)s$/i' => '\\1', '/(hive)s$/i' => '\\1', '/([^f])ves$/i' => '\\1fe', '/(^analy)ses$/i' => '\\1sis', '/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => '\\1\\2sis', '/([ti])a$/i' => '\\1um', '/(n)ews$/i' => '\\1ews', '/(.*)s$/i' => '\\1']
$_uncountables
An array of words with the same singular and plural spellings.
protected
array<string|int, mixed>
$_uncountables
= ['aircraft', 'cannon', 'deer', 'equipment', 'fish', 'information', 'money', 'moose', 'rice', 'series', 'sheep', 'species', 'swine']
$_uncountables_keys
Flipped map of uncountables
protected
array<string|int, mixed>
$_uncountables_keys
= []
Methods
__construct()
Constructor.
public
__construct() : mixed
Stores a map of the uncountable words for quicker checks.
Return values
mixed —camelize()
Camel-cases a word.
public
camelize(string $word[, string $firstLetter = 'upper' ]) : string
Parameters
- $word : string
-
The word to camel-case.
- $firstLetter : string = 'upper'
-
Whether to upper or lower case the first. letter of each slash-separated section.
Tags
Return values
string —Camelized $word
classify()
Creates a class name from a table name like Rails does for table names to models.
public
classify(mixed $tableName) : mixed
Examples:
- classify("egg_and_hams") => "EggAndHam"
- classify("post") => "Post"
Parameters
- $tableName : mixed
Return values
mixed —clearCache()
Clears the inflection cache.
public
clearCache() : mixed
Return values
mixed —dasherize()
Replaces underscores with dashes in the string.
public
dasherize(mixed $underscoredWord) : mixed
Example:
- dasherize("puni_puni") => "puni-puni"
Parameters
- $underscoredWord : mixed
Return values
mixed —demodulize()
Removes the module part from the expression in the string.
public
demodulize(mixed $classNameInModule) : mixed
Examples:
- demodulize("Fax_Job") => "Job"
- demodulize("User") => "User"
Parameters
- $classNameInModule : mixed
Return values
mixed —foreignKey()
Creates a foreign key name from a class name.
public
foreignKey(mixed $className[, mixed $separateClassNameAndIdWithUnderscore = true ]) : mixed
$separateClassNameAndIdWithUnderscore sets whether the method should put '_' between the name and 'id'.
Examples:
- foreignKey("Message") => "message_id"
- foreignKey("Message", false) => "messageid"
- foreignKey("Fax_Job") => "fax_job_id"
Parameters
- $className : mixed
- $separateClassNameAndIdWithUnderscore : mixed = true
Return values
mixed —getCache()
Retuns a cached inflection.
public
getCache(mixed $word, mixed $rule) : string
Parameters
- $word : mixed
- $rule : mixed
Return values
string —| false
humanize()
Capitalizes the first word and turns underscores into spaces and strips _id.
public
humanize(mixed $lowerCaseAndUnderscoredWord) : mixed
Like titleize(), this is meant for creating pretty output.
Examples:
- humanize("employee_salary") => "Employee salary"
- humanize("author_id") => "Author"
Parameters
- $lowerCaseAndUnderscoredWord : mixed
Return values
mixed —ordinalize()
Turns a number into an ordinal string used to denote the position in an ordered sequence such as 1st, 2nd, 3rd, 4th.
public
ordinalize(mixed $number) : mixed
Examples:
- ordinalize(1) => "1st"
- ordinalize(2) => "2nd"
- ordinalize(1002) => "1002nd"
- ordinalize(1003) => "1003rd"
Parameters
- $number : mixed
Return values
mixed —pluralize()
Singular English word to pluralize.
public
pluralize(string $word) : string
Parameters
- $word : string
-
Word to pluralize.
Return values
string —Plural form of $word.
setCache()
Caches an inflection.
public
setCache(string $word, string $rule, string $value) : string
Parameters
- $word : string
-
The word being inflected.
- $rule : string
-
The inflection rule.
- $value : string
-
The inflected value of $word.
Return values
string —The inflected value
singularize()
Plural English word to singularize.
public
singularize(string $word) : string
Parameters
- $word : string
-
Word to singularize.
Return values
string —Singular form of $word.
tableize()
Creates the name of a table like Rails does for models to table names.
public
tableize(mixed $className) : mixed
This method uses the pluralize() method on the last word in the string.
Examples:
- tableize("RawScaledScorer") => "raw_scaled_scorers"
- tableize("egg_and_ham") => "egg_and_hams"
- tableize("fancyCategory") => "fancy_categories"
Parameters
- $className : mixed
Return values
mixed —titleize()
Capitalizes all the words and replaces some characters in the string to create a nicer looking title.
public
titleize(mixed $word) : mixed
Titleize is meant for creating pretty output.
See:
- http://daringfireball.net/2008/05/title_case
- http://daringfireball.net/2008/08/title_case_update
Examples:
- titleize("man from the boondocks") => "Man From The Boondocks"
- titleize("x-men: the last stand") => "X Men: The Last Stand"
Parameters
- $word : mixed
Return values
mixed —uncountable()
Adds an uncountable word.
public
uncountable(string $word) : mixed
Parameters
- $word : string
-
The uncountable word.
Return values
mixed —underscore()
The reverse of camelize().
public
underscore(mixed $camelCasedWord) : mixed
Makes an underscored form from the expression in the string.
Examples:
- underscore("ActiveRecord") => "active_record"
- underscore("ActiveRecord_Errors") => "active_record_errors"
Parameters
- $camelCasedWord : mixed