Inflector.php 10 KB

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