Inflector.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. return strtolower($regex4);
  171. }
  172. /**
  173. * Returns a human-readable string from $word
  174. *
  175. * Returns a human-readable string from $word, by replacing
  176. * underscores with a space, and by upper-casing the initial
  177. * character by default.
  178. *
  179. * If you need to uppercase all the words you just have to
  180. * pass 'all' as a second parameter.
  181. *
  182. * @param string $word String to "humanize"
  183. * @param string $uppercase If set to 'all' it will uppercase all the words
  184. * instead of just the first one.
  185. *
  186. * @return string Human-readable word
  187. */
  188. public static function humanize($word, $uppercase = '')
  189. {
  190. $uppercase = $uppercase === 'all' ? 'ucwords' : 'ucfirst';
  191. return $uppercase(str_replace('_', ' ', preg_replace('/_id$/', '', $word)));
  192. }
  193. /**
  194. * Same as camelize but first char is underscored
  195. *
  196. * Converts a word like "send_email" to "sendEmail". It
  197. * will remove non alphanumeric character from the word, so
  198. * "who's online" will be converted to "whoSOnline"
  199. *
  200. * @see camelize
  201. *
  202. * @param string $word Word to lowerCamelCase
  203. *
  204. * @return string Returns a lowerCamelCasedWord
  205. */
  206. public static function variablize($word)
  207. {
  208. $word = static::camelize($word);
  209. return strtolower($word[0]) . substr($word, 1);
  210. }
  211. /**
  212. * Converts a class name to its table name according to rails
  213. * naming conventions.
  214. *
  215. * Converts "Person" to "people"
  216. *
  217. * @see classify
  218. *
  219. * @param string $class_name Class name for getting related table_name.
  220. *
  221. * @return string plural_table_name
  222. */
  223. public static function tableize($class_name)
  224. {
  225. return static::pluralize(static::underscorize($class_name));
  226. }
  227. /**
  228. * Converts a table name to its class name according to rails
  229. * naming conventions.
  230. *
  231. * Converts "people" to "Person"
  232. *
  233. * @see tableize
  234. *
  235. * @param string $table_name Table name for getting related ClassName.
  236. *
  237. * @return string SingularClassName
  238. */
  239. public static function classify($table_name)
  240. {
  241. return static::camelize(static::singularize($table_name));
  242. }
  243. /**
  244. * Converts number to its ordinal English form.
  245. *
  246. * This method converts 13 to 13th, 2 to 2nd ...
  247. *
  248. * @param int $number Number to get its ordinal value
  249. *
  250. * @return string Ordinal representation of given string.
  251. */
  252. public static function ordinalize($number)
  253. {
  254. static::init();
  255. if (\in_array($number % 100, range(11, 13), true)) {
  256. return $number . static::$ordinals['default'];
  257. }
  258. switch ($number % 10) {
  259. case 1:
  260. return $number . static::$ordinals['first'];
  261. case 2:
  262. return $number . static::$ordinals['second'];
  263. case 3:
  264. return $number . static::$ordinals['third'];
  265. default:
  266. return $number . static::$ordinals['default'];
  267. }
  268. }
  269. /**
  270. * Converts a number of days to a number of months
  271. *
  272. * @param int $days
  273. *
  274. * @return int
  275. */
  276. public static function monthize($days)
  277. {
  278. $now = new \DateTime();
  279. $end = new \DateTime();
  280. $duration = new \DateInterval("P{$days}D");
  281. $diff = $end->add($duration)->diff($now);
  282. // handle years
  283. if ($diff->y > 0) {
  284. $diff->m += 12 * $diff->y;
  285. }
  286. return $diff->m;
  287. }
  288. }