Inflector.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. <?php
  2. /**
  3. * @package Grav.Common
  4. *
  5. * @copyright Copyright (C) 2015 - 2018 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Common;
  9. use Grav\Common\Grav;
  10. /**
  11. * This file was originally part of the Akelos Framework
  12. */
  13. class Inflector
  14. {
  15. protected $plural;
  16. protected $singular;
  17. protected $uncountable;
  18. protected $irregular;
  19. protected $ordinals;
  20. public function init()
  21. {
  22. if (empty($this->plural)) {
  23. $language = Grav::instance()['language'];
  24. $this->plural = $language->translate('INFLECTOR_PLURALS', null, true) ?: [];
  25. $this->singular = $language->translate('INFLECTOR_SINGULAR', null, true) ?: [];
  26. $this->uncountable = $language->translate('INFLECTOR_UNCOUNTABLE', null, true) ?: [];
  27. $this->irregular = $language->translate('INFLECTOR_IRREGULAR', null, true) ?: [];
  28. $this->ordinals = $language->translate('INFLECTOR_ORDINALS', null, true) ?: [];
  29. }
  30. }
  31. /**
  32. * Pluralizes English nouns.
  33. *
  34. * @param string $word English noun to pluralize
  35. * @param int $count The count
  36. *
  37. * @return string Plural noun
  38. */
  39. public function pluralize($word, $count = 2)
  40. {
  41. $this->init();
  42. if ($count == 1) {
  43. return $word;
  44. }
  45. $lowercased_word = strtolower($word);
  46. foreach ($this->uncountable as $_uncountable) {
  47. if (substr($lowercased_word, (-1 * strlen($_uncountable))) == $_uncountable) {
  48. return $word;
  49. }
  50. }
  51. foreach ($this->irregular as $_plural => $_singular) {
  52. if (preg_match('/(' . $_plural . ')$/i', $word, $arr)) {
  53. return preg_replace('/(' . $_plural . ')$/i', substr($arr[0], 0, 1) . substr($_singular, 1), $word);
  54. }
  55. }
  56. foreach ($this->plural as $rule => $replacement) {
  57. if (preg_match($rule, $word)) {
  58. return preg_replace($rule, $replacement, $word);
  59. }
  60. }
  61. return false;
  62. }
  63. /**
  64. * Singularizes English nouns.
  65. *
  66. * @param string $word English noun to singularize
  67. * @param int $count
  68. *
  69. * @return string Singular noun.
  70. */
  71. public function singularize($word, $count = 1)
  72. {
  73. $this->init();
  74. if ($count != 1) {
  75. return $word;
  76. }
  77. $lowercased_word = strtolower($word);
  78. foreach ($this->uncountable as $_uncountable) {
  79. if (substr($lowercased_word, (-1 * strlen($_uncountable))) == $_uncountable) {
  80. return $word;
  81. }
  82. }
  83. foreach ($this->irregular as $_plural => $_singular) {
  84. if (preg_match('/(' . $_singular . ')$/i', $word, $arr)) {
  85. return preg_replace('/(' . $_singular . ')$/i', substr($arr[0], 0, 1) . substr($_plural, 1), $word);
  86. }
  87. }
  88. foreach ($this->singular as $rule => $replacement) {
  89. if (preg_match($rule, $word)) {
  90. return preg_replace($rule, $replacement, $word);
  91. }
  92. }
  93. return $word;
  94. }
  95. /**
  96. * Converts an underscored or CamelCase word into a English
  97. * sentence.
  98. *
  99. * The titleize public function converts text like "WelcomePage",
  100. * "welcome_page" or "welcome page" to this "Welcome
  101. * Page".
  102. * If second parameter is set to 'first' it will only
  103. * capitalize the first character of the title.
  104. *
  105. * @param string $word Word to format as tile
  106. * @param string $uppercase If set to 'first' it will only uppercase the
  107. * first character. Otherwise it will uppercase all
  108. * the words in the title.
  109. *
  110. * @return string Text formatted as title
  111. */
  112. public function titleize($word, $uppercase = '')
  113. {
  114. $uppercase = $uppercase == 'first' ? 'ucfirst' : 'ucwords';
  115. return $uppercase($this->humanize($this->underscorize($word)));
  116. }
  117. /**
  118. * Returns given word as CamelCased
  119. *
  120. * Converts a word like "send_email" to "SendEmail". It
  121. * will remove non alphanumeric character from the word, so
  122. * "who's online" will be converted to "WhoSOnline"
  123. *
  124. * @see variablize
  125. *
  126. * @param string $word Word to convert to camel case
  127. *
  128. * @return string UpperCamelCasedWord
  129. */
  130. public function camelize($word)
  131. {
  132. return str_replace(' ', '', ucwords(preg_replace('/[^A-Z^a-z^0-9]+/', ' ', $word)));
  133. }
  134. /**
  135. * Converts a word "into_it_s_underscored_version"
  136. *
  137. * Convert any "CamelCased" or "ordinary Word" into an
  138. * "underscored_word".
  139. *
  140. * This can be really useful for creating friendly URLs.
  141. *
  142. * @param string $word Word to underscore
  143. *
  144. * @return string Underscored word
  145. */
  146. public function underscorize($word)
  147. {
  148. $regex1 = preg_replace('/([A-Z]+)([A-Z][a-z])/', '\1_\2', $word);
  149. $regex2 = preg_replace('/([a-zd])([A-Z])/', '\1_\2', $regex1);
  150. $regex3 = preg_replace('/[^A-Z^a-z^0-9]+/', '_', $regex2);
  151. return strtolower($regex3);
  152. }
  153. /**
  154. * Converts a word "into-it-s-hyphenated-version"
  155. *
  156. * Convert any "CamelCased" or "ordinary Word" into an
  157. * "hyphenated-word".
  158. *
  159. * This can be really useful for creating friendly URLs.
  160. *
  161. * @param string $word Word to hyphenate
  162. *
  163. * @return string hyphenized word
  164. */
  165. public function hyphenize($word)
  166. {
  167. $regex1 = preg_replace('/([A-Z]+)([A-Z][a-z])/', '\1-\2', $word);
  168. $regex2 = preg_replace('/([a-z])([A-Z])/', '\1-\2', $regex1);
  169. $regex3 = preg_replace('/([0-9])([A-Z])/', '\1-\2', $regex2);
  170. $regex4 = preg_replace('/[^A-Z^a-z^0-9]+/', '-', $regex3);
  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 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 function variablize($word)
  208. {
  209. $word = $this->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 function tableize($class_name)
  225. {
  226. return $this->pluralize($this->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 function classify($table_name)
  241. {
  242. return $this->camelize($this->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 integer $number Number to get its ordinal value
  250. *
  251. * @return string Ordinal representation of given string.
  252. */
  253. public function ordinalize($number)
  254. {
  255. $this->init();
  256. if (in_array(($number % 100), range(11, 13))) {
  257. return $number . $this->ordinals['default'];
  258. } else {
  259. switch (($number % 10)) {
  260. case 1:
  261. return $number . $this->ordinals['first'];
  262. break;
  263. case 2:
  264. return $number . $this->ordinals['second'];
  265. break;
  266. case 3:
  267. return $number . $this->ordinals['third'];
  268. break;
  269. default:
  270. return $number . $this->ordinals['default'];
  271. break;
  272. }
  273. }
  274. }
  275. /**
  276. * Converts a number of days to a number of months
  277. *
  278. * @param int $days
  279. *
  280. * @return int
  281. */
  282. public function monthize($days)
  283. {
  284. $now = new \DateTime();
  285. $end = new \DateTime();
  286. $duration = new \DateInterval("P{$days}D");
  287. $diff = $end->add($duration)->diff($now);
  288. // handle years
  289. if ($diff->y > 0) {
  290. $diff->m = $diff->m + 12 * $diff->y;
  291. }
  292. return $diff->m;
  293. }
  294. }