PageRenderTime 50ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/intromagang/blog/system/src/Grav/Common/Inflector.php

https://gitlab.com/akbaryu/intro_magang_web
PHP | 332 lines | 144 code | 39 blank | 149 comment | 19 complexity | eb18b4ec793e94b1e871caf33f06f403 MD5 | raw file
  1. <?php
  2. /**
  3. * @package Grav.Common
  4. *
  5. * @copyright Copyright (C) 2014 - 2016 RocketTheme, 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-zd])([A-Z])/', '\1-\2', $regex1);
  169. $regex3 = preg_replace('/[^A-Z^a-z^0-9]+/', '-', $regex2);
  170. return strtolower($regex3);
  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 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 function variablize($word)
  207. {
  208. $word = $this->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 function tableize($class_name)
  224. {
  225. return $this->pluralize($this->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 function classify($table_name)
  240. {
  241. return $this->camelize($this->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 integer $number Number to get its ordinal value
  249. *
  250. * @return string Ordinal representation of given string.
  251. */
  252. public function ordinalize($number)
  253. {
  254. $this->init();
  255. if (in_array(($number % 100), range(11, 13))) {
  256. return $number . $this->ordinals['default'];
  257. } else {
  258. switch (($number % 10)) {
  259. case 1:
  260. return $number . $this->ordinals['first'];
  261. break;
  262. case 2:
  263. return $number . $this->ordinals['second'];
  264. break;
  265. case 3:
  266. return $number . $this->ordinals['third'];
  267. break;
  268. default:
  269. return $number . $this->ordinals['default'];
  270. break;
  271. }
  272. }
  273. }
  274. /**
  275. * Converts a number of days to a number of months
  276. *
  277. * @param int $days
  278. *
  279. * @return int
  280. */
  281. public function monthize($days)
  282. {
  283. $now = new \DateTime();
  284. $end = new \DateTime();
  285. $duration = new \DateInterval("P{$days}D");
  286. $diff = $end->add($duration)->diff($now);
  287. // handle years
  288. if ($diff->y > 0) {
  289. $diff->m = $diff->m + 12 * $diff->y;
  290. }
  291. return $diff->m;
  292. }
  293. }