Skip to content

Instantly share code, notes, and snippets.

@hobodave
Created September 30, 2008 18:15
Show Gist options
  • Select an option

  • Save hobodave/13900 to your computer and use it in GitHub Desktop.

Select an option

Save hobodave/13900 to your computer and use it in GitHub Desktop.
<?php
/**
* Hobo
*
* LICENSE
*
* Copyright (c) 2008 David Abdemoulaie
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @category Hobo
* @package Hobo_Inflector
* @copyright Copyright (c) 2008 David Abdemoulaie
* @license MIT License
* @author David Abdemoulaie
**/
/**
* Hobo_Inflector has static methods for inflecting text.
*
* @category Hobo
* @package Hobo_Inflector
* @author David Abdemoulaie
**/
class Hobo_Inflector
{
/**
* undocumented class variable
* @var string
**/
protected static $_uncountables = array(
'equipment', 'information', 'rice',
'money', 'species', 'fish', 'sheep');
protected static $_plurals = array(
'/c(?i)ow$/' => 'kine',
'/C(?i)ow$/' => 'Kine',
'/(m)ove$/i' => '\\1oves',
'/(s)ex$/i' => '\\1exes',
'/(c)hild$/i' => '\\1en',
'/(m)an$/i' => '\\1en',
'/(p)erson$/i' => '\\1eople',
'/(quiz)$/i' => '\\1zes',
'/^(ox)$/i' => '\\1en',
'/([m|l])ouse$/i' => '\\ice',
'/(matr|vert|ind)(?:ix|ex)$/i' => '\\1ices',
'/(x|ch|ss|sh)$/i' => '\\1es',
'/([^aeiouy]|qu)y$/i' => '\\1ies',
'/(hive)$/i' => '\\1s',
'/(?:([^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');
/**
* Pluralize a string.
*
* @param string $string
* @return string
* @author David Abdemoulaie
**/
public static function pluralize($string)
{
if (empty($string) || in_array(strtolower($string), self::$_uncountables)) {
return $string;
} else {
foreach (self::$_plurals as $pattern => $replace) {
if (preg_match($pattern, $string)) {
return preg_replace($pattern, $replace, $string);
}
}
}
}
/**
* Humanize a string. Converts '_' to ' ', strips '_id',
* and capitalizes the first word.
*
* Converts 'ticket_id' to 'Ticket'.
*
* @param string $string
* @return string
* @author David Abdemoulaie
**/
public static function humanize($string)
{
return ucfirst(preg_replace('/_/', ' ',
preg_replace('/_id/', '', $string)));
}
} // END class Hobo_Inflector
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment