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 195 2006-04-03 22:27:26Z haas $
  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. * Implement the Trax naming convention
  33. *
  34. * This class provides static methods to implement the
  35. * {@tutorial PHPonTrax/naming.pkg Trax naming convention}.
  36. * Inflector is never instantiated.
  37. * @tutorial PHPonTrax/Inflector.cls
  38. */
  39. class Inflector {
  40.  
  41. /**
  42. * Rules for converting an English singular word to plural form
  43. */
  44. private static $plural_rules =
  45. array( '/(x|ch|ss|sh)$/' => '\1es', # search, switch, fix, box, process, address
  46. '/series$/' => '\1series',
  47. '/([^aeiouy]|qu)ies$/' => '\1y',
  48. '/([^aeiouy]|qu)y$/' => '\1ies', # query, ability, agency
  49. '/(?:([^f])fe|([lr])f)$/' => '\1\2ves', # half, safe, wife
  50. '/sis$/' => 'ses', # basis, diagnosis
  51. '/([ti])um$/' => '\1a', # datum, medium
  52. '/person$/' => 'people', # person, salesperson
  53. '/man$/' => 'men', # man, woman, spokesman
  54. '/child$/' => 'children', # child
  55. '/(.*)status$/' => '\1statuses',
  56. '/s$/' => 's', # no change (compatibility)
  57. '/$/' => 's'
  58. );
  59.  
  60. /**
  61. * Rules for converting an English plural word to singular form
  62. */
  63. private static $singular_rules =
  64. array( '/(x|ch|ss)es$/' => '\1',
  65. '/movies$/' => 'movie',
  66. '/series$/' => 'series',
  67. '/([^aeiouy]|qu)ies$/' => '\1y',
  68. '/([lr])ves$/' => '\1f',
  69. '/([^f])ves$/' => '\1fe',
  70. '/(analy|ba|diagno|parenthe|progno|synop|the)ses$/' => '\1sis',
  71. '/([ti])a$/' => '\1um',
  72. '/people$/' => 'person',
  73. '/men$/' => 'man',
  74. '/(.*)statuses$/' => '\1status',
  75. '/children$/' => 'child',
  76. '/news$/' => 'news',
  77. '/s$/' => ''
  78. );
  79.  
  80. /**
  81. * Pluralize a word according to English rules
  82. *
  83. * Convert a lower-case singular word to plural form.
  84. * @param string $word Word to be pluralized
  85. * @return string Plural of $word
  86. */
  87. function pluralize($word) {
  88. $original = $word;
  89. foreach(self::$plural_rules as $rule => $replacement) {
  90. $word = preg_replace($rule,$replacement,$word);
  91. if($original != $word) break;
  92. }
  93. return $word;
  94. }
  95.  
  96. /**
  97. * Singularize a word according to English rules
  98. *
  99. * @param string $word Word to be singularized
  100. * @return string Singular of $word
  101. */
  102. function singularize($word) {
  103. $original = $word;
  104. foreach(self::$singular_rules as $rule => $replacement) {
  105. $word = preg_replace($rule,$replacement,$word);
  106. if($original != $word) break;
  107. }
  108. return $word;
  109. }
  110.  
  111. /**
  112. * Capitalize a word making it all lower case with first letter uppercase
  113. *
  114. * @param string $word Word to be capitalized
  115. * @return string Capitalized $word
  116. */
  117. function capitalize($word) {
  118. return ucfirst(strtolower($word));
  119. }
  120.  
  121. /**
  122. * Convert a phrase from the lower case and underscored form
  123. * to the camel case form
  124. *
  125. * @param string $lower_case_and_underscored_word Phrase to
  126. * convert
  127. * @return string Camel case form of the phrase
  128. */
  129. function camelize($lower_case_and_underscored_word) {
  130. return str_replace(" ","",ucwords(str_replace("_"," ",$lower_case_and_underscored_word)));
  131. }
  132.  
  133. /**
  134. * Convert a phrase from the camel case form to the lower case
  135. * and underscored form
  136. *
  137. * @param string $camel_cased_word Phrase to convert
  138. * @return string Lower case and underscored form of the phrase
  139. */
  140. function underscore($camel_cased_word) {
  141. $camel_cased_word = preg_replace('/([A-Z]+)([A-Z])/','\1_\2',$camel_cased_word);
  142. return strtolower(preg_replace('/([a-z])([A-Z])/','\1_\2',$camel_cased_word));
  143. }
  144.  
  145. /**
  146. * Generate a more human version of a lower case underscored word
  147. *
  148. * @param string $lower_case_and_underscored_word A word or phrase in
  149. * lower_case_underscore form
  150. * @return string The input value with underscores replaced by
  151. * blanks and the first letter of each word capitalized
  152. */
  153. function humanize($lower_case_and_underscored_word) {
  154. return ucwords(str_replace("_"," ",$lower_case_and_underscored_word));
  155. }
  156.  
  157. /**
  158. * Convert a class name to the corresponding table name
  159. *
  160. * The class name is a singular word or phrase in CamelCase.
  161. * By convention it corresponds to a table whose name is a plural
  162. * word or phrase in lower case underscore form.
  163. * @param string $class_name Name of {@link ActiveRecord} sub-class
  164. * @return string Pluralized lower_case_underscore form of name
  165. */
  166. function tableize($class_name) {
  167. return self::pluralize(self::underscore($class_name));
  168. }
  169.  
  170. /**
  171. * Convert a table name to the corresponding class name
  172. *
  173. * @param string $table_name Name of table in the database
  174. * @return string Singular CamelCase form of $table_name
  175. */
  176. function classify($table_name) {
  177. return self::camelize(self::singularize($table_name));
  178. }
  179.  
  180. /**
  181. * Get foreign key column corresponding to a table name
  182. *
  183. * @param string $table_name Name of table referenced by foreign
  184. * key
  185. * @return string Column name of the foreign key column
  186. */
  187. function foreign_key($class_name) {
  188. return self::underscore($class_name) . "_id";
  189. }
  190. }
  191.  
  192.  
  193. ?>

Documentation generated on Thu, 04 May 2006 19:47:46 -0600 by phpDocumentor 1.3.0RC4