PHPonTrax
[ class tree: PHPonTrax ] [ index: PHPonTrax ] [ all elements ]

Source for file inflector.php

Documentation is available at inflector.php

  1. <?php
  2. /**
  3.  *  File containing the Inflector class
  4.  *
  5.  *  (PHP 5)
  6.  *
  7.  *  @package PHPonTrax
  8.  *  @version $Id: inflector.php 276 2007-01-22 11:53:53Z john $
  9.  *  @copyright (c) 2005 John Peterson
  10.  *
  11.  *   Permission is hereby granted, free of charge, to any person obtaining
  12.  *   a copy of this software and associated documentation files (the
  13.  *   "Software"), to deal in the Software without restriction, including
  14.  *   without limitation the rights to use, copy, modify, merge, publish,
  15.  *   distribute, sublicense, and/or sell copies of the Software, and to
  16.  *   permit persons to whom the Software is furnished to do so, subject to
  17.  *   the following conditions:
  18.  *
  19.  *   The above copyright notice and this permission notice shall be
  20.  *   included in all copies or substantial portions of the Software.
  21.  *
  22.  *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23.  *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24.  *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25.  *   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26.  *   LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27.  *   OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28.  *   WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29.  */
  30.  
  31.  
  32. include_once(TRAX_LIB_ROOT "/inflections.php");
  33.  
  34. /**
  35.  *  Implement the Trax naming convention
  36.  *
  37.  *  This class provides static methods to implement the
  38.  *  {@tutorial PHPonTrax/naming.pkg Trax naming convention}.
  39.  *  Inflector is never instantiated.
  40.  *  @tutorial PHPonTrax/Inflector.cls
  41.  */
  42. class Inflector {
  43.  
  44.     /**
  45.      *  Pluralize a word according to English rules
  46.      *
  47.      *  Convert a lower-case singular word to plural form.
  48.      *  If $count > 0 then prefixes $word with the $count
  49.      *
  50.      *  @param  string $word  Word to be pluralized
  51.      *  @param  int $count How many of these $words are there
  52.      *  @return string  Plural of $word
  53.      */
  54.     function pluralize($word$count 0{
  55.         if($count == || $count 1{          
  56.             if(!in_array($wordInflections::$uncountables)) 
  57.                 $original $word;   
  58.                 foreach(Inflections::$plurals as $plural_rule{
  59.                     $word preg_replace($plural_rule['rule']$plural_rule['replacement']$word);
  60.                     if($original != $wordbreak;
  61.                 }
  62.             }
  63.         }
  64.         return ($count >= "{$count} {$word}$word);
  65.     }
  66.  
  67.     /**
  68.      *  Singularize a word according to English rules
  69.      *
  70.      *  @param  string $word  Word to be singularized
  71.      *  @return string  Singular of $word
  72.      */
  73.     function singularize($word{
  74.         if(!in_array($wordInflections::$uncountables)) 
  75.             $original $word;   
  76.             foreach(Inflections::$singulars as $singular_rule{
  77.                 $word preg_replace($singular_rule['rule']$singular_rule['replacement']$word);
  78.                 if($original != $wordbreak;
  79.             }
  80.         }
  81.         return $word;
  82.     }
  83.  
  84.     /**
  85.      *  Capitalize a word making it all lower case with first letter uppercase
  86.      *
  87.      *  @param  string $word  Word to be capitalized
  88.      *  @return string Capitalized $word
  89.      */
  90.     function capitalize($word{
  91.         return ucfirst(strtolower($word));     
  92.     }
  93.  
  94.     /**
  95.      *  Convert a phrase from the lower case and underscored form
  96.      *  to the camel case form
  97.      *
  98.      *  @param string $lower_case_and_underscored_word  Phrase to
  99.      *                                                   convert
  100.      *  @return string  Camel case form of the phrase
  101.      */
  102.     function camelize($lower_case_and_underscored_word{
  103.         return str_replace(" ","",ucwords(str_replace("_"," ",$lower_case_and_underscored_word)));
  104.     }
  105.  
  106.     /**
  107.      *  Convert a phrase from the camel case form to the lower case
  108.      *  and underscored form
  109.      *
  110.      *  @param string $camel_cased_word  Phrase to convert
  111.      *  @return string Lower case and underscored form of the phrase
  112.      */
  113.     function underscore($camel_cased_word{
  114.         $camel_cased_word preg_replace('/([A-Z]+)([A-Z])/','\1_\2',$camel_cased_word);
  115.         return strtolower(preg_replace('/([a-z])([A-Z])/','\1_\2',$camel_cased_word));
  116.     }
  117.  
  118.     /**
  119.      *  Generate a more human version of a lower case underscored word
  120.      *
  121.      *  @param string $lower_case_and_underscored_word  A word or phrase in
  122.      *                                            lower_case_underscore form
  123.      *  @return string The input value with underscores replaced by
  124.      *   blanks and the first letter of each word capitalized
  125.      */
  126.     function humanize($lower_case_and_underscored_word{
  127.         return ucwords(str_replace("_"," ",$lower_case_and_underscored_word));
  128.     }
  129.     
  130.     /**
  131.      *  Convert a word or phrase into a title format "Welcome To My Site"
  132.      *
  133.      *  @param string $word  A word or phrase
  134.      *  @return string A string that has all words capitalized and splits on existing caps.
  135.      */    
  136.     function titleize($word{
  137.         return preg_replace('/\b([a-z])/'self::capitalize('$1')self::humanize(self::underscore($word)));
  138.     }
  139.  
  140.     /**
  141.      *  Convert a word's underscores into dashes
  142.      *
  143.      *  @param string $underscored_word  Word to convert
  144.      *  @return string All underscores converted to dashes
  145.      */    
  146.     function dasherize($underscored_word{
  147.         return str_replace('_''-'self::underscore($underscored_word));
  148.     }
  149.  
  150.     /**
  151.      *  Convert a class name to the corresponding table name
  152.      *
  153.      *  The class name is a singular word or phrase in CamelCase.
  154.      *  By convention it corresponds to a table whose name is a plural
  155.      *  word or phrase in lower case underscore form.
  156.      *  @param string $class_name  Name of {@link ActiveRecord} sub-class
  157.      *  @return string Pluralized lower_case_underscore form of name
  158.      */
  159.     function tableize($class_name{
  160.         return self::pluralize(self::underscore($class_name));
  161.     }
  162.  
  163.     /**
  164.      *  Convert a table name to the corresponding class name
  165.      *
  166.      *  @param string $table_name Name of table in the database
  167.      *  @return string Singular CamelCase form of $table_name
  168.      */
  169.     function classify($table_name{
  170.         return self::camelize(self::singularize($table_name));
  171.     }
  172.  
  173.     /**
  174.      *  Get foreign key column corresponding to a table name
  175.      *
  176.      *  @param string $table_name Name of table referenced by foreign
  177.      *     key
  178.      *  @return string Column name of the foreign key column
  179.      */
  180.     function foreign_key($class_name{
  181.         return self::underscore($class_name"_id";
  182.     }
  183.  
  184.  
  185.     /**
  186.      *  Add to a number st, nd, rd, th
  187.      *
  188.      *  @param integer $number Number to append to
  189.      *     key
  190.      *  @return string Number formatted with correct st, nd, rd, or th
  191.      */    
  192.     function ordinalize($number{
  193.         $test (intval($number100);
  194.         if($test >= 11 && $test <= 13{
  195.             $number "{$number}th";
  196.         else {
  197.             switch((intval($number10)) {
  198.                 case 1:
  199.                     $number "{$number}st";
  200.                     break;
  201.                 case 2:
  202.                     $number "{$number}nd";
  203.                     break;
  204.                 case 3:
  205.                     $number "{$number}rd";
  206.                     break;
  207.                 default:
  208.                     $number "{$number}th"
  209.             }    
  210.         }
  211.         return $number;
  212.     }
  213.     
  214. }
  215.  
  216.  
  217. ?>

Documentation generated on Mon, 21 May 2007 22:28:37 -0600 by phpDocumentor 1.3.2