InflectShell.php 5.7 KB

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