inflector_helper.php

Go to the documentation of this file.
00001 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002 /**
00003  * CodeIgniter
00004  *
00005  * An open source application development framework for PHP 4.3.2 or newer
00006  *
00007  * @package             CodeIgniter
00008  * @author              ExpressionEngine Dev Team
00009  * @copyright   Copyright (c) 2008, EllisLab, Inc.
00010  * @license             http://codeigniter.com/user_guide/license.html
00011  * @link                http://codeigniter.com
00012  * @since               Version 1.0
00013  * @filesource
00014  */
00015 
00016 // ------------------------------------------------------------------------
00017 
00018 /**
00019  * CodeIgniter Inflector Helpers
00020  *
00021  * @package             CodeIgniter
00022  * @subpackage  Helpers
00023  * @category    Helpers
00024  * @author              ExpressionEngine Dev Team
00025  * @link                http://codeigniter.com/user_guide/helpers/directory_helper.html
00026  */
00027 
00028 
00029 // --------------------------------------------------------------------
00030 
00031 /**
00032  * Singular
00033  *
00034  * Takes a plural word and makes it singular
00035  *
00036  * @access      public
00037  * @param       string
00038  * @return      str
00039  */     
00040 if ( ! function_exists('singular'))
00041 {       
00042         function singular($str)
00043         {
00044                 $str = strtolower(trim($str));
00045                 $end = substr($str, -3);
00046         
00047                 if ($end == 'ies')
00048                 {
00049                         $str = substr($str, 0, strlen($str)-3).'y';
00050                 }
00051                 elseif ($end == 'ses')
00052                 {
00053                         $str = substr($str, 0, strlen($str)-2);
00054                 }
00055                 else
00056                 {
00057                         $end = substr($str, -1);
00058                 
00059                         if ($end == 's')
00060                         {
00061                                 $str = substr($str, 0, strlen($str)-1);
00062                         }
00063                 }
00064         
00065                 return $str;
00066         }
00067 }
00068 
00069 // --------------------------------------------------------------------
00070 
00071 /**
00072  * Plural
00073  *
00074  * Takes a singular word and makes it plural
00075  *
00076  * @access      public
00077  * @param       string
00078  * @param       bool
00079  * @return      str
00080  */     
00081 if ( ! function_exists('plural'))
00082 {       
00083         function plural($str, $force = FALSE)
00084         {
00085                 $str = strtolower(trim($str));
00086                 $end = substr($str, -1);
00087 
00088                 if ($end == 'y')
00089                 {
00090                         $str = substr($str, 0, strlen($str)-1).'ies';
00091                 }
00092                 elseif ($end == 's')
00093                 {
00094                         if ($force == TRUE)
00095                         {
00096                                 $str .= 'es';
00097                         }
00098                 }
00099                 else
00100                 {
00101                         $str .= 's';
00102                 }
00103 
00104                 return $str;
00105         }
00106 }
00107 
00108 // --------------------------------------------------------------------
00109 
00110 /**
00111  * Camelize
00112  *
00113  * Takes multiple words separated by spaces or underscores and camelizes them
00114  *
00115  * @access      public
00116  * @param       string
00117  * @return      str
00118  */     
00119 if ( ! function_exists('camelize'))
00120 {       
00121         function camelize($str)
00122         {               
00123                 $str = 'x'.strtolower(trim($str));
00124                 $str = ucwords(preg_replace('/[\s_]+/', ' ', $str));
00125                 return substr(str_replace(' ', '', $str), 1);
00126         }
00127 }
00128 
00129 // --------------------------------------------------------------------
00130 
00131 /**
00132  * Underscore
00133  *
00134  * Takes multiple words separated by spaces and underscores them
00135  *
00136  * @access      public
00137  * @param       string
00138  * @return      str
00139  */     
00140 if ( ! function_exists('underscore'))
00141 {
00142         function underscore($str)
00143         {
00144                 return preg_replace('/[\s]+/', '_', strtolower(trim($str)));
00145         }
00146 }
00147 
00148 // --------------------------------------------------------------------
00149 
00150 /**
00151  * Humanize
00152  *
00153  * Takes multiple words separated by underscores and changes them to spaces
00154  *
00155  * @access      public
00156  * @param       string
00157  * @return      str
00158  */     
00159 if ( ! function_exists('humanize'))
00160 {       
00161         function humanize($str)
00162         {
00163                 return ucwords(preg_replace('/[_]+/', ' ', strtolower(trim($str))));
00164         }
00165 }
00166         
00167 
00168 /* End of file inflector_helper.php */
00169 /* Location: ./system/helpers/inflector_helper.php */