Fr3nch13/CakePHP Utilities

Inflector
in package

Pluralize and singularize English words.

Inflector pluralizes and singularizes English nouns. Used by CakePHP's naming conventions throughout the framework.

Tags
link
https://book.cakephp.org/4/en/core-libraries/inflector.html

Table of Contents

Properties

$_cache  : array<string, mixed>
Method cache array.
$_initialState  : array<string|int, mixed>
The initial state of Inflector so reset() works.
$_irregular  : array<string, string>
Irregular rules
$_plural  : array<string, string>
Plural inflector rules
$_singular  : array<string, string>
Singular inflector rules
$_uninflected  : array<string|int, string>
Words that should not be inflected

Methods

camelize()  : string
Returns the input lower_case_delimited_string as a CamelCasedString.
classify()  : string
Returns Cake model class name ("Person" for the database table "people".) for given database table.
dasherize()  : string
Returns the input CamelCasedString as an dashed-string.
delimit()  : string
Expects a CamelCasedInputString, and produces a lower_case_delimited_string
humanize()  : string
Returns the input lower_case_delimited_string as 'A Human Readable String'.
pluralize()  : string
Return $word in plural form.
reset()  : void
Clears Inflectors inflected value caches. And resets the inflection rules to the initial values.
rules()  : void
Adds custom inflection $rules, of either 'plural', 'singular', 'uninflected' or 'irregular' $type.
singularize()  : string
Return $word in singular form.
tableize()  : string
Returns corresponding table name for given model $className. ("people" for the model class "Person").
underscore()  : string
Returns the input CamelCasedString as an underscored_string.
variable()  : string
Returns camelBacked version of an underscored string.
_cache()  : string|false
Cache inflected values, and return if already available

Properties

$_cache

Method cache array.

protected static array<string, mixed> $_cache = []

$_initialState

The initial state of Inflector so reset() works.

protected static array<string|int, mixed> $_initialState = []

$_irregular

Irregular rules

protected static array<string, string> $_irregular = ['atlas' => 'atlases', 'beef' => 'beefs', 'brief' => 'briefs', 'brother' => 'brothers', 'cafe' => 'cafes', 'child' => 'children', 'cookie' => 'cookies', 'corpus' => 'corpuses', 'cow' => 'cows', 'criterion' => 'criteria', 'ganglion' => 'ganglions', 'genie' => 'genies', 'genus' => 'genera', 'graffito' => 'graffiti', 'hoof' => 'hoofs', 'loaf' => 'loaves', 'man' => 'men', 'money' => 'monies', 'mongoose' => 'mongooses', 'move' => 'moves', 'mythos' => 'mythoi', 'niche' => 'niches', 'numen' => 'numina', 'occiput' => 'occiputs', 'octopus' => 'octopuses', 'opus' => 'opuses', 'ox' => 'oxen', 'penis' => 'penises', 'person' => 'people', 'sex' => 'sexes', 'soliloquy' => 'soliloquies', 'testis' => 'testes', 'trilby' => 'trilbys', 'turf' => 'turfs', 'potato' => 'potatoes', 'hero' => 'heroes', 'tooth' => 'teeth', 'goose' => 'geese', 'foot' => 'feet', 'foe' => 'foes', 'sieve' => 'sieves', 'cache' => 'caches']

$_plural

Plural inflector rules

protected static array<string, string> $_plural = ['/(s)tatus$/i' => '\1tatuses', '/(quiz)$/i' => '\1zes', '/^(ox)$/i' => '\1\2en', '/([m|l])ouse$/i' => '\1ice', '/(matr|vert)(ix|ex)$/i' => '\1ices', '/(x|ch|ss|sh)$/i' => '\1es', '/([^aeiouy]|qu)y$/i' => '\1ies', '/(hive)$/i' => '\1s', '/(chef)$/i' => '\1s', '/(?:([^f])fe|([lre])f)$/i' => '\1\2ves', '/sis$/i' => 'ses', '/([ti])um$/i' => '\1a', '/(p)erson$/i' => '\1eople', '/(?<!u)(m)an$/i' => '\1en', '/(c)hild$/i' => '\1hildren', '/(buffal|tomat)o$/i' => '\1\2oes', '/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin)us$/i' => '\1i', '/us$/i' => 'uses', '/(alias)$/i' => '\1es', '/(ax|cris|test)is$/i' => '\1es', '/s$/' => 's', '/^$/' => '', '/$/' => 's']

