ICanBoogie
  • Documentation
  • API Reference
  • Inflector v1.4.2
Namespaces
  • ICanBoogie
Classes
  • Inflections
  • Inflector
Constants
  • INFLECTOR_DEFAULT_LOCALE
Functions
  • camelize
  • capitalize
  • downcase
  • humanize
  • hyphenate
  • pluralize
  • singularize
  • titleize
  • underscore
  • upcase

Class Inflector

The Inflector transforms words from singular to plural, class names to table names, modularized class names to ones without, and class names to foreign keys. Inflections can be localized, the default english inflections for pluralization, singularization, and uncountable words are kept in lib/inflections/en.php.

Namespace: ICanBoogie
Located at inflector.php

Methods summary

public static get( string $locale = self::DEFAULT_LOCALE ) : ICanBoogie\Inflector

Returns an inflector for the specified locale.

Returns an inflector for the specified locale.

Note: Inflectors are shared for the same locale. If you need to alter an inflector you MUST clone it first.

Parameters

$locale

Returns

ICanBoogie\Inflector
protected __construct( ICanBoogie\Inflections $inflections = null )

Initializes the ICanBoogie\Inflector::$inflections property.

Initializes the ICanBoogie\Inflector::$inflections property.

Parameters

$inflections
public __get( string $property )

Returns the ICanBoogie\Inflector::$inflections property.

Returns the ICanBoogie\Inflector::$inflections property.

Parameters

$property

Throws

PropertyNotDefined

in attempt to read an inaccessible property. If the PropertyNotDefined class is not available a \InvalidArgumentException is thrown instead.

public __clone( void )

Clone inflections.

Clone inflections.

public pluralize( string $word ) : string

Returns the plural form of the word in the string.

Returns the plural form of the word in the string.

$this->pluralize('post');       // "posts"
$this->pluralize('children');   // "child"
$this->pluralize('sheep');      // "sheep"
$this->pluralize('words');      // "words"
$this->pluralize('CamelChild'); // "CamelChild"

Parameters

$word

Returns

string
public singularize( string $word ) : string

The reverse of pluralize, returns the singular form of a word in a string.

The reverse of pluralize, returns the singular form of a word in a string.

$this->singularize('posts');         // "post"
$this->singularize('childred');      // "child"
$this->singularize('sheep');         // "sheep"
$this->singularize('word');          // "word"
$this->singularize('CamelChildren'); // "CamelChild"

Parameters

$word

Returns

string
public camelize( string $term, boolean $downcase_first_letter = self::UPCASE_FIRST_LETTER ) : string

By default, camelize converts strings to UpperCamelCase.

By default, camelize converts strings to UpperCamelCase.

camelize will also convert "/" to "\" which is useful for converting paths to namespaces.

$this->camelize('active_model');                // 'ActiveModel'
$this->camelize('active_model', true);          // 'activeModel'
$this->camelize('active_model/errors');         // 'ActiveModel\Errors'
$this->camelize('active_model/errors', true);   // 'activeModel\Errors'

As a rule of thumb you can think of camelize as the inverse of underscore, though there are cases where that does not hold:

$this->camelize($this->underscore('SSLError')); // "SslError"

Parameters

$term
$downcase_first_letter

One of ICanBoogie\Inflector::UPCASE_FIRST_LETTER, ICanBoogie\Inflector::DOWNCASE_FIRST_LETTER.

Returns

string
public underscore( string $camel_cased_word ) : string

Makes an underscored, lowercase form from the expression in the string.

Makes an underscored, lowercase form from the expression in the string.

Changes "\" to "/" to convert namespaces to paths.

$this->underscore('ActiveModel');        // 'active_model'
$this->underscore('ActiveModel\Errors'); // 'active_model/errors'

As a rule of thumb you can think of underscore as the inverse of camelize(), though there are cases where that does not hold:

$this->camelize($this->underscore('SSLError')); // "SslError"

Parameters

$camel_cased_word

Returns

string
public humanize( string $lower_case_and_underscored_word ) : string

Capitalizes the first word and turns underscores into spaces and strips a trailing "_id", if any. Like titleize(), this is meant for creating pretty output.

Capitalizes the first word and turns underscores into spaces and strips a trailing "_id", if any. Like titleize(), this is meant for creating pretty output.

$this->humanize('employee_salary'); // "Employee salary"
$this->humanize('author_id');       // "Author"

Parameters

$lower_case_and_underscored_word

Returns

string
public titleize( string $str ) : string

Capitalizes all the words and replaces some characters in the string to create a nicer looking title. titleize() is meant for creating pretty output. It is not used in the Rails internals.

Capitalizes all the words and replaces some characters in the string to create a nicer looking title. titleize() is meant for creating pretty output. It is not used in the Rails internals.

$this->titleize('man from the boondocks');  // "Man From The Boondocks"
$this->titleize('x-men: the last stand');   // "X Men: The Last Stand"
$this->titleize('TheManWithoutAPast');      // "The Man Without A Past"
$this->titleize('raiders_of_the_lost_ark'); // "Raiders Of The Lost Ark"

Parameters

$str

Returns

string
public dasherize( string $underscored_word ) : string

Replaces underscores with dashes in the string.

Replaces underscores with dashes in the string.

$this->dasherize('puni_puni'); // "puni-puni"

Parameters

$underscored_word

Returns

string
public hyphenate( string $str ) : string

Makes an hyphenated, lowercase form from the expression in the string.

Makes an hyphenated, lowercase form from the expression in the string.

This is a combination of underscore and ICanBoogie\Inflector::dasherize().

Parameters

$str

Returns

string
public ordinal( integer $number ) : string

Returns the suffix that should be added to a number to denote the position in an ordered sequence such as 1st, 2nd, 3rd, 4th.

Returns the suffix that should be added to a number to denote the position in an ordered sequence such as 1st, 2nd, 3rd, 4th.

$this->ordinal(1);     // "st"
$this->ordinal(2);     // "nd"
$this->ordinal(1002);  // "nd"
$this->ordinal(1003);  // "rd"
$this->ordinal(-11);   // "th"
$this->ordinal(-1021); // "st"

Parameters

$number

Returns

string
public ordinalize( integer $number ) : string

Turns a number into an ordinal string used to denote the position in an ordered sequence such as 1st, 2nd, 3rd, 4th.

Turns a number into an ordinal string used to denote the position in an ordered sequence such as 1st, 2nd, 3rd, 4th.

$this->ordinalize(1);     // "1st"
$this->ordinalize(2);     // "2nd"
$this->ordinalize(1002);  // "1002nd"
$this->ordinalize(1003);  // "1003rd"
$this->ordinalize(-11);   // "-11th"
$this->ordinalize(-1021); // "-1021st"

Parameters

$number

Returns

string

Constants summary

DEFAULT_LOCALE

Default inflector locale.

Default inflector locale.

Alias to ICanBoogie\INFLECTOR_DEFAULT_LOCALE.

ICanBoogie\INFLECTOR_DEFAULT_LOCALE
DOWNCASE_FIRST_LETTER : boolean

camelize() option to downcase the first letter.

camelize() option to downcase the first letter.

true
UPCASE_FIRST_LETTER : boolean

camelize() option to keep the first letter as is.

camelize() option to keep the first letter as is.

false

Properties summary

protected $inflections : ICanBoogie\Inflections

Inflections used by the inflector.

Inflections used by the inflector.


		

Magic properties

public read-only $inflections : ICanBoogie\Inflections

Inflections used by the inflector.

Inflector v1.4.2 – Check on GitHub – API documentation generated by ApiGen