Inflector.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <?php
  2. /**
  3. * @package Grav\Common
  4. *
  5. * @copyright Copyright (C) 2015 - 2019 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Common;
  9. /**
  10. * This file was originally part of the Akelos Framework
  11. */
  12. class Inflector
  13. {
  14. protected static $plural;
  15. protected static $singular;
  16. protected static $uncountable;
  17. protected static $irregular;
  18. protected static $ordinals;
  19. public static function init()
  20. {
  21. if (empty(static::$plural)) {
  22. $language = Grav::instance()['language'];
  23. static::$plural = $language->translate('GRAV.INFLECTOR_PLURALS', null, true) ?: [];
  24. static::$singular = $language->translate('GRAV.INFLECTOR_SINGULAR', null, true) ?: [];
  25. static::$uncountable = $language->translate('GRAV.INFLECTOR_UNCOUNTABLE', null, true) ?: [];
  26. static::$irregular = $language->translate('GRAV.INFLECTOR_IRREGULAR', null, true) ?: [];
  27. static::$ordinals = $language->translate('GRAV.INFLECTOR_ORDINALS', null, true) ?: [];
  28. }
  29. }
  30. /**
  31. * Pluralizes English nouns.
  32. *
  33. * @param string $word English noun to pluralize
  34. * @param int $count The count
  35. *
  36. * @return string Plural noun
  37. */
  38. public static function pluralize($word, $count = 2)
  39. {
  40. static::init();
  41. if ((int)$count === 1) {
  42. return $word;
  43. }
  44. $lowercased_word = strtolower($word);
  45. foreach (static::$uncountable as $_uncountable) {
  46. if (substr($lowercased_word, -1 * strlen($_uncountable)) === $_uncountable) {
  47. return $word;
  48. }
  49. }
  50. foreach (static::$irregular as $_plural => $_singular) {
  51. if (preg_match('/(' . $_plural . ')$/i', $word, $arr)) {
  52. return preg_replace('/(' . $_plural . ')$/i', substr($arr[0], 0, 1) . substr($_singular, 1), $word);
  53. }
  54. }
  55. foreach (static::$plural as $rule => $replacement) {
  56. if (preg_match($rule, $word)) {
  57. return preg_replace($rule, $replacement, $word);
  58. }
  59. }
  60. return false;
  61. }
  62. /**
  63. * Singularizes English nouns.
  64. *
  65. * @param string $word English noun to singularize
  66. * @param int $count
  67. *
  68. * @return string Singular noun.
  69. */
  70. public static function singularize($word, $count = 1)
  71. {
  72. static::init();
  73. if ((int)$count !== 1) {
  74. return $word;
  75. }
  76. $lowercased_word = strtolower($word);
  77. foreach (static::$uncountable as $_uncountable) {
  78. if (substr($lowercased_word, -1 * strlen($_uncountable)) === $_uncountable) {
  79. return $word;
  80. }
  81. }
  82. foreach (static::$irregular as $_plural => $_singular) {
  83. if (preg_match('/(' . $_singular . ')$/i', $word, $arr)) {
  84. return preg_replace('/(' . $_singular . ')$/i', substr($arr[0], 0, 1) . substr($_plural, 1), $word);
  85. }
  86. }
  87. foreach (static::$singular as $rule => $replacement) {
  88. if (preg_match($rule, $word)) {
  89. return preg_replace($rule, $replacement, $word);
  90. }
  91. }
  92. return $word;
  93. }
  94. /**
  95. * Converts an underscored or CamelCase word into a English
  96. * sentence.
  97. *
  98. * The titleize public function converts text like "WelcomePage",
  99. * "welcome_page" or "welcome page" to this "Welcome
  100. * Page".
  101. * If second parameter is set to 'first' it will only
  102. * capitalize the first character of the title.
  103. *
  104. * @param string $word Word to format as tile
  105. * @param string $uppercase If set to 'first' it will only uppercase the
  106. * first character. Otherwise it will uppercase all
  107. * the words in the title.
  108. *
  109. * @return string Text formatted as title
  110. */
  111. public static function titleize($word, $uppercase = '')
  112. {
  113. $uppercase = $uppercase === 'first' ? 'ucfirst' : 'ucwords';
  114. return $uppercase(static::humanize(static::underscorize($word)));
  115. }
  116. /**
  117. * Returns given word as CamelCased
  118. *
  119. * Converts a word like "send_email" to "SendEmail". It
  120. * will remove non alphanumeric character from the word, so
  121. * "who's online" will be converted to "WhoSOnline"
  122. *
  123. * @see variablize
  124. *
  125. * @param string $word Word to convert to camel case
  126. *
  127. * @return string UpperCamelCasedWord
  128. */
  129. public static function camelize($word)
  130. {
  131. return str_replace(' ', '', ucwords(preg_replace('/[^A-Z^a-z^0-9]+/', ' ', $word)));
  132. }
  133. /**
  134. * Converts a word "into_it_s_underscored_version"
  135. *
  136. * Convert any "CamelCased" or "ordinary Word" into an
  137. * "underscored_word".
  138. *
  139. * This can be really useful for creating friendly URLs.
  140. *
  141. * @param string $word Word to underscore
  142. *
  143. * @return string Underscored word
  144. */
  145. public static function underscorize($word)
  146. {
  147. $regex1 = preg_replace('/([A-Z]+)([A-Z][a-z])/', '\1_\2', $word);
  148. $regex2 = preg_replace('/([a-zd])([A-Z])/', '\1_\2', $regex1);
  149. $regex3 = preg_replace('/[^A-Z^a-z^0-9]+/', '_', $regex2);
  150. return strtolower($regex3);
  151. }
  152. /**
  153. * Converts a word "into-it-s-hyphenated-version"
  154. *
  155. * Convert any "CamelCased" or "ordinary Word" into an
  156. * "hyphenated-word".
  157. *
  158. * This can be really useful for creating friendly URLs.
  159. *
  160. * @param string $word Word to hyphenate
  161. *
  162. * @return string hyphenized word
  163. */
  164. public static function hyphenize($word)
  165. {
  166. $regex1 = preg_replace('/([A-Z]+)([A-Z][a-z])/', '\1-\2', $word);
  167. $regex2 = preg_replace('/([a-z])([A-Z])/', '\1-\2', $regex1);
  168. $regex3 = preg_replace('/([0-9])([A-Z])/', '\1-\2', $regex2);
  169. $regex4 = preg_replace('/[^A-Z^a-z^0-9]+/', '-', $regex3);
  170. $regex4 = trim($regex4, '-');
  171. return strtolower($regex4);
  172. }
  173. /**
  174. * Returns a human-readable string from $word
  175. *
  176. * Returns a human-readable string from $word, by replacing
  177. * underscores with a space, and by upper-casing the initial
  178. * character by default.
  179. *
  180. * If you need to uppercase all the words you just have to
  181. * pass 'all' as a second parameter.
  182. *
  183. * @param string $word String to "humanize"
  184. * @param string $uppercase If set to 'all' it will uppercase all the words
  185. * instead of just the first one.
  186. *
  187. * @return string Human-readable word
  188. */
  189. public static function humanize($word, $uppercase = '')
  190. {
  191. $uppercase = $uppercase === 'all' ? 'ucwords' : 'ucfirst';
  192. return $uppercase(str_replace('_', ' ', preg_replace('/_id$/', '', $word)));
  193. }
  194. /**
  195. * Same as camelize but first char is underscored
  196. *
  197. * Converts a word like "send_email" to "sendEmail". It
  198. * will remove non alphanumeric character from the word, so
  199. * "who's online" will be converted to "whoSOnline"
  200. *
  201. * @see camelize
  202. *
  203. * @param string $word Word to lowerCamelCase
  204. *
  205. * @return string Returns a lowerCamelCasedWord
  206. */
  207. public static function variablize($word)
  208. {
  209. $word = static::camelize($word);
  210. return strtolower($word[0]) . substr($word, 1);
  211. }
  212. /**
  213. * Converts a class name to its table name according to rails
  214. * naming conventions.
  215. *
  216. * Converts "Person" to "people"
  217. *
  218. * @see classify
  219. *
  220. * @param string $class_name Class name for getting related table_name.
  221. *
  222. * @return string plural_table_name
  223. */
  224. public static function tableize($class_name)
  225. {
  226. return static::pluralize(static::underscorize($class_name));
  227. }
  228. /**
  229. * Converts a table name to its class name according to rails
  230. * naming conventions.
  231. *
  232. * Converts "people" to "Person"
  233. *
  234. * @see tableize
  235. *
  236. * @param string $table_name Table name for getting related ClassName.
  237. *
  238. * @return string SingularClassName
  239. */
  240. public static function classify($table_name)
  241. {
  242. return static::camelize(static::singularize($table_name));
  243. }
  244. /**
  245. * Converts number to its ordinal English form.
  246. *
  247. * This method converts 13 to 13th, 2 to 2nd ...
  248. *
  249. * @param int $number Number to get its ordinal value
  250. *
  251. * @return string Ordinal representation of given string.
  252. */
  253. public static function ordinalize($number)
  254. {
  255. static::init();
  256. if (\in_array($number % 100, range(11, 13), true)) {
  257. return $number . static::$ordinals['default'];
  258. }
  259. switch ($number % 10) {
  260. case 1:
  261. return $number . static::$ordinals['first'];
  262. case 2:
  263. return $number . static::$ordinals['second'];
  264. case 3:
  265. return $number . static::$ordinals['third'];
  266. default:
  267. return $number . static::$ordinals['default'];
  268. }
  269. }
  270. /**
  271. * Converts a number of days to a number of months
  272. *
  273. * @param int $days
  274. *
  275. * @return int
  276. */
  277. public static function monthize($days)
  278. {
  279. $now = new \DateTime();
  280. $end = new \DateTime();
  281. $duration = new \DateInterval("P{$days}D");
  282. $diff = $end->add($duration)->diff($now);
  283. // handle years
  284. if ($diff->y > 0) {
  285. $diff->m += 12 * $diff->y;
  286. }
  287. return $diff->m;
  288. }
  289. }