PHP Class Phactory\Inflector

This Inflector is a port of Ruby on Rails Inflector. It can be really helpful for developers that want to create frameworks based on naming conventions rather than configurations. It was ported to PHP for the Akelos Framework, a multilingual Ruby on Rails like framework for PHP that will be launched soon.
Since: 0.1
Datei anzeigen Open project: chriskite/phactory Class Usage Examples

Public Methods

Method Description
camelize ( string $word ) : string Returns given word as CamelCased
classify ( string $table_name ) : string Converts a table name to its class name according to rails naming conventions.
humanize ( string $word, string $uppercase = '' ) : string Returns a human-readable string from $word
ordinalize ( integer $number ) : string Converts number to its ordinal English form.
pluralize ( string $word ) : string Pluralizes English nouns.
singularize ( string $word ) : string Singularizes English nouns.
tableize ( string $class_name ) : string Converts a class name to its table name according to rails naming conventions.
titleize ( string $word, string $uppercase = '' ) : string Converts an underscored or CamelCase word into a English sentence.
underscore ( string $word ) : string Converts a word "into_it_s_underscored_version"
variablize ( string $word ) : string Same as camelize but first char is underscored

Method Details

camelize() public method

Converts a word like "send_email" to "SendEmail". It will remove non alphanumeric character from the word, so "who's online" will be converted to "WhoSOnline"
See also: variablize
public camelize ( string $word ) : string
$word string Word to convert to camel case
return string UpperCamelCasedWord

classify() public method

Converts "people" to "Person"
See also: tableize
public classify ( string $table_name ) : string
$table_name string Table name for getting related ClassName.
return string SingularClassName

humanize() public method

Returns a human-readable string from $word, by replacing underscores with a space, and by upper-casing the initial character by default. If you need to uppercase all the words you just have to pass 'all' as a second parameter.
public humanize ( string $word, string $uppercase = '' ) : string
$word string String to "humanize"
$uppercase string If set to 'all' it will uppercase all the words instead of just the first one.
return string Human-readable word

ordinalize() public method

This method converts 13 to 13th, 2 to 2nd ...
public ordinalize ( integer $number ) : string
$number integer Number to get its ordinal value
return string Ordinal representation of given string.

pluralize() public static method

Pluralizes English nouns.
public static pluralize ( string $word ) : string
$word string English noun to pluralize
return string Plural noun

singularize() public method

Singularizes English nouns.
public singularize ( string $word ) : string
$word string English noun to singularize
return string Singular noun.

tableize() public method

Converts "Person" to "people"
See also: classify
public tableize ( string $class_name ) : string
$class_name string Class name for getting related table_name.
return string plural_table_name

titleize() public method

The titleize function converts text like "WelcomePage", "welcome_page" or "welcome page" to this "Welcome Page". If second parameter is set to 'first' it will only capitalize the first character of the title.
public titleize ( string $word, string $uppercase = '' ) : string
$word string Word to format as tile
$uppercase string If set to 'first' it will only uppercase the first character. Otherwise it will uppercase all the words in the title.
return string Text formatted as title

underscore() public method

Convert any "CamelCased" or "ordinary Word" into an "underscored_word". This can be really useful for creating friendly URLs.
public underscore ( string $word ) : string
$word string Word to underscore
return string Underscored word

variablize() public method

Converts a word like "send_email" to "sendEmail". It will remove non alphanumeric character from the word, so "who's online" will be converted to "whoSOnline"
See also: camelize
public variablize ( string $word ) : string
$word string Word to lowerCamelCase
return string Returns a lowerCamelCasedWord