InflectShell.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. namespace Tools\Shell;
  3. use Cake\Console\Shell;
  4. use Cake\Utility\Inflector;
  5. /**
  6. * Inflect Shell
  7. *
  8. * Inflect the heck out of your word(s)
  9. *
  10. * @author Jose Diaz-Gonzalez
  11. * @author Mark Scherer
  12. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  13. */
  14. class InflectShell extends Shell {
  15. /**
  16. * Valid inflection rules
  17. *
  18. * @var array
  19. */
  20. public $validMethods = [
  21. 'pluralize', 'singularize', 'camelize',
  22. 'underscore', 'humanize', 'tableize',
  23. 'classify', 'variable', 'dasherize', 'slug',
  24. ];
  25. /**
  26. * Valid inflection rules
  27. *
  28. * @var array
  29. */
  30. public $validCommands = [
  31. 'pluralize', 'singularize', 'camelize',
  32. 'underscore', 'humanize', 'tableize',
  33. 'classify', 'variable', 'dasherize', 'slug', 'all', 'quit',
  34. ];
  35. /**
  36. * Inflects words
  37. *
  38. * @return bool|int|null
  39. */
  40. public function main() {
  41. if (!empty($this->args)) {
  42. $arguments = $this->_parseArguments($this->args);
  43. } else {
  44. $arguments = $this->_interactive();
  45. }
  46. $this->_inflect($arguments['method'], $arguments['words']);
  47. }
  48. /**
  49. * Prompts the user for words
  50. *
  51. * @return array
  52. */
  53. protected function _interactive() {
  54. $method = $this->_getMethod();
  55. $words = $this->_getWords();
  56. return ['method' => $method, 'words' => $words];
  57. }
  58. /**
  59. * Requests a valid inflection method
  60. *
  61. * @return string|null
  62. */
  63. protected function _getMethod() {
  64. $validCharacters = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'q'];
  65. $validCommands = array_merge($validCharacters, $this->validCommands);
  66. $command = null;
  67. while (empty($command)) {
  68. $this->out('Please type the number or name of the inflection method you would like to use');
  69. $this->hr();
  70. $this->out('[1] Pluralize');
  71. $this->out('[2] Singularize');
  72. $this->out('[3] Camelize');
  73. $this->out('[4] Underscore');
  74. $this->out('[5] Humanize');
  75. $this->out('[6] Tableize');
  76. $this->out('[7] Classify');
  77. $this->out('[8] Variable');
  78. $this->out('[9] Dasherize');
  79. $this->out('[10] Slug');
  80. $this->out('[q] Quit');
  81. $temp = $this->in('What command would you like to perform?', null, 'q');
  82. if (in_array(strtolower($temp), $validCommands)) {
  83. $command = strtolower($temp);
  84. } else {
  85. $this->out('Try again.');
  86. }
  87. }
  88. switch ($command) {
  89. case '1':
  90. case 'pluralize':
  91. return 'pluralize';
  92. case '2':
  93. case 'singularize':
  94. return 'singularize';
  95. case '3':
  96. case 'camelize':
  97. return 'camelize';
  98. case '4':
  99. case 'underscore':
  100. return 'underscore';
  101. case '5':
  102. case 'humanize':
  103. return 'humanize';
  104. case '6':
  105. case 'tableize':
  106. return 'tableize';
  107. case '7':
  108. case 'classify':
  109. return 'classify';
  110. case '8':
  111. case 'variable':
  112. return 'variable';
  113. case '9':
  114. case 'dasherize':
  115. return 'dasherize';
  116. case '10':
  117. case 'slug':
  118. return 'slug';
  119. case 'q':
  120. case 'quit':
  121. default:
  122. $this->out('Exit');
  123. $this->_stop();
  124. return null;
  125. }
  126. }
  127. /**
  128. * Requests words to inflect
  129. *
  130. * @return string|null
  131. */
  132. protected function _getWords() {
  133. $words = null;
  134. while (empty($words)) {
  135. $temp = $this->in('What word(s) would you like to inflect?');
  136. if (!empty($temp)) {
  137. $words = $temp;
  138. } else {
  139. $this->out('Try again.');
  140. }
  141. }
  142. return $words;
  143. }
  144. /**
  145. * Parse the arguments into the function and the word(s) to be inflected
  146. *
  147. * @param array $arguments
  148. * @return array
  149. */
  150. protected function _parseArguments($arguments) {
  151. $words = null;
  152. $function = $arguments[0];
  153. unset($arguments[0]);
  154. if (!in_array($function, array_merge($this->validMethods, ['all']))) {
  155. $function = $this->_getMethod();
  156. }
  157. $arguments = array_reverse($arguments);
  158. if (count($arguments) == 0) {
  159. $words = $this->_getWords();
  160. } else {
  161. while (count($arguments) > 0) {
  162. $words .= array_pop($arguments);
  163. if (count($arguments) > 0) {
  164. $words .= ' ';
  165. }
  166. }
  167. }
  168. return ['method' => $function, 'words' => $words];
  169. }
  170. /**
  171. * Inflects a set of words based upon the inflection set in the arguments
  172. *
  173. * @param string $function
  174. * @param string $words
  175. * @return void
  176. */
  177. protected function _inflect($function, $words) {
  178. $this->out($words);
  179. if ($function === 'all') {
  180. foreach ($this->validMethods as $method) {
  181. $functionName = $this->_getMessage($method);
  182. $this->out("{$functionName}: " . Inflector::$method($words));
  183. }
  184. } else {
  185. $functionName = $this->_getMessage($function);
  186. $this->out("{$functionName}: " . Inflector::$function($words));
  187. }
  188. }
  189. /**
  190. * Returns the appropriate message for a given function
  191. *
  192. * @param string $function
  193. * @return string
  194. */
  195. protected function _getMessage($function) {
  196. $messages = [
  197. 'camelize' => 'CamelCase form ',
  198. 'classify' => 'Cake Model Class form ',
  199. 'humanize' => 'Human Readable Group form ',
  200. 'singularize' => 'Singular form ',
  201. 'dasherize' => 'Dasherized-form ',
  202. 'slug' => 'Slugged-form ',
  203. 'pluralize' => 'Pluralized form ',
  204. 'tableize' => 'table_names form ',
  205. 'underscore' => 'under_scored_form ',
  206. 'variable' => 'variableForm ',
  207. ];
  208. return $messages[$function];
  209. }
  210. /**
  211. * Displays help contents
  212. *
  213. * @return void
  214. */
  215. public function help() {
  216. $this->out('Inflector Shell');
  217. $this->out('');
  218. $this->out('This shell uses the Inflector class to inflect any word(s) you wish');
  219. $this->hr();
  220. $this->out('Usage: cake inflect');
  221. $this->out(' cake inflect methodName');
  222. $this->out(' cake inflect methodName word');
  223. $this->out(' cake inflect methodName words to inflect');
  224. $this->out('');
  225. }
  226. }