Inflector.php 9.1 KB

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