$_singular

Singular inflector rules

protected static array<string, string> $_singular = ['/(s)tatuses$/i' => '\1\2tatus', '/^(.*)(menu)s$/i' => '\1\2', '/(quiz)zes$/i' => '\1', '/(matr)ices$/i' => '\1ix', '/(vert|ind)ices$/i' => '\1ex', '/^(ox)en/i' => '\1', '/(alias|lens)(es)*$/i' => '\1', '/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|viri?)i$/i' => '\1us', '/([ftw]ax)es/i' => '\1', '/(cris|ax|test)es$/i' => '\1is', '/(shoe)s$/i' => '\1', '/(o)es$/i' => '\1', '/ouses$/' => 'ouse', '/([^a])uses$/' => '\1us', '/([m|l])ice$/i' => '\1ouse', '/(x|ch|ss|sh)es$/i' => '\1', '/(m)ovies$/i' => '\1\2ovie', '/(s)eries$/i' => '\1\2eries', '/(s)pecies$/i' => '\1\2pecies', '/([^aeiouy]|qu)ies$/i' => '\1y', '/(tive)s$/i' => '\1', '/(hive)s$/i' => '\1', '/(drive)s$/i' => '\1', '/([le])ves$/i' => '\1f', '/([^rfoa])ves$/i' => '\1fe', '/(^analy)ses$/i' => '\1sis', '/(analy|diagno|^ba|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => '\1\2sis', '/([ti])a$/i' => '\1um', '/(p)eople$/i' => '\1\2erson', '/(m)en$/i' => '\1an', '/(c)hildren$/i' => '\1\2hild', '/(n)ews$/i' => '\1\2ews', '/eaus$/' => 'eau', '/^(.*us)$/' => '\1', '/s$/i' => '']

$_uninflected

Words that should not be inflected

protected static array<string|int, string> $_uninflected = ['.*[nrlm]ese', '.*data', '.*deer', '.*fish', '.*measles', '.*ois', '.*pox', '.*sheep', 'people', 'feedback', 'stadia', '.*?media', 'chassis', 'clippers', 'debris', 'diabetes', 'equipment', 'gallows', 'graffiti', 'headquarters', 'information', 'innings', 'news', 'nexus', 'pokemon', 'proceedings', 'research', 'sea[- ]bass', 'series', 'species', 'weather']

Methods

dasherize()

Returns the input CamelCasedString as an dashed-string.

public static dasherize(string $string) : string

Also replaces underscores with dashes

Parameters
$string : string

The string to dasherize.

Return values
string

Dashed version of the input string

delimit()

Expects a CamelCasedInputString, and produces a lower_case_delimited_string

public static delimit(string $string[, string $delimiter = '_' ]) : string
Parameters
$string : string

String to delimit

$delimiter : string = '_'

the character to use as a delimiter

Return values
string

delimited string

humanize()

Returns the input lower_case_delimited_string as 'A Human Readable String'.

public static humanize(string $string[, string $delimiter = '_' ]) : string

(Underscores are replaced by spaces and capitalized following words.)

Parameters
$string : string

String to be humanized

$delimiter : string = '_'

the character to replace with a space

Tags
link
https://book.cakephp.org/4/en/core-libraries/inflector.html#creating-human-readable-forms
Return values
string

Human-readable string

reset()

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

public static reset() : void

rules()

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

public static rules(string $type, array<string|int, mixed> $rules[, bool $reset = false ]) : void

Usage:

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

The type of inflection, either 'plural', 'singular', or 'uninflected'.

$rules : array<string|int, mixed>

Array of rules to be added.

$reset : bool = false

If true, will unset default inflections for all new rules that are being defined in $rules.

_cache()

Cache inflected values, and return if already available

protected static _cache(string $type, string $key[, string|false $value = false ]) : string|false
Parameters
$type : string

Inflection type

$key : string

Original value

$value : string|false = false

Inflected value

Return values
string|false

Inflected value on cache hit or false on cache miss.


        
On this page

Search results