SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/Inflector.php

Copyright (C) 2017 Karmabunny Pty Ltd.

This file is a part of SproutCMS.

SproutCMS is free software: you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation, either
version 2 of the License, or (at your option) any later version.

For more information, visit <http://getsproutcms.com>.

This class was originally from Kohana 2.3.4
Copyright 2007-2008 Kohana Team
  1. <?php
  2. /**
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  *
  13.  * This class was originally from Kohana 2.3.4
  14.  * Copyright 2007-2008 Kohana Team
  15.  */
  16. namespace Sprout\Helpers;
  17.  
  18. use Kohana;
  19.  
  20.  
  21. /**
  22.  * Language inflection such as pluralisation.
  23.  */
  24. class Inflector
  25. {
  26.  
  27. // Cached inflections
  28. protected static $cache = array();
  29.  
  30. // Uncountable and irregular words
  31. protected static $uncountable;
  32. protected static $irregular;
  33.  
  34. /**
  35.   * Checks if a word is defined as uncountable.
  36.   *
  37.   * @param string word to check
  38.   * @return boolean
  39.   */
  40. public static function uncountable($str)
  41. {
  42. if (Inflector::$uncountable === NULL)
  43. {
  44. // Cache uncountables
  45. Inflector::$uncountable = Kohana::config('inflector.uncountable');
  46.  
  47. // Make uncountables mirroed
  48. Inflector::$uncountable = array_combine(Inflector::$uncountable, Inflector::$uncountable);
  49. }
  50.  
  51. return isset(Inflector::$uncountable[strtolower($str)]);
  52. }
  53.  
  54. /**
  55.   * Makes a plural word singular.
  56.   *
  57.   * @param string word to singularize
  58.   * @param integer number of things
  59.   * @return string
  60.   */
  61. public static function singular($str, $count = NULL)
  62. {
  63. // Remove garbage
  64. $str = strtolower(trim($str));
  65.  
  66. if (is_string($count))
  67. {
  68. // Convert to integer when using a digit string
  69. $count = (int) $count;
  70. }
  71.  
  72. // Do nothing with a single count
  73. if ($count === 0 OR $count > 1)
  74. return $str;
  75.  
  76. // Cache key name
  77. $key = 'singular_'.$str.$count;
  78.  
  79. if (isset(Inflector::$cache[$key]))
  80. return Inflector::$cache[$key];
  81.  
  82. if (Inflector::uncountable($str))
  83. return Inflector::$cache[$key] = $str;
  84.  
  85. if (empty(Inflector::$irregular))
  86. {
  87. // Cache irregular words
  88. Inflector::$irregular = Kohana::config('inflector.irregular');
  89. }
  90.  
  91. if ($irregular = array_search($str, Inflector::$irregular))
  92. {
  93. $str = $irregular;
  94. }
  95. elseif (preg_match('/[sxz]es$/', $str) OR preg_match('/[^aeioudgkprt]hes$/', $str))
  96. {
  97. // Remove "es"
  98. $str = substr($str, 0, -2);
  99. }
  100. elseif (preg_match('/[^aeiou]ies$/', $str))
  101. {
  102. $str = substr($str, 0, -3).'y';
  103. }
  104. elseif (substr($str, -1) === 's' AND substr($str, -2) !== 'ss')
  105. {
  106. $str = substr($str, 0, -1);
  107. }
  108.  
  109. return Inflector::$cache[$key] = $str;
  110. }
  111.  
  112. /**
  113.   * Makes a singular word plural.
  114.   *
  115.   * @param string word to pluralize
  116.   * @return string
  117.   */
  118. public static function plural($str, $count = NULL)
  119. {
  120. // Remove garbage
  121. $str = strtolower(trim($str));
  122.  
  123. if (is_string($count))
  124. {
  125. // Convert to integer when using a digit string
  126. $count = (int) $count;
  127. }
  128.  
  129. // Do nothing with singular
  130. if ($count === 1)
  131. return $str;
  132.  
  133. // Cache key name
  134. $key = 'plural_'.$str.$count;
  135.  
  136. if (isset(Inflector::$cache[$key]))
  137. return Inflector::$cache[$key];
  138.  
  139. if (Inflector::uncountable($str))
  140. return Inflector::$cache[$key] = $str;
  141.  
  142. if (empty(Inflector::$irregular))
  143. {
  144. // Cache irregular words
  145. Inflector::$irregular = Kohana::config('inflector.irregular');
  146. }
  147.  
  148. if (isset(Inflector::$irregular[$str]))
  149. {
  150. $str = Inflector::$irregular[$str];
  151. }
  152. elseif (preg_match('/[sxz]$/', $str) OR preg_match('/[^aeioudgkprt]h$/', $str))
  153. {
  154. $str .= 'es';
  155. }
  156. elseif (preg_match('/[^aeiou]y$/', $str))
  157. {
  158. // Change "y" to "ies"
  159. $str = substr_replace($str, 'ies', -1);
  160. }
  161. else
  162. {
  163. $str .= 's';
  164. }
  165.  
  166. // Set the cache and return
  167. return Inflector::$cache[$key] = $str;
  168. }
  169.  
  170.  
  171. /**
  172.   * Pluralises a number and word combination, e.g. [1, 'pig'] => '1 pig'; [0, 'pig'] => '0 pigs'
  173.   * @param int $num The number in question
  174.   * @param string $word The word to pluralise if $num is not 1
  175.   */
  176. public static function numPlural($num, $word)
  177. {
  178. if ($num == 1) return "1 {$word}";
  179. return $num . ' ' . self::plural($word);
  180. }
  181.  
  182.  
  183.  
  184. /**
  185.   * Makes a phrase camel case.
  186.   *
  187.   * @param string phrase to camelize
  188.   * @return string
  189.   */
  190. public static function camelize($str)
  191. {
  192. $str = 'x'.strtolower(trim($str));
  193. $str = ucwords(preg_replace('/[\s_]+/', ' ', $str));
  194.  
  195. return substr(str_replace(' ', '', $str), 1);
  196. }
  197.  
  198. /**
  199.   * Makes a phrase underscored instead of spaced.
  200.   *
  201.   * @param string phrase to underscore
  202.   * @return string
  203.   */
  204. public static function underscore($str)
  205. {
  206. return preg_replace('/\s+/', '_', trim($str));
  207. }
  208.  
  209. /**
  210.   * Makes an underscored or dashed phrase human-reable.
  211.   *
  212.   * @param string phrase to make human-reable
  213.   * @return string
  214.   */
  215. public static function humanize($str)
  216. {
  217. return preg_replace('/[_-]+/', ' ', trim($str));
  218. }
  219.  
  220. } // End inflector
  221.